Microsoft has introduced a new blob type, CloudAppendBlob for appending data to an existing azure blob. It has the method AppendFromByteArray which appends data to the tail of existing blob.
We need to use CloudAppendBlob type for the blobs for which we want to append data.
It can be useful in a scenario where you want to upload data in a chunk and each chunk will append at the tail of previous chunk.
Here is the sample code for the same.
public string AppendBlobData(string blobName, byte[] fileBytes)
{
if (fileBytes == null || fileBytes.Length == 0)
throw new ArgumentException("Byte Array is empty!");
StorageCredentials creds = new StorageCredentials(accountName, key);
CloudBlobClient blobStorage = new CloudBlobClient(new Uri(baseUri), creds);
CloudBlobContainer container = blobStorage.GetContainerReference(containerAddress);
CloudAppendBlob blob;
if (string.IsNullOrEmpty(blobName))
{
blobName = Guid.NewGuid().ToString();
blob = container.GetAppendBlobReference(blobName);
blob.UploadFromByteArray(fileBytes, 0, fileBytes.Count());
return blobName;
}
else
{
blob = container.GetAppendBlobReference(blobName);
if (blob.Exists())
{
blob.AppendFromByteArray(fileBytes, 0, fileBytes.Count());
return blobName;
}
}
return blobName;
}
In above method, we accept the blob name and byte array as parameter. So if we are uploading a chunk of 10 byte arrays then for first byte array we wouldn`t be having a blob name and it will be passed as empty. Then in the method, we are checking if the blob name is passed or not. If it is passed then we go for appending the data to existing blow but if it is not then we create a new append blob and returns its name as return value.
0 Comment(s)