gpt4 book ai didi

python - 如何在 Python 中生成 Azure blob SAS URL?

转载 作者:行者123 更新时间:2023-12-02 05:50:58 25 4
gpt4 key购买 nike

我正在尝试使用 azure-storage-blob 包动态生成 blob SAS URL。 This solution仅当您拥有现已弃用的 azure-storage 软件包(无法再安装)时才有效。

我需要一种方法来模仿 BlockBlobService.generate_blob_shared_access_signature 的行为来生成 Blob SAS URL,如下所示:

from datetime import datetime, timedelta
from azure.storage.blob import (
BlockBlobService,
ContainerPermissions,
BlobPermissions,
PublicAccess,
)

AZURE_ACC_NAME = '<account_name>'
AZURE_PRIMARY_KEY = '<account_key>'
AZURE_CONTAINER = '<container_name>'
AZURE_BLOB='<blob_name>'

block_blob_service = BlockBlobService(account_name=AZURE_ACC_NAME, account_key=AZURE_PRIMARY_KEY)
sas_url = block_blob_service.generate_blob_shared_access_signature(AZURE_CONTAINER,AZURE_BLOB,permission=BlobPermissions.READ,expiry= datetime.utcnow() + timedelta(hours=1))
print('https://'+AZURE_ACC_NAME+'.blob.core.windows.net/'+AZURE_CONTAINER+'/'+AZURE_BLOB+'?'+sas_url)

如果您有已弃用的软件包,则上述解决方案有效,但我需要一个不需要它的解决方案。

最佳答案

看看下面的代码:

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

account_name = 'STORAGE_ACCOUNT_NAME'
account_key = 'STORAGE_ACCOUNT_ACCESS_KEY'
container_name = 'CONTAINER_NAME'
blob_name = 'IMAGE_PATH/IMAGE_NAME'

def get_blob_sas(account_name,account_key, container_name, blob_name):
sas_blob = generate_blob_sas(account_name=account_name,
container_name=container_name,
blob_name=blob_name,
account_key=account_key,
permission=BlobSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1))
return sas_blob

blob = get_blob_sas(account_name,account_key, container_name, blob_name)
url = 'https://'+account_name+'.blob.core.windows.net/'+container_name+'/'+blob_name+'?'+blob

查看此文档了解更多详细信息:link

关于python - 如何在 Python 中生成 Azure blob SAS URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62523166/

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