【问题标题】:Upload image to azure blob storage using python使用 python 将图像上传到 azure blob 存储
【发布时间】:2020-01-09 00:38:58
【问题描述】:

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

images
    --0001.png
    --0002.jpg
    --0003.png

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

  1. 即使安装了azure-blob-storage,那个包里也没有BlobService这样的东西。
  2. 是否有明确记录如何执行此操作的地方?

【问题讨论】:

    标签: python azure azure-storage azure-blob-storage


    【解决方案1】:

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

    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上的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 运行我的示例代码。

    【讨论】:

    • 问题是即使安装了azure-blob-storage,这个包里也没有BlockBlobService
    • @mlRocks 请初始化 Python 虚拟环境并安装 azure-storage 包。我已经更新了我的帖子。
    【解决方案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)
    

    【讨论】:

      【解决方案3】:

      目前有两个版本的 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 )
      

      【讨论】:

        【解决方案4】:

        显然,create_blob_from_bytes 和 BlockBlobService 都不存在于您默认获得的最新 python SDK[1] 中,除非您正在管理各种包的版本。

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

        以下代码在 Azure Functions 中不起作用。

            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://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobserviceclient?view=azure-python

        [2]https://docs.microsoft.com/en-us/python/api/azure-identity/azure.identity.azureclicredential?view=azure-python

        【讨论】:

          【解决方案5】:
          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)}')
          

          【讨论】:

            猜你喜欢
            • 2018-12-17
            • 1970-01-01
            • 2014-07-22
            • 2020-02-23
            • 2020-01-25
            • 2021-01-16
            • 2013-04-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多