如何将无效的 blob 类型转换为有效的 blob 类型?

问题描述 投票:0回答:1

我使用下面的代码来复制 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)}")
    

当 blobtype 有效时,此代码可以正常工作。 我不知道我的 blob 是如何获得无效类型的,不知道

  1. 我尝试手动下载并尝试上传它,然后也显示了

    无法上传 1 个 blob 中的 1 个: 配置-0.jsonl:blob 类型对此操作无效。

  2. 我使用上面的代码将其作为新的blob上传,但仍然没有成功。 它显示:

     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.
    

    请求ID:458f9f48-401e-0062-179a-23d583000000 时间:2024-10-21T09:24:06.4130282Z 错误代码:无效Blob类型 内容:

    InvalidBlobType
    此操作的 blob 类型无效。 请求 ID:458f9f48-401e-0062-179a-23d583000000 时间:2024-10-21T09:24:06.4130282Z 2024-10-21 11:24:29 INFO [blob_storage_life_cycle.py:358]:源 Blob 类型:BlobType.BLOCKBLOB

python azure azure-blob-storage blobstore
1个回答
0
投票

无法上传新的块 Blob“publicstatistics_16.jsonl”:Blob 类型对此操作无效。 请求ID:2139ea82-701e-000d-114a-24af5f000000 时间:2024-10-22T06:24:01.8073038Z 错误代码:无效Blob类型 内容:

InvalidBlobType
此操作的 blob 类型无效。 请求ID:2139ea82-701e-000d-114a-24af5f000000。

当您上传与

blob name
相同的
different blob type
时,可能会出现上述错误。

在我的存储帐户中,我存储了

configuration-0.jsonl
文件是附加 blob 类型。

enter image description here

当我尝试使用相同的 blob 名称和不同的 blob 类型上传到存储帐户时,我也遇到了相同的错误。

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>

我尝试上传具有不同名称的文件,它在我的环境中使用以下代码成功执行。

代码:

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)

输出:

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.