Uploading an image with Azure IoT SDK for Python

826 views Asked by At

Hi everyone thanks for reading this. I want to upload a picture from my raspberry pi to my Blob storage. My Blob storage is linked to my Azure IoT hub with a container for the pictures. To achieve this I'm using Azure IoT SDK for Python now I'm in a pickle how I should format my image. I did some research and apparently when you upload a picture to a Blob it should be in a byte array ? I tried a wide range of formats, but without success. I have the image stored locally and want to pass it as content.

My code in Python:

def upload_to_blob(filename,content):
#Upload foto naar blob
try:
    iotHubClient.upload_blob_async(filename,content,len(content),blob_upload_confirmation_callback,1001)        
except Exception as e:
    print e
    update_log('Unexpected error from IoT hub')

I found this mock version of iotHubClient in C++

IOTHUB_CLIENT_RESULT IoTHubClient_UploadToBlobAsync(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const char* destinationFileName, const unsigned char* source, size_t size, IOTHUB_CLIENT_FILE_UPLOAD_CALLBACK iotHubClientFileUploadCallback, void* context)

In the samples they only show how to upload plain text as a text file and I can't seem to find it in the documentation. If you could help me so that I can open the picture, I send to my blob, on my laptop. Thanks in advance !

1

There are 1 answers

0
Dominic Betts On BEST ANSWER

The Python SDK handles to upload for you, so you can do something like this:

    filename = "myimage.png"
    f = open("C:\Temp\myimage.png", "rb")
    content = f.read()

    print("IoTHubClient is uploading blob to storage")
    iotHubClient.upload_blob_async(filename, content, len(content), blob_upload_confirmation_callback, 1001)