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))
我已经尝试了上述解决方案,但得到未经授权的错误。无法理解这个问题。
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>
1条答案
按热度按时间oaxa6hgo1#
谢谢,我同意@大卫Makogon和@Gaurav Mantri的观点,即您需要写权限,可以按如下所示更改以下代码:
获取您的代码并进行更新:
更新代码部分:
这里您需要
AccountSasPermissions(read=True,list=True,Write=True,create=True)
写入和创建权限。**注意:**此处为创建权限,用于在没有文件时创建文件
通过改变这一点,我可以很容易地复制文件。