2015-12-26 52 views
2

我正嘗試使用Azure Python SDK爲Azure存儲中的容器創建有效的共享訪問簽名URL。我試圖生成它是立即生效,在30天后到期,並給予讀&整個容器(而不僅僅是BLOB)寫訪問。下面的代碼工作正常,並在最後打印最終的URL。我也在門戶中手動驗證容器和blob是否已成功創建。如何使用Azure Python SDK爲容器創建共享訪問簽名

然而,粘貼網址到瀏覽器後,我收到以下錯誤信息:

<?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>

看來這個問題必須與這行代碼:

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

以下是完整的代碼示例:

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) 

任何想法如何解決這一問題?謝謝!

回答

2

isoformat方法追加微秒到字符串,AFAICT這在ISO8601中是無效的。

如果您修改代碼:

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

生成的字符串有效。

例如,您收到:

2016-01-03T21:04:10.545430

的變化將其轉化爲有效:

2016-01 -03T21:04:10Z

相關問題