file upload x509 python with custom filepath

Cabrejos, Jason J 10 Reputation points
2024-02-19T21:06:15.9+00:00

I am following the tutorial at: https://learn.microsoft.com/en-us/azure/iot-hub/file-upload-python#upload-a-file-from-a-device-app However, when I try to upload the file it always uploads with the filepath DEVICENAME/file.jpeg. What if I want to upload a file with a different filepath such as /Location/DEVICENAME/Date/file.jpeg. or what if I don't want to upload using the Device Name at all? so I just uploaded it with /Location/Date/file.jpeg. Looking at the documentation for BlobClient, if I change the blob_info["blobName"] filepath it leads to authentication problems. so how do I tackle this problem? Thank you for your time

Azure IoT
Azure IoT
A category of Azure services for internet of things devices.
379 questions
Azure IoT Edge
Azure IoT Edge
An Azure service that is used to deploy cloud workloads to run on internet of things (IoT) edge devices via standard containers.
535 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,116 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AshokPeddakotla-MSFT 27,386 Reputation points
    2024-02-20T06:09:12.4433333+00:00

    Cabrejos, Jason J Greetings & Welcome to Microsoft Q&A forum!

    I understand that you are trying to upload files to Azure Blob Storage.

    when I try to upload the file it always uploads with the filepath DEVICENAME/file.jpeg

    The documenation you are following uses the BlobClient class from the azure.storage.blob package to upload the file.

    The BlobClient class has a upload_blob method that takes the blob_name parameter to specify the name of the blob. By default, the blob_name parameter is set to DEVICENAME/file.jpeg.

    What if I want to upload a file with a different filepath such as /Location/DEVICENAME/Date/file.jpeg. or what if I don't want to upload using the Device Name at all?

    To upload the file with a custom filepath, you can try to modify the blob_name parameter in the upload_blob method.

    For example, to upload the file to /Location/DEVICENAME/Date/file.jpeg, the blob_name parameter should be set to Location/DEVICENAME/Date/file.jpeg. If you would like to upload the file without using the device name, you can set the blob_name parameter to the desired filepath.

    For example, to upload the file to /Location/Date/file.jpeg, the blob_name parameter should be set to Location/Date/file.jpeg.

    Looking at the documentation for BlobClient, if I change the blob_info["blobName"] filepath it leads to authentication problems.

    It's important to note that the blob_info parameter is not used in the upload_blob method. Instead, the blob_name parameter is used to specify the name of the blob.

    If you are still having authentication problems, you may need to check the Azure IoT Hub permissions and access rights.

    You can refer to the Azure documentation on IoT Hub access control and Upload files with IoT Hub for more information.

    Also, refer to Azure Blob storage client library v12 for Python for more information on using the BlobClient class to upload files.

    Do let me know if that helps or have any other queries.

    If the response helped, please do click Accept Answer and Yes for was this answer helpful.

    Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.


  2. LeelaRajeshSayana-MSFT 13,456 Reputation points
    2024-02-22T21:00:37.0933333+00:00

    Hi @Cabrejos, Jason J Greetings! Welcome to Microsoft Q&A forum. Thank you for posting this question here.

    I am not sure if the BlobClinet class has the support for the use case you are looking for. I have tested the use case you are looking for using BlobServiceClient class which has some additional capabilities. Below is the code I have tested which creates the BlobServiceClient instance using the Azure Blob storage account connection string instead of the SAS URL.

    from azure.storage.blob import BlobServiceClient
    from azure.iot.device import IoTHubDeviceClient
    import datetime
    import os
    
    # Connection string for the Azure Storage account
    connection_string = "Storage Account Connection string"
    device_connection_string = "<Replcae with device connection string>"
    
    # Name of the container to upload the file to
    container_name = "<Contianer name>"
    
    # Path to the file to upload
    file_path = r"<Path to your image>"
    
    
    
    # Create a BlobServiceClient object using the connection string
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    
    device_client = IoTHubDeviceClient.create_from_connection_string(device_connection_string)
    
    
    # Create a ContainerClient object for the container
    container_client = blob_service_client.get_container_client(container_name)
    
    # Get the filename from the file path
    filename = os.path.basename(file_path)
    
    # Desired filepath for the uploaded file
    
    current_date = datetime.datetime.now().strftime("%Y-%m-%d")
    directory_path = "Location"
    desired_filepath = f"{directory_path}/{current_date}/{filename}"
    
    # Create a BlobClient object for the file
    blob_client = container_client.get_blob_client(desired_filepath)
    
    # Upload the file to the container
    with open(file_path, "rb") as data:
        result = blob_client.upload_blob(data)
    
    print(result)
    
    storage_info = device_client.get_storage_info_for_blob(filename)
    
    device_client.notify_blob_upload_status(storage_info["correlationId"], True, 200, "OK: {}".format(desired_filepath))
    

    You can get the Connection string of your Azure storage account from Access Keys tab of your storage account. Please refer the below image for reference.

    enter image description here

    Make sure to pass a valid container_name in the above code. As you can see from the below image, the file gets saved in the path specified without the need of providing device name.

    enter image description here

    Hope this helps! Please let us know if you have any additional questions or need further clarification.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.