gpt4 book ai didi

python - Azure Blob 存储 : Download blob with SAS token

转载 作者:行者123 更新时间:2023-11-28 22:21:42 34 4
gpt4 key购买 nike

我正在尝试从 Blob 存储帐户中的容器下载文件。

在我的 python 脚本中,我首先创建一个 sas token ,然后尝试使用它来下载文件

from datetime import datetime, timedelta
from urllib.request import urlretrieve

from azure.storage.blob import (
BlockBlobService,
BlobPermissions
)
accountName ="myaccountName"
accountKey = "myaccountKey"
containerName = "mycontainerName"
blob_name = "test.txt"
local_path = "testlocal.txt"

service = BlockBlobService(account_name=accountName, account_key=accountKey)
permission = BlobPermissions(_str="racwd")
sas = service.generate_blob_shared_access_signature(containerName, 'test.txt', permission, datetime.utcnow() + timedelta(hours=1),)



sas_service = BlockBlobService(
account_name=accountName,
sas_token= sas
)


blob_uri = "https://myaccountName.blob.core.windows.net/" + containerName + "/" + blob_name +"?"+ sas

print (sas)
print(blob_uri)
urlretrieve(blob_uri, local_path)

运行会产生错误

HTTPError: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

有谁知道我如何创建一个 sas token ,让我可以下载我容器中的一个特定文件?

最佳答案

请引用下面的代码片段,它适用于我。

from datetime import datetime, timedelta

from azure.storage.blob import (
BlockBlobService,
ContainerPermissions,
)

accountName = "***"
accountKey = "***"
containerName = "***"

blobService = BlockBlobService(account_name=accountName, account_key=accountKey)
sas_token = blobService.generate_container_shared_access_signature(containerName,ContainerPermissions.READ, datetime.utcnow() + timedelta(hours=1))
print sas_token

blobService = BlockBlobService(account_name=accountName, account_key=None, sas_token=sas_token)
blobService.get_blob_to_path(containerName, "1.png", "E://testLocal.png")

希望对你有帮助。

关于python - Azure Blob 存储 : Download blob with SAS token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48139078/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com