Azure Blob 存储 Python SDK MAC 签名

Azure Blob Storage Python SDK MAC signature

我生成 sas 并且可以访问文件但是当我将 url 传递给 blob 客户端到 upload_file_url 它抛出异常

try:
#getting container client
blob_container_client = BlobServiceClient(account_url,account_key).get_container_client(container=container_name)

#generate sas tocket for block container
sas=(BlobSharedAccessSignature(account_name, account_key).\
    generate_container(container_name,permission='r',start='2021-03-01T00:00Z',expiry='2021-04-01T00:00Z'))

#generate final url with sas
final_source_url=(blob_container_client.url+'/'+sourcefile+'?'+sas)
print(final_source_url)

blob_client1=blob_container_client.get_blob_client(sourcefile)
source=blob_client1.download_blob().readall()

#---------ACCOUNT 2---------------------
# getting container client for second account
blob_client2 = BlobServiceClient(account_url2, account_key2).get_blob_client(container=container_name2,blob=destinationfile2)
blob_client2.upload_blob_from_url(final_source_url)

except 作为 ex 的异常: 打印('Exception:') 打印(ex)

Exception:
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:527a4df5-501e-003e-03d6-0f4d5b000000
Time:2021-03-03T02:41:38.6605824Z
ErrorCode:AuthenticationFailed
Error:None
AuthenticationErrorDetail:The MAC signature found in the HTTP request 'xxxxxxxxxxxxxxxxxxxxxxxxxxx=' is not the same as any computed signature. Server used following string to sign: 'PUT

x-ms-client-request-id:f9918e5a-7bc9-11eb-86ab-803049852acd
x-ms-copy-source:://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x-ms-date:Wed, 03 Mar 2021 02:41:37 GMT
x-ms-version:2020-06-12
/xxxxx/xxxxxxx/testt.txt'.

SAS 用于从 account1 读取,然后 account2 使用 sas 上传文件。 我想将文件从 account1 复制到 account2 .

这一行有例外: blob_client2.start_copy_from_url('final_source_url')

问题出在您的共享访问签名中的权限。您正在创建仅具有 read (r) 权限的共享访问签名(这对于读取 blob 来说很好)但是您正在尝试使用该共享访问签名执行写操作(复制操作是写操作)。

sas=(BlobSharedAccessSignature(account_name, account_key).\
    generate_container(container_name,permission='r',start='2021-03-01T00:00Z',expiry='2021-04-01T00:00Z'))

请更改您的签名以包含 write (w) 权限,您应该不会收到此错误。类似于以下内容:

sas=(BlobSharedAccessSignature(account_name, account_key).\
    generate_container(container_name,permission='rw',start='2021-03-01T00:00Z',expiry='2021-04-01T00:00Z'))