0

我正在尝试使用适用于 Azure 的 Python SDK 生成容器 SAS 令牌。使用下面的代码,我收到此错误:AuthenticationErrorDetail:Signature fields not well formed.

生成的令牌似乎时间格式不正确 - 这是一个问题吗?如果是,我该如何解决?看起来像这样:se=2021-03-08T17%3A49%3A35Z&sp=rwlacu&spr=https&sv=2020-04-08&sr=c&sig=QhAFl1435SDFGSFDguiPA8%3D

我很确定我应该将 sas_token 与容器 url 连接起来,我已经尝试过了,但仍然出现错误。我是否需要再连接,例如container_url + "?sp=r&st=2021-03-08T12:12:14Z&" + sas_token?我可以在门户中生成的 SAS 令牌至少看起来更像这样。

import datetime
from azure.storage.blob import ContainerClient, BlobServiceClient, generate_container_sas, \
    ResourceTypes, AccountSasPermissions

storage_conn_str = "myconstring fetched from the portal"

blob_service_client = BlobServiceClient.from_connection_string(storage_conn_str)

start = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + "Z" 
expiry = (datetime.datetime.utcnow() + datetime.timedelta(hours=3)).replace(microsecond=0).isoformat() + "Z"

sas_token = generate_container_sas(
    blob_service_client.account_name,
    container_name="containername",
    account_key=blob_service_client.credential.account_key,
    resource_types=ResourceTypes(container=True, object=True),
    permission=AccountSasPermissions(read=True, write=True, list=True, add=True, create=True,
                                     update=True),
    start=start,
    expiry=expiry,
    protocol="https"
)

print(sas_token)

cclient = ContainerClient.from_container_url(
    container_url="https://myblob.blob.core.windows.net/containername/",
    credential=sas_token)

print(cclient.get_account_information())
4

1 回答 1

2

我相信问题来了,因为你正在混合Service Shared Access SignatureAccount Shared Access Signature

请尝试以下代码:

sas_token = generate_account_sas(
    blob_service_client.account_name,
    account_key=blob_service_client.credential.account_key,
    resource_types=ResourceTypes(container=True, object=True),
    permission=AccountSasPermissions(read=True, write=True, list=True, add=True, create=True,
                                    update=True),
    start=start,
    expiry=expiry,
    protocol="https"
)

这将生成 Account SAS。我使用此代码读取容器属性,如下所示:

cclient = ContainerClient.from_container_url(
container_url="https://account.blob.core.windows.net/container/",
credential=sas_token)
print(cclient.get_account_information())

而且我没有收到任何错误。

请确保导入generate_account_sas

于 2021-03-08T15:37:55.400 回答