使用python将映像上载到azure blob存储

2024-10-06 12:34:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为images的图像目录,其中包含以下图像文件:

imgages
    --0001.png
    --0002.jpg
    --0003.png

现在我想用相同的文件结构将这个目录上传到我的azureblob存储。我查看了给定的herehere的示例代码,但是:

  1. 即使安装了azure-blob-storage,该包中也没有{}这样的东西。在
  2. 有没有地方明确记录了如何做到这一点?在

Tags: 文件代码图像目录示例herepng图像文件
2条回答

在你链接的文件里。在

不是BlobService而是BlobClient。在

from azure.storage.blob import BlobClient

blob = BlobClient.from_connection_string("my_connection_string", container="mycontainer", blob="my_blob")

with open("./SampleSource.txt", "rb") as data:
    blob.upload_blob(data)

这是我的示例代码,对我来说很好。在

import os
from azure.storage.blob import BlockBlobService

root_path = '<your root path>'
dir_name = 'images'
path = f"{root_path}/{dir_name}"
file_names = os.listdir(path)

account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name, such as `test` for me>'

block_blob_service = BlockBlobService(
    account_name=account_name,
    account_key=account_key
)

for file_name in file_names:
    blob_name = f"{dir_name}/{file_name}"
    file_path = f"{path}/{file_name}"
    block_blob_service.create_blob_from_path(container_name, blob_name, file_path)

如下图所示的结果是来自Azure Storage Explorer的屏幕截图。在

enter image description here

有关Azure Storage SDK For Python的API引用的详细信息,请参阅https://azure-storage.readthedocs.io/index.html。在


更新:我使用的Python版本是Windows上的python3.7.4,所需的包是azure-storage==0.36.0,您可以从https://pypi.org/project/azure-storage/找到它。在

  1. $ virtualenv test
  2. $ cd test
  3. $ Scripts\active
  4. $ pip install azure-storage

然后,您可以在当前Python虚拟环境中通过python upload_images.py运行我的示例代码。在

相关问题 更多 >