1

I have written a piece of code to copy a file from one container to another within the same storage account.

  block_blob_service = BlockBlobService(
  account_name='', account_key='')

  blob_name = file_name
  copy_from_container = source
  copy_to_container = destination
  blob_url = block_blob_service.make_blob_url(copy_from_container, blob_name)
  # blob_url:https://demostorage.blob.core.windows.net/image-container/pretty.jpg
  block_blob_service.copy_blob(copy_to_container, blob_name, blob_url)

But now I want to copy files across different storage accounts. How can I do that?

New_to_work
  • 253
  • 2
  • 5
  • 16

1 Answers1

0

Basically approach for copying a blob across storage accounts would remain more or less the same. You would need to do something like the following:

  source_block_blob_service = BlockBlobService(
  source_account_name= '', source_account_key= '')

  target_block_blob_service = BlockBlobService(
  target_account_name= '', target_account_key= '')

  blob_name = file_name
  copy_from_container = source
  copy_to_container = destination
  blob_url = source_block_blob_service.make_blob_url(copy_from_container, blob_name)
  # blob_url:https://demostorage.blob.core.windows.net/image-container/pretty.jpg
  target_block_blob_service.copy_blob(copy_to_container, blob_name, blob_url)

Please note that when copying blob across storage account, the source blob URL should be publicly accessible. You can do so by either creating a Shared Access Signature (SAS) URL with at least read permission on the source blob (recommended approach) or make the ACL of source blob container (copy_from_container) as Blob (not recommended).

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • I am getting The following error: "azure.common.AzureMissingResourceHttpError: The specified resource does not exist. ErrorCode: CannotVerifyCopySource CannotVerifyCopySourceThe specified resource does not exist." Although the storage account, container and files do exist. – New_to_work Aug 26 '19 at 07:56
  • Like I said in my answer, the source blob should be accessible. Please see the last paragraph in my answer. – Gaurav Mantri Aug 26 '19 at 08:11
  • no module name .make_blob_url, I think this doesn't work for newer sdk versions – mas Mar 26 '21 at 04:00
  • also ImportError: cannot import name 'BlockBlobService', – mas Mar 26 '21 at 04:25
  • @mas ... the answer I provided is for an older version of the SDK. The newer version (12.x) is drastically different than the older ones. Please refer to https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python for using the newer version. – Gaurav Mantri Mar 26 '21 at 04:44