Last active
January 20, 2023 07:07
-
-
Save lastcoolnameleft/cb01f2e27fbbcf4e2dd7759ceb604dd8 to your computer and use it in GitHub Desktop.
Generate Azure Blob SAS Token with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great piece of code, it's just that this code is deprecated now.
You only need to
pip install azure-storage-blob
and thenfrom azure.storage.blob import BlobServiceClient
asBlockBlobService
is no longer part of theazure.storage.blob
.Here's a link https://pypi.org/project/azure-storage-blob/
Scroll to Types of Credentials #2