2017-07-06 70 views
0

我使用Web界面在Azure存儲帳戶上創建了共享訪問簽名(SAS)令牌。令牌看起來像Azure Blob Store SAS令牌缺失服務資源字段

?sv=xxxx-xx-xx&ss=b&srt=sco&sp=rl&se=xxxx-xx-xxTxx:xx:xxZ&st=xxxx-xx-xxTxx:xx:xxZ&spr=https&sig=xxxxxxxxxxxxxxxxxxxxxx

的SAS令牌這裏缺少服務資源的sr領域。我必須手動將sr=b添加到查詢字符串才能使其工作。我必須做錯事,因爲這看起來非常挑剔。

from azure.storage.blob import BlockBlobService 
sas_token = "?sv=xxxx-xx-xx&ss=b&srt=sco&sp=rl&se=xxxx-xx-xxTxx:xx:xxZ&st=xxxx-xx-xxTxx:xx:xxZ&spr=https&sig=xxxxxxxxxxxxxxxxxxxxxx" 
sas_token = "?sr=b&" + sas_token[1:] 

serv = BlockBlobService(account_name='myaccount', sas_token=sas_token) 

for cont in serv.list_containers(): 
    print cont.name 

沒有sas_token = "?sr=b&" + sas_token[1:]我得到的錯誤:

sr is mandatory. Cannot be empty

而且如果sr=b字段不是第一次查詢,我得到一個驗證錯誤,如

Access without signed identifier cannot have time window more than 1 hour

+0

您使用的是什麼版本的Python SDK? –

+0

我使用python 2.7.6和azure-storage 0.34.3 –

回答

1

Access without signed identifier cannot have time window more than 1 hour

根據此錯誤消息,您可能需要設置到期時間小於從現在起1小時。請參閱Windows Azure Shared Access Signature always gives: Forbidden 403


我把你的代碼Python v2.7.12@azure-storage-python v0.34.3(最新版本)。它在我的網站上運行良好。所以,我建議你升級到最新版本並再次嘗試。

enter image description here

UPDATE:

我跟蹤code of Azure Storage SDK for Python這裏是我的發現。該SDK是一個REST API warpper它假定SAS令牌是這樣的:

sv=2015-04-05&ss=bfqt&srt=sco&sp=rl&se=2015-09-20T08:49Z&sip=168.1.5.60-168.1.5.70&sig=a39%2BYozJhGp6miujGymjRpN8tsrQfLo9Z3i8IRyIpnQ%3d 

正如你所看到的,令牌不包括?。當它向SAS Azure存儲REST服務發出GET請求時,SDK將在SAS令牌之前附加?

enter image description here

這將導致該的簽名版本的密鑰被解析爲?sv,那麼它提出的問題。所以,爲了避免這種情況,我們應該從SAS令牌中刪除?

+0

上面的代碼確實有效。我的問題是,修改SAS令牌似乎是錯誤的方法。我應該可以使用未經修改的SAS令牌對Azure存儲帳戶進行身份驗證和查詢。 –

+1

只需從SAS令牌中刪除'?'。我已經用細節更新了我的答案。 –