I'm trying to append new lines of json to existing json file in container. Below is my code:
import json
import os
from azure.storage.blob import (
BlockBlobService, AppendBlobService
)
from DBConnection import account_name, container_name, account_key
def getData(self, resp, filename):
blobService = BlockBlobService(account_name=account_name, account_key=account_key)
appendblobservice = AppendBlobService(account_name=account_name, account_key=account_key)
resp = json.dumps(self.resp) #CONVERT FROM DICT TO STR
filename = self.filename + ".json" #eg: 'ww1_abcded_202002031100.json'
file_exist = blobService.exists(container_name, filename)
if file_exist is False:
print("inside IF")
blobService.create_blob_from_text(container_name, filename, self.resp)
else:
print("Inside else")
appendblobservice.append_blob_from_text(container_name, filename, self.resp)
print("2345675t43")
I'm getting error at append_blob_from_text
and producing the following error:
azure.common.AzureConflictHttpError: The blob type is invalid for this operation. ErrorCode: InvalidBlobType
InvalidBlobType
The blob type is invalid for this operation.
I believe you're getting this error is because you're calling a method applicable only for Append Blob
on a Block Blob
.
Following code of yours creates a Block Blob:
blobService.create_blob_from_text(container_name, filename, self.resp)
However you're trying to perform an append blob only operation:
appendblobservice.append_blob_from_text(container_name, filename, self.resp)
Because of this you're getting this error.
Two possible solutions:
create_blob
to create an empty append blob and then append the contents using append_blob_from_text
method.get_blob_to_text
method, append the new content and then reupload the blob using create_blob_from_text
method.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With