Pythongenerate_blob_sas生成非工作SAS

问题描述 投票:0回答:1

我正在尝试在 python 中生成 blob SAS,但我不断收到以下错误:

Signature did not match. String to sign used was r...

我的函数如下所示:

    start_time = datetime.now(timezone.utc)
    expiry_time = start_time + timedelta(days=1)

    sas_token = generate_blob_sas(
        account_name=account_name,
        container_name=container_name,
        blob_name=filename,
        account_key=account_key,
        permission=BlobSasPermissions(read=True),
        expiry=expiry_time,
        start=start_time
    )

我生成的 URL 如下所示:

https://{{storage account name}}.blob.core.windows.net/{{container name}}/{{blob name}}?st=2024-04-25T15%3A04%3A23Z&se=2024-05-02T15%3A04%3A23Z&sp=r&sv=2023-11-03&sr=b&sig={{signature}}
python python-3.x azure-blob-storage
1个回答
0
投票

当 SAS 令牌中的签名与预期签名不匹配时,会出现上述错误。

以下是使用 Azure Python SDK 生成 blob SAS 的代码。

代码:

from datetime import datetime, timedelta, timezone
from azure.storage.blob import generate_blob_sas, BlobSasPermissions

# Set the storage account details
storage_account_name = "venkat123"
storage_account_key = "accountkey"
storage_container_name = "test"
blob_name = "<Your-blob-name>"

# Set the SAS token parameters
sas_expiry = datetime.now(timezone.utc) + timedelta(hours=1)
sas_permissions = BlobSasPermissions(read=True, write=True)

# Generate SAS token for the blob
blob_sas_token = generate_blob_sas(
    account_name=storage_account_name,
    container_name=storage_container_name,
    blob_name=blob_name,
    account_key=storage_account_key,
    permission=sas_permissions,
    expiry=sas_expiry
)

# Construct the full URL with the SAS token
blob_url_with_sas = f"https://{storage_account_name}.blob.core.windows.net/{storage_container_name}/{blob_name}?{blob_sas_token}"

print("Blob URL with SAS token:")
print(blob_url_with_sas)

输出:

Blob URL with SAS token:
https://venkat123.blob.core.windows.net/test/example.png?se=2024-04-26T04%3A12%3A16Z&sp=rw&sv=2023-11-03&sr=b&sig=bfRpgp%2BXtGcsJZ3xxxxx

enter image description here

浏览器: enter image description here

参考: azure.storage.blob 包 |微软学习

© www.soinside.com 2019 - 2024. All rights reserved.