Python:如何将Azure Blob从一个容器移动或复制到另一个容器

Sho*_*jaz 7 python azure azure-storage-blobs

我在项目中使用Microsoft Azure SDK for Python.我想将Blob从一个容器移动或复制到另一个容器.例如

https://demostorage.blob.core.windows.net/image-container/pretty.jpg
Run Code Online (Sandbox Code Playgroud)

我想把这个blob移动到

https://demostorage.blob.core.windows.net/demo-container/
Run Code Online (Sandbox Code Playgroud)

我在python SDK中找到了以下方法但无法理解它.

def copy_blob(self, container_name, blob_name,...):
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?谢谢

Sho*_*jaz 14

我这样做了.

from azure.storage.blob import BlobService

def copy_azure_files(self):

        blob_service = BlobService(account_name='account_name', account_key='account_key')
        blob_name = 'pretty.jpg'
        copy_from_container = 'image-container'
        copy_to_container = 'demo-container'

        blob_url = blob_service.make_blob_url(copy_from_container, blob_name)
        # blob_url:https://demostorage.blob.core.windows.net/image-container/pretty.jpg

        blob_service.copy_blob(copy_to_container, blob_name, blob_url)

        #for move the file use this line
        blob_service.delete_blob(copy_from_container, blob_name)
Run Code Online (Sandbox Code Playgroud)

我还没有找到任何Blob Move方法.所以我使用了copy方法然后执行Blob函数.

这是我的解决方案.如果您有更好的方法来处理这一切,请与我分享.

注意:我没有使用任何自定义方法,所有这些方法都包含在SDK中.

  • Azure存储中本机不支持移动.您可以通过执行复制操作完成移动操作,等待复制完成(非常重要,因为复制是异步操作),然后在源blob上执行删除操作. (2认同)
  • 认为你的意思是写'blob_url = blob_service.make_blob_url(copy_from_container,blob_name)`&`blob_service.delete_blob(copy_from_container,blob_name)` (2认同)
  • 初始导入(“from azure.storage.blob import BlobService”)是否应该更改为“from azure.storage.blob.baseblobservice import BaseBlobService”?我在[Azure SDK for python](https://azure-storage.readthedocs.io/ref/azure.storage.blob.baseblobservice.html)中找不到“BlobService”对象的任何引用,所以我假设他们将其重构为`BaseBlobService`... (2认同)
  • 我认为你需要导入这个`from azure.storage.blob import BlockBlobService` (2认同)

ope*_*onk 5

使用最新的Azure Blob 存储 SDK

from azure.storage.blob import BlockBlobService

account_name = "demostorage"
account_key = "lkjASDRwelriJfou3lkjksdfjLj349u9LJfasdjfs/dlkjfjLKSjdfi8ulksjdfAlkjsdfkL762FDSDFSDAfju==" 

source_container_name = "image-container"
source_file_path = "pretty.jpg"

target_container_name = "demo-container"
target_file_path = "pretty_copy.jpg"

service = BlockBlobService(account_name, account_key)

service.copy_blob(
    target_container_name, 
    target_file_path, 
    f"https://{account_name}.blob.core.windows.net/{source_container_name}/{source_file_path}",
)
Run Code Online (Sandbox Code Playgroud)


小智 5

使用当前版本的 azure-storage-blob(目前为 v12.3.2),您将收到 ImportError:

cannot import name 'BlockBlobService' from 'azure.storage.blob'
Run Code Online (Sandbox Code Playgroud)

此代码在我的情况下有效:

from azure.storage.blob import BlobServiceClient

# Azure
# Get this from Settings/Access keys in your Storage account on Azure portal
account_name = "YOUR_AZURE_ACCOUNT_NAME"
connection_string = "YOUR_AZURE_CONNECTION_STRING"

# Source
source_container_name = "sourcecontainer"
source_file_path = "soure.jpg"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
source_blob = (f"https://{account_name}.blob.core.windows.net/{source_container_name}/{source_file_path}")

# Target
target_container_name = "targetcontainer"
target_file_path = "target.jpg"
copied_blob = blob_service_client.get_blob_client(target_container_name, target_file_path)
copied_blob.start_copy_from_url(source_blob)

# If you would like to delete the source file
remove_blob = blob_service_client.get_blob_client(source_container_name, source_file_path)
remove_blob.delete_blob()
Run Code Online (Sandbox Code Playgroud)