使用Python上传图像到Azure Blob存储

6
我有一个名为images的图片目录,其中包含以下图像文件:
images
    --0001.png
    --0002.jpg
    --0003.png

现在我想将这个目录以相同的文件结构上传到我的 Azure Blob 存储中。我查看了这里提供的示例代码 here here ,但是:
  1. 即使安装了azure-blob-storage,也没有BlobService这样的东西。
  2. 是否有任何清晰的文档说明如何操作?
6个回答

6

在你提供的文档中有写。

不是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)

查看BlobClient.from_connection_string文档。

4

这是我的示例代码,对我来说运行良好。

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

有关 Python Azure 存储 SDK 的 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/中找到它。
  1. $ virtualenv test
  2. $ cd test
  3. $ Scripts\active
  4. $ pip install azure-storage

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


3
问题在于,即使安装了 azure-blob-storage,该包中也没有 BlockBlobService - enterML
1
@mlRocks 请初始化Python虚拟环境并安装azure-storage包。我已经更新了我的帖子。 - Peter Pan

1

目前有两个版本的azure.storage.blob。如果您创建一个Azure VM并在其中处理数据,则可能会得到其中之一。

较旧的版本需要(如Adam Marczak所指出的):

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)

当新版本:

from azure.storage.blob import BlockBlobService

blob_service = BlockBlobService(account_name, account_key)

blob_service.create_blob_from_path(
    container_name, blob_name ,  full_file_path )

0
def upload_file(remote_path,local_path):
    try:
        blobService = BlockBlobService(account_name=SETTINGS.AZURE_ACCOUNT_NAME, account_key=SETTINGS.AZURE_ACCOUNT_KEY)
        blobService.create_blob_from_path('data',remote_path,local_path)
    except Exception as e:
        logger.error(f'Unable to save azure blob data. {str(e)}')
        raise Exception(f'Unable to save azure blob data. {str(e)}')

0
from azure.storage.blob import BlobClient,ContentSettings

blob = BlobClient.from_connection_string(conn_str=connection_string, container_name=container_name, blob_name="my_blob9")
image_content_setting = ContentSettings(content_type='image/jpeg')

with open("/content/sample_data/kanha.png","rb") as data:
    blob.upload_blob(data,overwrite=True,content_settings=image_content_setting)

如果您想上传PNG / JPEG / 原始文件,则可以使用此代码进行上传,否则对于二进制文件,您可以检查https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/storage/azure-storage-blob#create-a-container - Kalpataru sahoo

0

显然,在最新的Python SDK中,create_blob_from_bytes和BlockBlobService都不存在[1],除非您正在管理各种软件包的版本,否则默认情况下会获取它们。

我假设您可能想将其作为独立脚本执行。因此,您可以使用AzureCliCredential[2]进行身份验证,并通过SDK提供的方法检索必要的资源端点。

以下代码在Azure函数中无法工作。

    from azure.storage.blob import (
       BlobServiceClient,
       ContentSettings
    )

    storage_connection_string='DefaultEndpointsProtocol=https;AccountName=<STORAGE_ACCOUNT_NAME>;AccountKey=<ACCOUNT_KEY>;EndpointSuffix=core.windows.net'
    container_name =
    blob_service_client = BlobServiceClient.(
        conn_str=storage_connection_string
        )
    logging.debug(f'getting client for container : {container_name}')
    container_client = 
    blob_service_client.get_container_client(container=container_name)
    blob_client = container_client.get_blob_client(blob_name)
    if blob_client.exists():
        blob_client.delete_blob()
    blob_client =blob_service_client.get_blob_client(container=container_name, 
    blob=blob_name)
    try:
        with open(filename, "rb") as data:
             blob.upload(data)
        content_settings =ContentSettings(content_type='image/png')
        logging.debug(f'setting the content type : {content_settings}')
    except Exception as e:
        logging.error(str(e))

[1] https://learn.microsoft.com/zh-cn/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python

[2] https://learn.microsoft.com/zh-cn/python/api/azure-identity/azure.identity.azureclicredential?view=azure-python


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接