skip to Main Content

I have a blob with data like this

2324
2321
2132

How do I add a new value in this blob? So if I add ‘2200’, it becomes

2324
2321
2132
2200

I have tried append.block() but that gives the error

Exception: ResourceExistsError: The blob type is invalid for this operation.
RequestId:16a8f0f9-001e-
Time:2023-02-24T05:05:16.1581160Z
ErrorCode:InvalidBlobType
    blob_client = container_client.get_blob_client("LIST.txt")
    blob_client.append_block('5231n')
    stuff = blob_client.download_blob().readall()
    ans = stuff.decode('utf-8')
    ans_list = ans.split('rn')
    # print(ans_list)
    for an in ans_list:
        if an == '5231':
            print("Num Exists")

2

Answers


  1. There are 3 ways by which you can add data to an existing blob:

    1. Use append blobs: Append blobs are special kind of blobs where you can only append data to it. If your use case is to always add data to a blob, then you can use it instead of a block blob. However for this you would need to delete your existing blob and recreate it as append blob. You can read more about append blobs here: https://learn.microsoft.com/en-us/rest/api/storageservices/understanding-block-blobs–append-blobs–and-page-blobs#about-append-blobs.
    2. Download and upload: Considering your blob is a block blob, one way of adding data to that blob is to download the blob, update its contents with the modified data and then upload it again. This approach may work for smaller blobs but may not be an efficient approach for large blobs.
    3. Use block blob specific features: Another way of adding data to a block blob is to make use a block blob specific features. Essentially a block blob consists of blocks. What you have to do is download the block list of an existing blob, upload the new data as a new block and then commit the new block list. You can read more about it here: Put Block, Put Block List and Get Block List.
    Login or Signup to reply.
  2. Adding new text data to an existing blob in Azure

    I have followed Document1 and SO-thread:

    I have taken a file as below:

    2324
    2321
    2132

    enter image description here

    Then Uploaded it to storage account:

    enter image description here

    Then executed the below code :

    from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
    constring = "DefaultEndpointsProtocol=pointSuffix=core.windows.net"
    conname = "name of the conatiner"
    blobname = "list.txt"
    
    blob_service_client = BlobServiceClient.from_connection_string(constring)
    
    Containerclient = blob_service_client.get_container_client(conname )
    
    Blob_client = Containerclient.get_blob_client(blobname )
    
    existingtext = Blob_client.download_blob().content_as_text()
    
    addtext = "n2200"
    newtext = existingtext + addtext
    
    Blob_client.upload_blob(newtext, overwrite=True)
    

    enter image description here

    Output:

    2200 got added as below:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search