简体   繁体   English

如何使用Azure Python SDK为容器创建共享访问签名

[英]How to create a Shared Access Signature for a container with the Azure Python SDK

I am trying to create a valid Shared Access Signature URL for a container in Azure storage, using the Azure Python SDK. 我正在尝试使用Azure Python SDK为Azure存储中的容器创建有效的共享访问签名URL。 I'm trying to generate it to be effective immediately, to expire after 30 days, and to give read & write access to the whole container (not just the blob). 我试图让它立即生效,30天后过期,并给整个容器(不仅仅是blob)提供读写权限。 The below code works fine, and it prints the final URL at the end. 下面的代码工作正常,并在最后打印最终的URL。 I also manually verified in the portal that the container and blob were successfully created. 我还在门户中手动验证了容器和blob是否已成功创建。

However, after pasting the URL into a browser, I receive the following error message: 但是,将URL粘贴到浏览器后,我收到以下错误消息:

 <?xml version="1.0" encoding="UTF-8"?> -<Error> <Code>AuthenticationFailed</Code> <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:adecbe4e-0001-007c-0d19-40670c000000 Time:2015-12-26T20:10:45.9030215Z</Message> <AuthenticationErrorDetail>Signature fields not well formed.</AuthenticationErrorDetail> </Error> 

It seems the issue must be with this line of code: 似乎问题必须在这行代码中:

sasToken = blob_service.generate_shared_access_signature(containerName, None,SharedAccessPolicy(AccessPolicy(None, todayPlusMonthISO, "rw"), None))

Here is the full code sample: 以下是完整的代码示例:

from azure.storage.blob import BlobService
import datetime
from azure.storage import AccessPolicy, CloudStorageAccount, SharedAccessPolicy

containerName = "testcontainer"
blobName = "testblob.txt"
azureStorageAccountName = "" # Removed for publishing to StackOverflow
azureStorageAccountKey = "" # Removed for publishing to StackOverflow
blob_service = BlobService(account_name=azureStorageAccountName, account_key=azureStorageAccountKey)
blob_service.create_container(containerName)
blob_service.put_block_blob_from_text(containerName,blobName,"Hello World")
today = datetime.datetime.utcnow()
todayPlusMonth = today + datetime.timedelta(30)
todayPlusMonthISO = todayPlusMonth.isoformat()
sasToken = blob_service.generate_shared_access_signature(containerName, None,SharedAccessPolicy(AccessPolicy(None, todayPlusMonthISO, "rw"), None))
url = "https://" + azureStorageAccountName + ".blob.core.windows.net/" + containerName + "/" + blobName + "?" + sasToken
print(url)

Any ideas how to fix this? 任何想法如何解决这一问题? Thank you! 谢谢!

The isoformat method appends microseconds to the string, AFAICT this is not valid in ISO8601. isoformat方法将微秒附加到字符串,AFAICT在ISO8601中无效。

If you modify your code like this: 如果您修改代码如下:

todayPlusMonthISO = todayPlusMonth.replace(microsecond=0).isoformat() + 'Z'

The generated string becomes valid. 生成的字符串变为有效。

For example, before you had: 例如,在你有:

2016-01-03T21:04:10.545430 2016-01-03T21:04:10.545430

The change will transform it into the valid: 更改将其转换为有效:

2016-01-03T21:04:10Z 2016-01-03T21:04:10Z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM