My application needs access to the blobs inside my storage and sinc I dont want to ditribute any credentials I'm develpoing a backend endpoint to supply limited SAS token to the client app after authentication.
I thought on generating a blob SAS token (thus following the principle of least privilege and even though I think it worked in the past and I managed to retrieve the blob, it stopped working and I'm getting the below error. Generating a container SAS token and using it on the sme blob - works.
I also tried to generate a blob token from Azure Portal and use it in the same way and that actually worked as expected
Any idea what is wrong with what I'm doing?
this is the code I'm using to generate the blob token (I'm using the Python SDK):
start_time = datetime.datetime.now(datetime.timezone.utc)
expiry_time = start_time + datetime.timedelta(seconds=int(900))
sas_token = generate_blob_sas(
account_name=account_name,
container_name=container_name,
blob_name=blob_name,
account_key=account_key,
permission=BlobSasPermissions(read=True),
expiry=expiry_time,
start=start_time
)
using this generated token with a blob URL from the container, gives the following error (HTTP status code 403) when trying to retrieve the blob (GET request):
When I change the code to generate a container token, by using the following code:
start_time = datetime.datetime.now(datetime.timezone.utc)
expiry_time = start_time + datetime.timedelta(seconds=int(900))
sas_token = generate_container_sas(
account_name=account_name,
container_name=container_name,
account_key=account_key,
permission=BlobSasPermissions(read=True),
expiry=expiry_time,
start=start_time
)
Everything works, and I manage to retirieve the blob.
Both generate_blob_sas and generate_container_sas comes from azure.storage.blob Python SDK (currently using version 12.19.0)
The error occurs when you pass with wrong parameters in the
generate_blob_sas
function.I used the below code to generate the
SAS
token for the blob it created successfully using Python with the same packageazure-storage-blob==12.19.0
.In my environment, I have stored an image with the blob name
spring-flowers.jpg
in the Azure blob storage.Portal:
Code:
Output:
When I copied the same blob sas URL and pasted it into the browser it worked perfectly.
Portal:
Reference:
azure.storage.blob package | Microsoft Learn