0

I am using the below code to append data to Azure blob using python.

from azure.storage.blob import AppendBlobService
append_blob_service = AppendBlobService(account_name='myaccount', account_key='mykey')

# The same containers can hold all types of blobs
append_blob_service.create_container('mycontainer')

# Append blobs must be created before they are appended to
append_blob_service.create_blob('mycontainer', 'myappendblob')
append_blob_service.append_blob_from_text('mycontainer', 'myappendblob', u'Hello, world!')

append_blob = append_blob_service.get_blob_to_text('mycontainer', 'myappendblob')

The above code works fine, but when I tried to insert new data, the old data gets overwritten. Is there any way I can append data to 'myappendblob'

2
  • Are you calling the same code every time you try to append contents to the blob? Commented Jun 29, 2022 at 16:18
  • @GauravMantri Yes, same code with different data to append. Commented Jun 29, 2022 at 16:38

2 Answers 2

1

Considering you are calling the same code to append the data, the issue is with the following line of code:

append_blob_service.create_blob('mycontainer', 'myappendblob')

If you read the documentation for create_blob method, you will notice the following:

Creates a blob or overrides an existing blob. Use if_none_match=* to prevent overriding an existing blob.

So essentially you are overriding the blob every time you call your code.

You should call this method with if_none_match="*" as the documentation suggests. If the blob exists, your code will throw an exception which you will need to handle.

Sign up to request clarification or add additional context in comments.

4 Comments

append_blob_service.create_blob('raw', 'appendblob2',if_none_match=*) it give me invalid syntax. Kindly let me know what im missing here. Thank you
Try append_blob_service.create_blob('raw', 'appendblob2',if_none_match="*") (essentially put * in double quotes).
Thank you it worked. Is there any way we can load the data to a table from Python.
Is there any way we can load the data to a table from Python - Please ask a separate question for this and provide all the details there.
0

Try this code which is taken from the Document and it is given by @Harsh Jain ,

from azure.storage.blob import AppendBlobService
   def append_data_to_blob(data):
     service = AppendBlobService(account_name="<Storage acc name>",

       account_key="<Storage acc key>")

     try:

       service.append_blob_from_text(container_name="<name of Conatiner >", blob_name="<The name of file>", text = data)

     except:

       service.create_blob(container_name="<name of Conatiner >", blob_name="<the name of file>")

       service.append_blob_from_text(container_name="<name of Conatiner>", blob_name="<the name of file>", text = data)

     print('Data got Appended ')

 append_data_to_blob('Hi blob')

Taken References from:

https://www.educative.io/answers/how-to-append-data-in-blob-storage-in-azure-using-python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.