Updating a blob content in azure container

Yididiya Samuel 6 Reputation points
2021-12-09T17:01:36.45+00:00

Greetings,

Is there any way to update the content of a blob (Using PATCH or PUT method) that is in Azure container using rest API in any preferred programming language(Python, Node...)? I have a user's list json file in the blob storage and I want to add a user on a button click to the blob storage.

Thanks,

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. shiva patpi 13,366 Reputation points Microsoft Employee Moderator
    2021-12-10T02:08:39.867+00:00

    Hello @Yididiya Samuel ,
    If the blob is of type "Appendblob" , then we can update the content.
    Below is the C#.net code

    public static void AppendUsersToExistingJsonInBlobStorage()

        {  
    
            string connection = "DefaultEndpointsProtocol=https;AccountName=storageaccountname;AccountKey=key=;EndpointSuffix=core.windows.net";  
    
            string filename = "users.json";  
    
            string containerstring = "users";  
    
            CloudStorageAccount storage = CloudStorageAccount.Parse(connection);  
            CloudBlobClient client = storage.CreateCloudBlobClient();  
            CloudBlobContainer container = client.GetContainerReference(containerstring);  
            CloudAppendBlob blob = container.GetAppendBlobReference(filename);  
    
            using (MemoryStream stream = new MemoryStream())  
            using (StreamWriter sw = new StreamWriter(stream))  
            {  
                sw.WriteLine("user3");  
                sw.Flush();  
                stream.Position = 0;  
                blob.AppendFromStream(stream);  
            }  
        }  
    

    Please note: Above code might not maintain the JSON formatting after adding/updating the blob , so please modify accordingly based upon your requirements

    Regards,
    Shiva.

    1 person found this answer helpful.

  2. Antara Das 381 Reputation points
    2021-12-10T14:06:38.16+00:00

    @Yididiya Samuel

    with python sdk you can check this link:
    https://learn.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.appendblobservice.appendblobservice?view=azure-python-previous

    for example :

    from azure.storage.blob.appendblobservice import AppendBlobService  
      
    append_blob = AppendBlobService(account_name=None, account_key=None, sas_token=None)  
    append_blob.append_blob_from_text()  
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.