'How can I copy blob files from one account to another in azure
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?
Solution 1:[1]
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:
move or copy files(blob) between storage accounts python (azure function)? - Stack Overflow.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RukminiMr-MT |
