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

1

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.

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.

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật