Append data to a blob with .NET - Azure Storage (original) (raw)
You can append data to a blob by creating an append blob. Append blobs are made up of blocks like block blobs, but are optimized for append operations. Append blobs are ideal for scenarios such as logging data from virtual machines.
Create an append blob and append data
Use these methods to create an append blob.
Use either of these methods to append data to that append blob:
The maximum size in bytes of each append operation is defined by the AppendBlobMaxAppendBlockBytes property. The following example creates an append blob and appends log data to that blob. This example uses the AppendBlobMaxAppendBlockBytes property to determine whether multiple append operations are required.
static async Task AppendToBlob(
BlobContainerClient containerClient,
MemoryStream logEntryStream,
string logBlobName)
{
AppendBlobClient appendBlobClient = containerClient.GetAppendBlobClient(logBlobName);
await appendBlobClient.CreateIfNotExistsAsync();
int maxBlockSize = appendBlobClient.AppendBlobMaxAppendBlockBytes;
long bytesLeft = logEntryStream.Length;
byte[] buffer = new byte[maxBlockSize];
while (bytesLeft > 0)
{
int blockSize = (int)Math.Min(bytesLeft, maxBlockSize);
int bytesRead = await logEntryStream.ReadAsync(buffer.AsMemory(0, blockSize));
await using (MemoryStream memoryStream = new MemoryStream(buffer, 0, bytesRead))
{
await appendBlobClient.AppendBlockAsync(memoryStream);
}
bytesLeft -= bytesRead;
}
}