我正在更新一些使用 Microsoft 较旧的 azure-storage 模块的脚本,并切换 v12
SDK。
我在为 blob 生成 SAS token 时遇到问题。使用以下代码:
from datetime import datetime, timedelta
from azure.storage.blob import (
BlobServiceClient,
BlobSasPermissions,
generate_blob_sas,
)
client = BlobServiceClient(account_url=account_url, credential=account_key)
token = generate_blob_sas(
account_name=client.account_name,
account_key=client.credential.account_key,
container_name="tempcontainer",
blob_name="test.txt",
permissions=BlobSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
我收到的 token 如下所示:
se=2021-05-04T01%3A50%3A41Z&sv=2020-06-12&sr=b&sig=___________________________________________%3D
当我尝试使用下载资源时返回以下错误:
<Error>
<link type="text/css" rel="stylesheet" id="dark-mode-custom-link"/>
<link type="text/css" rel="stylesheet" id="dark-mode-general-link"/>
<style lang="en" type="text/css" id="dark-mode-custom-style"/>
<style lang="en" type="text/css" id="dark-mode-native-style"/>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:7c78e0c4-001e-010f-6b7f-40cd26000000 Time:2021-05-04T00:48:24.8329422Z
</Message>
<AuthenticationErrorDetail>sp is mandatory. Cannot be empty</AuthenticationErrorDetail>
</Error>
使用相同的帐户和凭据,我仍然能够使用旧版本成功生成 SAS token :
from azure.storage.blob import BlockBlobService, ContainerPermissions
token = self.client.generate_blob_shared_access_signature(
"tempcontainer",
"test.txt",
ContainerPermissions.READ,
datetime.now() + duration,
)
它会生成工作 token ,其中包含“sp”查询参数。
se=2021-06-03T16%3A57%3A59Z&sp=r&sv=2017-04-17&sr=b& sig=___________________________________________%3D
我还使用 Azure 存储资源管理器进行了测试,以验证我使用的帐户/ key 没有问题,并且我也能够通过该工具生成 SAS 链接。
这是 pip list
的输出以及我的 venv 中安装的所有 azure 模块:
azure-common 1.1.25
azure-core 1.13.0
azure-identity 1.3.1
azure-keyvault-secrets 4.1.0
azure-mgmt-core 1.2.2
azure-mgmt-keyvault 2.2.0
azure-storage-blob 12.8.1
我在使用新 SDK 生成这些 SAS token 时是否做错了什么?这是一个不同的 token ,还是我缺少一些额外的参数?也许有一种不同/更好的方法来使用这个新版本生成 SAS token ?
非常感谢您对此提供的任何帮助!
最佳答案
在使用 V12 SDK 的代码中,对于 generate_blob_sas
函数,参数名称应为 permission
而不是 permissions
这是我基于您共享的代码的测试代码:
from datetime import datetime, timedelta
from azure.storage.blob import (
BlobSasPermissions,
generate_blob_sas
)
token = generate_blob_sas(
account_name='<account name>',
account_key='<account key>',
container_name='<container name>',
blob_name='<blob name>',
permission=BlobSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
print(token)
结果:
关于python - generate_blob_sas 创建无效的 SAS token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67377579/