from azure.storage.blob import BlobClient, BlobServiceClient
from azure.storage.blob import ResourceTypes, AccountSasPermissions
from azure.storage.blob import generate_account_sas
from datetime import datetime, timedelta
#Credentials for Source Container Blob
dest_connection_string = '<connectionString>'
dest_account_key = "<storageKey>"
src_container_name = 'con1' # Name of container which has blob to be copied
src_blob_name = "demo1/folder1/file.txt"
src_acct_name = "demo1"
#Credentials for Destination Container Blob
dest_container_name = 'demo/file.txt' # Name of container where blob will be copied
dest_connection_string = '<connectionString>'
dest_account_key = "<storageKey>"
dest_container = "con1"
dest_blob_name = "demo2/file.txt"
dest_account_name = "demo2"
# Create Src client
src_client = BlobServiceClient.from_connection_string(src_connection_string)
# Create Dest client
dest_client = BlobServiceClient.from_connection_string(dest_connection_string)
# Create sas token for Src blob
src_sas_token = generate_account_sas(
account_name = src_client.account_name,
account_key = src_account_key,
resource_types = ResourceTypes(object=True, container=True),
permission= AccountSasPermissions(read=True,list=True),
start = datetime.now(),
expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
)
# Create sas token for Dest blob
dest_sas_token = generate_account_sas(
account_name = dest_client.account_name,
account_key = dest_account_key,
resource_types = ResourceTypes(object=True, container=True),
permission= AccountSasPermissions(read=True,list=True),
start = datetime.now(),
expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
)
# Create blob client for source blob
src_source_blob = BlobClient(
src_client.url,
container_name = src_container_name,
blob_name = src_blob_name,
credential = src_sas_token
)
# Create blob client for Dest blob
dest_source_blob = BlobClient(
dest_client.url,
container_name = dest_container_name,
blob_name = dest_blob_name,
credential = dest_sas_token
)
# Start copy from Src to Destination.
try:
copy = dest_source_blob.start_copy_from_url(src_source_blob.url)
# Get the copy properties and print status
props = dest_source_blob.get_blob_properties()
print(props.copy.status)
except Exception as e:
print("Failed to copy blob. Error:" + str(e))
I have tried the above solution, but getting the Unauthorized error. Could not understand the issue.
Failed to copy blob. Error:This request is not authorized to perform this operation using this permission.
RequestId:8a33f614-601e-011b-7df6-51dc91000000
Time:2023-03-08T19:43:17.3158296Z
ErrorCode:AuthorizationPermissionMismatch
Content: <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthorizationPermissionMismatch</Code><Message>This request is not authorized to perform this operation using this permission.
RequestId:8a33f614-601e-011b-7df6-51dc91000000
Time:2023-03-08T19:43:17.3158296Z</Message></Error>