如何使用python sdk将blob上传至带子目录的azure存储容器?

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

我一直在关注这篇文章。Quickstart: 用Python v12 SDK管理blob 和文件 ContainerClient.upload_blob

这是上传一个blob的片段,用这个目录结构。testcontainer / backup / HelloWorld.cab

bsc = BlobServiceClient.from_connection_string('<connection-string>')
cc = bsc.get_container_client('testcontainer')
cc.upload_blob(name='testcontainer/backup/HelloWorld.cab', data=open(r"\\network\path\to\backup\HelloWorld.cab", 'rb').read())

但我得到了以下错误。有什么想法,我做错了什么?

azure.storage.blob._generated.models._models_py3.StorageErrorException: Operation returned an invalid status 'The specifed resource name contains invalid characters.'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python3\lib\site-packages\azure\core\tracing\decorator.py", line 83, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "C:\Python3\lib\site-packages\azure\storage\blob\_container_client.py", line 836, in upload_blob
    blob.upload_blob(
  File "C:\Python3\lib\site-packages\azure\core\tracing\decorator.py", line 83, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "C:\Python3\lib\site-packages\azure\storage\blob\_blob_client.py", line 496, in upload_blob
    return upload_block_blob(**options)
  File "C:\Python3\lib\site-packages\azure\storage\blob\_upload_helpers.py", line 153, in upload_block_blob
    process_storage_error(error)
  File "C:\Python3\lib\site-packages\azure\storage\blob\_shared\response_handlers.py", line 147, in process_storage_error
    raise error
azure.core.exceptions.HttpResponseError: The specifed resource name contains invalid characters.
RequestId:71cad76d-801e-0097-8068-1fc9e0000000
Time:2020-05-01T03:28:33.5320153Z
ErrorCode:InvalidResourceName
Error:None

注意:我还看到了这个 回答 对于这个问题。Microsoft Azure: 如何在blob容器中创建子目录?

python azure-storage-blobs
1个回答
1
投票

如果我使用无效的资源名,我能够重现这个问题 (这就是错误信息所告诉你的)。

例如,如果我使用 testcontainer 作为我的blob容器名称(这是正确的),我能够上传blob。

但是如果我使用 testContainer 作为我的blob容器名称(无效,注意 "C "的大写字母),我得到了和你一样的错误。

请检查blob容器和blob的名称。关于blob资源的命名约定,请参考这个链接。https:/docs.microsoft.comen-usrestapistorageservicesnaming and-referencing-containers--blobs--and-metadata..

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