Adding Data to a existing AppendBlob/Block BLOB

Ashutosh Saini 36 Reputation points
2021-11-24T23:50:55.037+00:00

Is it possible to add rows to an existing append/Block blob in premium tier. We are facing issues that if we try to add it overrides the older data and in the end only 1 record in present which is updated in the last.

We need to write data to a file from multiple instances of the application which will be receiving data from event hub.

Using Java we tried below approach but didn't work:

BlobClient blobClient = containerClient.getBlobClient("appendBlobTest.txt");
        //blobClient.
        AppendBlobClient appendClient = blobClient.getAppendBlobClient();

        InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
        appendClient.appendBlock(stream, data.length());
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,425 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. shiva patpi 13,131 Reputation points Microsoft Employee
    2021-11-26T00:31:28.687+00:00

    Hello @Ashutosh Saini ,
    Thanks for reaching out to Microsoft Q&A platform.
    You can't append to Block Blob but I just tried for Append Blob using C#.net , below is the piece of tested code and it works by appending to the existing file. I am sure similar functions will work for Java SDK too, modify the code accordingly to get your stream. Please try out.

        public static void AppendToBlobStorage()  
    
        {  
    
            string connection = "DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accountkey;EndpointSuffix=core.windows.net";  
    
            string filename = "appendblob.txt";  
    
            string container= "test";  
    
            CloudStorageAccount storage = CloudStorageAccount.Parse(connection);  
            CloudBlobClient client = storage.CreateCloudBlobClient();  
            CloudBlobContainer container = client.GetContainerReference(container);  
            CloudAppendBlob blob = container.GetAppendBlobReference(filename);  
    
            using (MemoryStream stream = new MemoryStream())  
            using (StreamWriter sw = new StreamWriter(stream))  
            {  
                sw.WriteLine("This is appended line");  
                sw.Flush();  
                stream.Position = 0;  
                blob.AppendFromStream(stream);  
            }  
        }  
    

    If that helps out , kindly make sure to Upvote & Accept the answer.

    1 person found this answer helpful.

  2. SaiKishor-MSFT 17,181 Reputation points
    2022-10-14T20:38:46.607+00:00

    @Sergey Shabalov Thanks for reaching out to Microsoft Q&A. I understand that you are having issues with appending data to your storage file.

    I read through your code and itseems like right now, this code is attempted to append and flush to the same offset over and over again. The offsets for the Append and Flush needs to be updated each iteration of that loop. Checkout the DatalakeClient.AppendAsync method and the offset parameter to understand better here- https://learn.microsoft.com/en-us/dotnet/api/azure.storage.files.datalake.datalakefileclient.appendasync?view=azure-dotnet#azure-storage-files-datalake-datalakefileclient-appendasync(system-io-stream-system-int64-azure-storage-files-datalake-models-datalakefileappendoptions-system-threading-cancellationtoken)

    Please let us know if you have any more questions and we will be glad to assist you further. Thank you!

    Remember:

    Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer. Here is how.

    Want a reminder to come back and check responses? Here is how to subscribe to a notification.

    0 comments No comments