简体   繁体   English

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

[英]How to upload a blob into azure storage container with sub directories using the python sdk?

I was following along this article: Quickstart: Manage blobs with Python v12 SDK and the documentation for ContainerClient.upload_blob我一直在关注这篇文章: 快速入门:使用 Python v12 SDKContainerClient.upload_blob的文档管理 blob

Here's the snippet to upload a blob with this directory structure: testcontainer / backup / HelloWorld.cab这是上传具有此目录结构的 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())

But I get the following error.但我收到以下错误。 Any ideas on what I'm doing wrong?关于我做错了什么的任何想法?

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

Note: I also saw this answer to this question: Microsoft Azure: How to create sub directory in a blob container注意:我也看到了这个问题的答案Microsoft Azure: How to create sub directory in a blob container

I am able to reproduce this issue if I use invalid resource name (which is what the error message is telling you).如果我使用无效的资源名称(这是错误消息告诉您的内容),我能够重现此问题。

For example, if I use testcontainer as my blob container name (which is correct), I am able to upload the blob.例如,如果我使用testcontainer作为我的 blob 容器名称(这是正确的),我可以上传 blob。

However if I use testContainer as my blob container name (which is invalid, notice the uppercase "C"), I get the same error as you're getting.但是,如果我使用testContainer作为我的 blob 容器名称(这是无效的,请注意大写的“C”),我会得到与你得到的相同的错误。

Please check the name of the blob container and the blob.请检查 blob 容器的名称和 blob。 Please see this link for naming convention for blob resources: https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata .请参阅此链接了解 blob 资源的命名约定: https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Azure Blob 存储 SDK 将 Blob 从一个容器复制到另一个容器 - How to copy a blob from one container to another container using Azure Blob storage SDK 使用适用于 Python 的 Azure 存储 SDK 将多个文件从文件夹上传到 Azure Blob 存储 - Upload multiple files from folder to Azure Blob storage using Azure Storage SDK for Python 如何在 Python 中使用 Azure Functions 的 Azure Blob 存储绑定将 JSON 数据上传到 Azure 存储 blob - How to upload JSON data to Azure storage blob using Azure Blob storage bindings for Azure Functions in Python 使用 python 将图像上传到 azure blob 存储 - Upload image to azure blob storage using python Azure:使用容器创建存储帐户并将 Blob 上传到 Python 中 - Azure: create storage account with container and upload blob to it in Python 使用带有 Python V12 SDK 的 BlobServiceClient 将本地文件夹上传到 Azure Blob 存储 - Upload local folder to Azure Blob Storage using BlobServiceClient with Python V12 SDK 使用Python获取Azure Blob存储中的容器大小 - Get container sizes in Azure Blob Storage using Python 使用 python 创建 csv 文件并将其上传到 azure blob 存储 - Create and upload csv file to azure blob storage using python 如何使用 Python 在 Azure 中创建 Blob 容器? - How to create a blob container in Azure using Python? 使用 Azure-Storage-Blob Python 读取 Blob 容器目录中每个 Blob 的文件大小 - Reading the File size for each blob inside a directory of a Blob Container using Azure-Storage-Blob Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM