generate_blob_sas sdk sometimes doesnt work in python

Maheshwaram Srikaran 96 Reputation points
2022-04-06T01:17:57.483+00:00

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 Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,142 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
210 questions
0 comments No comments
{count} votes

Accepted answer
  1. António Sérgio Azevedo 7,666 Reputation points Microsoft Employee
    2022-04-06T14:24:24.737+00:00

    Hello @Maheshwaram Srikaran ,

    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.

0 additional answers

Sort by: Most helpful