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

3lxsmp7m  于 2023-02-13  发布在  Python
关注(0)|答案(3)|浏览(122)

我正在尝试使用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)

如果你有一个过时的软件包,上面的解决方案可以工作,但是我需要一个不需要它的解决方案。

wwodge7n

wwodge7n1#

看一下下面的代码:

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

有关详细信息,请查看此文档:链接

ippsafx7

ippsafx72#

请尝试以下代码:

from azure.storage.blob.sharedaccesssignature import BlobSharedAccessSignature
from datetime import datetime, timedelta

AZURE_ACC_NAME = '<account_name>'
AZURE_PRIMARY_KEY = '<account_key>'
AZURE_CONTAINER = '<container_name>'
AZURE_BLOB='<blob_name>'
expiry= datetime.utcnow() + timedelta(hours=1)

blobSharedAccessSignature = BlobSharedAccessSignature(AZURE_ACC_NAME, AZURE_PRIMARY_KEY)

sasToken = blobSharedAccessSignature.generate_blob(AZURE_CONTAINER, AZURE_BLOB, expiry=expiry, permission="r")

print sasToken

您可以在此处了解有关使用新的Storage SDK生成SAS令牌的更多信息:https://azure-storage.readthedocs.io/ref/azure.storage.blob.sharedaccesssignature.html.

gxwragnw

gxwragnw3#

The accepted answerazure-storage-blob==1.5.0Python 3.8.13中不适用
我不得不使用generate_blob_shared_access_signature函数代替。Documentation

from azure.storage.blob import BlockBlobService, BlobPermissions
from datetime import datetime, timedelta

azure_account_name = 'STORAGE_ACCOUNT_NAME'
azure_account_key = 'STORAGE_ACCOUNT_ACCESS_KEY'

def get_sas_url(container_name, blob_name):
    block_blob_service = BlockBlobService(account_name=azure_account_name, account_key=azure_account_key)

    sas_token = block_blob_service.generate_blob_shared_access_signature(container_name=container_name,
                                                                         blob_name=blob_name,
                                                                         permission=BlobPermissions(read=True),
                                                                         expiry=datetime.utcnow() + timedelta(minutes=10))

    return "https://"+azure_account_name+".blob.core.windows.net/"+container_name+"/"+blob_name+"?"+sas_token

相关问题