4

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 InvalidBlobTypeThe blob type is invalid for this operation.

4
  • @GauravMantri I've created a new question. Hopefully you can help! Thanks Commented Feb 5, 2020 at 4:21
  • Answered. Why did you delete your previous question? Commented Feb 5, 2020 at 4:35
  • I thought it's too silly @GauravMantri Commented Feb 5, 2020 at 4:43
  • No question is too silly my friend :). Commented Feb 5, 2020 at 4:52

1 Answer 1

5

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:

  1. Create an append blob instead of a block blob if you wish to use append blobs. You can use create_blob to create an empty append blob and then append the contents using append_blob_from_text method.
  2. If you wish to use a block blob, then for appending contents, first you will need to download the blob's contents using get_blob_to_text method, append the new content and then reupload the blob using create_blob_from_text method.
Sign up to request clarification or add additional context in comments.

1 Comment

I used the first solution and it works out finally! Thank you very much. Just wondering if it is possible to specify the append in a newline rather than {abc}{def} to {abc}\n{def} from the method itself.

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.