boolingBloke
boolingBloke

Reputation: 31

Creating and Uploading Append Blobs to store in Azure Container

I am trying to upload a new append blob file to a container every time a message comes in from a service bus. I do not want to append to the blob that is already there. I want to create a whole new append blob and add it at the end.

Is this possible?

I was looking at this article but couldn't quiet understand what they meant when they got to the content part: https://azuresdkdocs.blob.core.windows.net/$web/javascript/azure-storage-blob/12.1.1/classes/appendblobclient.html#appendblock

Here is the code that I have so far:

public static async void StoreToBlob(Services service)
    {
        //Serealize Object 
        var sender = JsonConvert.SerializeObject(service);

        // Create a BlobServiceClient object which will be used to create a container client
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

        // Create the container and return a container client object
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

        // Create the container if it doesn't already exist.  
        await containerClient.CreateIfNotExistsAsync();

        //Reference to blob
        AppendBlobClient appendBlobClient = containerClient.GetAppendBlobClient("services" + Guid.NewGuid().ToString() + ".json");

        // Create the blob. 
        appendBlobClient.Create();

        await appendBlobClient.AppendBlock(sender, sender.Length); //here is where I am having an issue
    }

Upvotes: 1

Views: 3681

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136336

Can you try something like the following (not tested code):

byte[] blockContent = Encoding.UTF8.GetBytes(sender);
using (var ms = new MemoryStream(blockContent))
{
    appendBlobClient.AppendBlock(ms, blockContent.Length);
}

Essentially we're converting the string to byte array, creating a stream out of it and then uploading that stream.

Upvotes: 1

Related Questions