【问题标题】:How to copy a blob from one container to another container using Azure Blob storage SDK如何使用 Azure Blob 存储 SDK 将 Blob 从一个容器复制到另一个容器
【发布时间】:2020-03-29 05:44:57
【问题描述】:

我一直在参考文档https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python。我找不到合适的 API 来将文件从一个容器复制/移动到另一个容器。假设我有两个容器 A 和 B。现在我想将 Blob 从 A 复制到 B。我该如何实现呢?一个例子将不胜感激。

图书馆详情:

azure-core==1.1.1
azure-storage-blob==12.0.0

注意:我已经使用了this thread,它仅在旧版本的 SDK 中受支持。

【问题讨论】:

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


    【解决方案1】:

    以下是 SDK 12.0.0 版本的完整示例:

    from azure.storage.blob import BlobClient, BlobServiceClient
    from azure.storage.blob import ResourceTypes, AccountSasPermissions
    from azure.storage.blob import generate_account_sas    
    
    connection_string = '' # The connection string for the source container
    account_key = '' # The account key for the source container
    source_container_name = '' # Name of container which has blob to be copied
    blob_name = '' # Name of the blob you want to copy
    destination_container_name = '' # Name of container where blob will be copied
    
    # Create client
    client = BlobServiceClient.from_connection_string(connection_string) 
    
    # Create sas token for blob
    sas_token = generate_account_sas(
        account_name = client.account_name,
        account_key = account_key 
        resource_types = ResourceTypes(object=True, container=True),
        permission= AccountSasPermission(read=True,list=True),
        start = datetime.now()
        expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
    )
    
    # Create blob client for source blob
    source_blob = BlobClient(
        client.url,
        container_name = source_container_name, 
        blob_name = blob_name,
        credential = sas_token
    )
    
    # Create new blob and start copy operation.
    new_blob = client.get_blob_client(destination_container_name, blob_name)    
    new_blob.start_copy_from_url(source_blob.url)
    

    有关如何获取容器的连接字符串和访问密钥的更多信息,请参阅here


    此答案假定两个容器都在同一个订阅中。

    【讨论】:

    • 非常感谢您的回答。它对我有用。但是,您能告诉我您从哪里知道类变量“url”吗?我关注了docs.microsoft.com/en-us/python/api/azure-storage-blob/…中的文档,但找不到。
    • @kanchan 欢迎您,很高兴您发现答案有用。我一定是通过检查对象找到了 url 属性。请参阅here:它需要存储帐户 URI。碰巧客户端具有该端点的属性,因此直接使用它很方便。
    • 是的,我可以通过转储对象来查看变量。我担心的是这个类变量没有在 API 参考中公开,如果我的代码在未来发生故障怎么办。只是好奇,最新SDK中旧SDK“make_blob_url”中存在的方法是否有任何替代方法?
    • @kanchan 我理解您的担忧,但如果您不更改 SDK 版本,那么它将来不会中断。无论如何,该 URI 指向存储源容器的存储帐户,例如 f'https://{mystorageaccount}.blob.core.windows.net/',因此您可以很容易地获取该字符串。
    【解决方案2】:

    你应该看看 SDK 中的start_copy_from_url 方法。

    来自同一链接:

    # Get the blob client with the source blob
    source_blob = "<source-blob-url>"
    copied_blob = blob_service_client.get_blob_client("<target-container>", '<blob-name>')
    
    # start copy and check copy status
    copy = copied_blob.start_copy_from_url(source_blob)
    props = copied_blob.get_blob_properties()
    print(props.copy.status)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 2015-07-16
      • 2020-10-20
      • 2020-12-06
      • 2020-03-26
      • 2017-09-10
      • 2020-01-27
      相关资源
      最近更新 更多