0

I have tried copying azure blob files from one account to another with the following python script

...
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)

but I have two sas URLs with a blob name, I need to copy with source and destination sas URLs How can I do it?

ganesh tirumani
  • 35
  • 1
  • 10

1 Answers1

0

Please find the below code for copying the blob files from one storage account to another storage account in Azure using Python, if helpful:

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)

For more in detail, please refer below links:

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.

Rukmini
  • 6,015
  • 2
  • 4
  • 14
  • Thanks, @RukminiMr-MT for responding but I am looking for the solution I only have two URLs --> source and destination sas, how can I copy with them? as you mention if there are sources account name,s and keys can be able to copy – ganesh tirumani Apr 22 '22 at 13:13
  • is it possible to get details of those values from sas URL? – ganesh tirumani Apr 22 '22 at 13:13
  • like as below `response_of_copying=requests.put(dest_file_url, headers={"x-ms-blob-type":"BlockBlob"}, data=soucre_file_url)` when having the two sas urls – ganesh tirumani Apr 24 '22 at 14:01