question

MaheshwaramSrikaran-6790 avatar image
0 Votes"
MaheshwaramSrikaran-6790 asked MaheshwaramSrikaran-6790 commented

generate_blob_sas sdk sometimes doesnt work in python

I am using the following method from python SDK for azure-iot-hub.

sas_blob = generate_blob_sas(account_name= STORAGE_ACCOUNT_NAME, account_key = ACCOUNT_ACCESS_KEY, container_name= containername, protocol = "https", start = datetime.datetime.utcnow() - timedelta( minutes= 40) , expiry= datetime.datetime.utcnow() + timedelta(hours=24), permission=BlobSasPermissions(read=True), blob_name = local_file_name)

This API generates sometimes invalid sas URL resulting in error
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

And sometimes it generates the correct sas URL through which files can be downloaded successfully.

Am I missing something? not able to identify why is there an inconsistency

azure-iot-hubazure-iot-sdk
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

asergaz avatar image
0 Votes"
asergaz answered MaheshwaramSrikaran-6790 commented

Hello @MaheshwaramSrikaran-6790 ,

The way a device connected with Azure IoT Hub uploads a file to an Azure Blob Storage is explained here: Configure IoT Hub file uploads using the Azure portal

In Azure IoT Python SDK we have a good example on how to do it: https://github.com/Azure/azure-iot-sdk-python/blob/main/azure-iot-device/samples/async-hub-scenarios/upload_to_blob.py

 # get the Storage SAS information from IoT Hub.
     blob_name = "fakeBlobName12"
     storage_info = await device_client.get_storage_info_for_blob(blob_name)
     result = {"status_code": -1, "status_description": "N/A"}
    
     # Using the Storage Blob V12 API, perform the blob upload.
     try:
         upload_result = await upload_via_storage_blob(storage_info)
         if hasattr(upload_result, "error_code"):
             result = {
                 "status_code": upload_result.error_code,
                 "status_description": "Storage Blob Upload Error",
             }
 ...

And sometimes it generates the correct sas URL through which files can be downloaded successfully

If I understood well, what you try to do after uploading a file with your device is to download that file using Azure SDK for Python ? One important thing to notice is that the blob you are trying to download was already uploaded to the Container. When you see an invalid sas URL error, you can Verify the file upload directly in the portal to validate the blob is really there.

One way to avoid creating a sas URL that is targetting a blob that does not exist is to enable file upload notifications in IoT hub - it will send a notification message to backend services when the file is uploaded and from there you can create the sas URL.

Remember:
- Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer. Here is how.
- Want a reminder to come back and check responses? Here is how to subscribe to a notification.




· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the explanation.

What I have tried to do here is,
1. upload the blob file, verified that blob is present in IOT Hub.
2. Next tried to generate as URL for that blob multiple times to see the consistency of generate_blob_sas method.

observations:
1. Sometime the sas URL correctly points to the file.
2. Then sometimes I get invalid sas URL issue.


1 Vote 1 ·
asergaz avatar image asergaz MaheshwaramSrikaran-6790 ·

Hi @MaheshwaramSrikaran-6790 ,
From your explanation you are having an issue when using the Azure Storage Blobs client library for Python that is part of the Azure SDK for Python and not related with Azure IoT Python sdk am I correct? From your description, all is working well from the IoT Hub and device side.

We may have better support if you file an issue directly in the repo: https://github.com/Azure/azure-sdk-for-python/issues

When you do so, let me know it's number and I will followup there with the engineer that will look at it.

Thanks!


0 Votes 0 ·

Sure. Thanks for redirecting

1 Vote 1 ·
asergaz avatar image asergaz MaheshwaramSrikaran-6790 ·

Hello @MaheshwaramSrikaran-6790 ,
Did you create the issue there? Can you share the link?

Please share with us if you have any other questions related with your original post. Otherwise could you go ahead and mark the above as answer?

Thank you so much.

Remember:
- Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer. Here is how.

0 Votes 0 ·