2024-10-06 12:34:17 发布
网友
我有一个名为images的图像目录,其中包含以下图像文件:
images
imgages --0001.png --0002.jpg --0003.png
现在我想用相同的文件结构将这个目录上传到我的azureblob存储。我查看了给定的here和here的示例代码,但是:
azure-blob-storage
在你链接的文件里。在
不是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的屏幕截图。在
有关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/找到它。在
azure-storage==0.36.0
$ virtualenv test
$ cd test
$ Scripts\active
$ pip install azure-storage
然后,您可以在当前Python虚拟环境中通过python upload_images.py运行我的示例代码。在
python upload_images.py
在你链接的文件里。在
不是BlobService而是BlobClient。在
这是我的示例代码,对我来说很好。在
如下图所示的结果是来自Azure Storage Explorer的屏幕截图。在
有关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/找到它。在$ virtualenv test
$ cd test
$ Scripts\active
$ pip install azure-storage
然后,您可以在当前Python虚拟环境中通过
python upload_images.py
运行我的示例代码。在相关问题 更多 >
编程相关推荐