Skip to content

Commit e75deae

Browse files
Update sample for append blob article
1 parent bdf0d49 commit e75deae

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

articles/storage/blobs/storage-blob-append.md

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,48 +36,34 @@ Use either of these methods to append data to that append blob:
3636
The maximum size in bytes of each append operation is defined by the [AppendBlobMaxAppendBlockBytes](/dotnet/api/azure.storage.blobs.specialized.appendblobclient.appendblobmaxappendblockbytes) property. The following example creates an append blob and appends log data to that blob. This example uses the [AppendBlobMaxAppendBlockBytes](/dotnet/api/azure.storage.blobs.specialized.appendblobclient.appendblobmaxappendblockbytes) property to determine whether multiple append operations are required.
3737

3838
```csharp
39-
public static async void AppendToBlob
40-
(BlobContainerClient containerClient, MemoryStream logEntryStream, string LogBlobName)
39+
static async Task AppendToBlob(
40+
BlobContainerClient containerClient,
41+
MemoryStream logEntryStream,
42+
string logBlobName)
4143
{
42-
AppendBlobClient appendBlobClient = containerClient.GetAppendBlobClient(LogBlobName);
44+
AppendBlobClient appendBlobClient = containerClient.GetAppendBlobClient(logBlobName);
4345

44-
appendBlobClient.CreateIfNotExists();
46+
await appendBlobClient.CreateIfNotExistsAsync();
4547

46-
var maxBlockSize = appendBlobClient.AppendBlobMaxAppendBlockBytes;
47-
48-
var buffer = new byte[maxBlockSize];
48+
var maxBlockSize = appendBlobClient.AppendBlobMaxAppendBlockBytes;
4949

5050
if (logEntryStream.Length <= maxBlockSize)
5151
{
52-
appendBlobClient.AppendBlock(logEntryStream);
52+
await appendBlobClient.AppendBlockAsync(logEntryStream);
5353
}
5454
else
5555
{
56-
var bytesLeft = (logEntryStream.Length - logEntryStream.Position);
56+
var bytesLeft = logEntryStream.Length;
5757

5858
while (bytesLeft > 0)
5959
{
60-
if (bytesLeft >= maxBlockSize)
61-
{
62-
buffer = new byte[maxBlockSize];
63-
await logEntryStream.ReadAsync
64-
(buffer, 0, maxBlockSize);
65-
}
66-
else
67-
{
68-
buffer = new byte[bytesLeft];
69-
await logEntryStream.ReadAsync
70-
(buffer, 0, Convert.ToInt32(bytesLeft));
71-
}
72-
73-
appendBlobClient.AppendBlock(new MemoryStream(buffer));
74-
75-
bytesLeft = (logEntryStream.Length - logEntryStream.Position);
76-
60+
var blockSize = (int)Math.Min(bytesLeft, maxBlockSize);
61+
var buffer = new byte[blockSize];
62+
await logEntryStream.ReadAsync(buffer, 0, blockSize);
63+
await appendBlobClient.AppendBlockAsync(new MemoryStream(buffer));
64+
bytesLeft -= blockSize;
7765
}
78-
7966
}
80-
8167
}
8268
```
8369

0 commit comments

Comments
 (0)