Nai*_*ain 6 python azure azure-storage azure-blob-storage
我有一个名为的图像目录images
,其中包含图像文件:
images
--0001.png
--0002.jpg
--0003.png
Run Code Online (Sandbox Code Playgroud)
现在我想将此目录上传到我的 azure blob 存储,并具有相同的文件结构。我查看了此处和此处给出的示例代码,但是:
azure-blob-storage
,该软件包中也没有这样的东西BlobService
。它位于您链接的文档中。
这不是BlobService
,这是BlobClient
阶级。
from azure.storage.blob import BlobClient
blob_client = BlobClient.from_connection_string(
conn_str='my_conn_str',
container_name='my_container_name',
blob_name='my_blob_name')
with open("./SampleSource.txt", "rb") as data:
blob.upload_blob(data)
Run Code Online (Sandbox Code Playgroud)
请参阅BlobClient.from_connection_string文档。
这是我的示例代码,对我来说效果很好。
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)
Run Code Online (Sandbox Code Playgroud)
如下图所示的结果是Azure Storage Explorer的屏幕截图。
有关 Azure Storage SDK for Python 的 API 参考的更多详细信息,请参阅https://azure-storage.readthedocs.io/index.html。
更新:我使用的Python版本是Windows上的Python 3.7.4,所需的包是azure-storage==0.36.0
,你可以从https://pypi.org/project/azure-storage/找到它。
$ virtualenv test
$ cd test
$ Scripts\active
$ pip install azure-storage
然后,您可以python upload_images.py
在当前的Python虚拟环境中运行我的示例代码。
归档时间: |
|
查看次数: |
21227 次 |
最近记录: |