Skip to content

Instantly share code, notes, and snippets.

@lastcoolnameleft
Last active January 20, 2023 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lastcoolnameleft/cb01f2e27fbbcf4e2dd7759ceb604dd8 to your computer and use it in GitHub Desktop.
Save lastcoolnameleft/cb01f2e27fbbcf4e2dd7759ceb604dd8 to your computer and use it in GitHub Desktop.
Generate Azure Blob SAS Token with Python
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)
@hussamalmuayad-8451
Copy link

Great piece of code, it's just that this code is deprecated now.
You only need to pip install azure-storage-blob and then from azure.storage.blob import BlobServiceClient as BlockBlobService is no longer part of the azure.storage.blob.

Here's a link https://pypi.org/project/azure-storage-blob/
Scroll to Types of Credentials #2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment