pythonazureazure-blob-storageblobstore

How to convert invalid blob type to a valid blob type?


I use the below code to copy the blob.

def _upload_small_block_blob(self, container_name, blob_name, new_block_blob_name):
        try:
            # Download the small blob using the SDK
            blob_service_client = BlobServiceClient.from_connection_string(self._conn_string)
            container_client = blob_service_client.get_container_client(container_name)
            old_blob_client = container_client.get_blob_client(blob_name)
            downloaded_data = old_blob_client.download_blob().readall()
            new_blob_client = container_client.get_blob_client(new_block_blob_name)
            if new_blob_client.exists():
                print(f"Blob '{new_block_blob_name}' already exists. Choose a different name.")
                return
            new_blob_client.upload_blob(downloaded_data, blob_type="BlockBlob")
            log.info(f"Uploaded new Block Blob '{blob_name}' successfully.")
        except Exception as e:
            log.error(f"Failed to upload new Block Blob '{blob_name}': {str(e)}")
    

This code works fine, when the blobtype is valid. I don't know how my blob got invalid type, no idea

  1. I tried manually downloading and tried to upload it, then also it shows

    Failed to upload 1 out of 1 blob(s): configuration-0.jsonl: The blob type is invalid for this operation.

  2. I used the above code to upload it as new blob, still no luck. It shows:

     2024-10-21 11:24:08 _ERROR_ [blob_storage_life_cycle.py:388]: Failed to upload new Block Blob '2024/10/01154538/bb-GOPACS-0.jsonl': The blob type is invalid for this operation.
    

    RequestId:458f9f48-401e-0062-179a-23d583000000 Time:2024-10-21T09:24:06.4130282Z ErrorCode:InvalidBlobType Content: InvalidBlobTypeThe blob type is invalid for this operation. RequestId:458f9f48-401e-0062-179a-23d583000000 Time:2024-10-21T09:24:06.4130282Z 2024-10-21 11:24:29 INFO [blob_storage_life_cycle.py:358]: Source Blob Type: BlobType.BLOCKBLOB


Solution

  • Failed to upload new Block Blob 'publicstatistics_16.jsonl': The blob type is invalid for this operation. RequestId:2139ea82-701e-000d-114a-24af5f000000 Time:2024-10-22T06:24:01.8073038Z ErrorCode:InvalidBlobType Content: InvalidBlobTypeThe blob type is invalid for this operation. RequestId:2139ea82-701e-000d-114a-24af5f000000.

    The above error may be occurred, when you are uploading the same blob name with different blob type.

    In my storage account, I stored configuration-0.jsonl file is in append blob type.

    enter image description here

    I also got the same error when I try with same blob name with different blob type to upload in storage account.

    Source Blob Type: BlobType.APPENDBLOB
    Failed to upload new Block Blob 'configuration-0.jsonl': The blob type is invalid for this operation.
    RequestId:260xxxx0000
    Time:2024-10-22T06:44:30.7815275Z
    ErrorCode:InvalidBlobType
    Content: <?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidBlobType</Code><Message>The blob type is invalid for this operation.     
    RequestId:260xxxx00000
    Time:2024-xxx0.7815275Z</Message></Error>
    

    I tried to upload the file with different name it executed successfully in my environment using below code.

    Code:

    from azure.storage.blob import BlobServiceClient, BlobClient ,BlobType
    
    def upload_small_block_blob(connection_string, container_name, blob_name, new_blob_name):
        try:
    
            blob_service_client = BlobServiceClient.from_connection_string(connection_string)
            container_client = blob_service_client.get_container_client(container_name)
            old_blob_client = container_client.get_blob_client(blob_name)
            blob_properties = old_blob_client.get_blob_properties()
            blob_type = blob_properties.blob_type
            print(f"Source Blob Type: {blob_type}")
    
            downloaded_data = old_blob_client.download_blob().readall()
            new_blob_client = container_client.get_blob_client(new_blob_name)
            new_blob_client.upload_blob(downloaded_data, blob_type=BlobType.BLOCKBLOB)
            print(f"Uploaded new Block Blob '{new_blob_name}' successfully.")
        except Exception as e:
            print(f"Failed to upload new Block Blob '{new_blob_name}': {str(e)}")
            
    # Example usage
    connection_string = "xxx"
    container_name = "venkat"
    blob_name = "publicstatistics_16.jsonl"  # Original append blob
    new_blob_name = "test.jsonl"  # New block blob
    
    upload_small_block_blob(connection_string, container_name, blob_name, new_blob_name)
    

    Output:

    Source Blob Type: BlobType.APPENDBLOB
    Uploaded new Block Blob 'test.jsonl' successfully.
    

    enter image description here