【问题标题】:How can I copy blob files from one account to another in azure如何在 azure 中将 blob 文件从一个帐户复制到另一个帐户
【发布时间】:2022-12-21 04:24:54
【问题描述】:

我尝试使用以下 python 脚本将 azure blob 文件从一个帐户复制到另一个帐户

...
source_block_blob_service = BlockBlobService(source_account_name= '"{}"'.format(from_account_name), source_account_key= '"{}"'.format(from_account_key))

target_block_blob_service = BlockBlobService(target_account_name= '"{}"'.format(to_account_name), target_account_key= '"{}"'.format(to_account_key))

blob_name = file_name
copy_from_container = source_loc
copy_to_container = destination
blob_url = source_block_blob_service.make_blob_url(copy_from_container, blob_name)
target_block_blob_service.copy_blob(copy_to_container, blob_name, blob_url)

但我有两个带有 blob 名称的 sas URL,我需要复制源和目标 sas URL 我该怎么做?

【问题讨论】:

    标签: python azure azure-blob-storage


    【解决方案1】:

    如果有帮助,请找到以下代码,使用 Python 将 blob 文件从一个存储帐户复制到 Azure 中的另一个存储帐户:

    from azure.storage.blob import ResourceTypes, AccountSasPermissions, generate_account_sas, BlobServiceClient
    from datetime import datetime, timedelta
    
    # Initialize your source and destination storage account name and key 
    source_account_key = ''
    dest_account_key = ''
    source_account_name = ''
    des_account_name = ''
    
    # genearte account sas token for source account
    sas_token = generate_account_sas(account_name=source_account_name, account_key=source_account_key,
                                     resource_types=ResourceTypes(
                                         service=True, container=True, object=True),
                                     permission=AccountSasPermissions(read=True),
                                     expiry=datetime.utcnow() + timedelta(hours=1))
                                     
    # Interact with the blobs on the account level                   
    source_blob_service_client = BlobServiceClient(
        account_url=f'https://{source_account_name}.blob.core.windows.net/', credential=source_account_key)
    des_blob_service_client = BlobServiceClient(
        account_url=f'https://{des_account_name}.blob.core.windows.net/', credential=dest_account_key)
    
    source_container_client = source_blob_service_client.get_container_client(
        'copy')
    
    source_blob = source_container_client.get_blob_client('Capture.PNG')
    source_url = source_blob.url+'?'+sas_token
    
    # copy
    des_blob_service_client.get_blob_client(
        'test', source_blob.blob_name).start_copy_from_url(source_url)
    

    欲知更多详情,请参考以下链接:

    https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/azure-storage-blob/samples/blob_samples_containers.py#L145

    move or copy files(blob) between storage accounts python (azure function)? - Stack Overflow

    【讨论】:

    • 谢谢@RukminiMr-MT 的回复,但我正在寻找解决方案我只有两个 URL --> 源和目标 sas,我如何复制它们?正如你提到的,如果有源帐户名,s 和密钥可以复制
    • 是否可以从 sas URL 获取这些值的详细信息?
    • 像下面的response_of_copying=requests.put(dest_file_url, headers={"x-ms-blob-type":"BlockBlob"}, data=soucre_file_url) 有两个 sas url
    【解决方案2】:

    您可以尝试一些不同的方式,利用适用于此类工作的平台服务:https://mohammedbrueckner.medium.com/copying-files-around-azure-storage-accounts-the-effective-way-d2ad7271f6af

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 2021-06-27
      • 1970-01-01
      • 2015-10-22
      • 2021-08-25
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多