Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ErrorCode: InvalidBlobType when appending to a blob

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.

like image 235
cna Avatar asked Sep 02 '25 06:09

cna


1 Answers

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.
like image 164
Gaurav Mantri Avatar answered Sep 04 '25 20:09

Gaurav Mantri