forked from Azure/azure-iot-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiothub_client_file_upload.py
49 lines (37 loc) · 1.47 KB
/
iothub_client_file_upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import time
import sys
import iothub_client
import os
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult, IoTHubError
CONNECTION_STRING = "[Device Connection String]"
PROTOCOL = IoTHubTransportProvider.HTTP
PATHTOFILE = "[Full path to file]"
FILENAME = "[File name on storage after upload]"
def blob_upload_conf_callback(result, user_context):
if str(result) == 'OK':
print ( "...file uploaded successfully." )
else:
print ( "...file upload callback returned: " + str(result) )
def iothub_file_upload_sample_run():
try:
print ( "IoT Hub file upload sample, press Ctrl-C to exit" )
client = IoTHubClient(CONNECTION_STRING, PROTOCOL)
f = open(PATHTOFILE, "r")
content = f.read()
client.upload_blob_async(FILENAME, content, len(content), blob_upload_conf_callback, 0)
print ( "" )
print ( "File upload initiated..." )
while True:
time.sleep(30)
except IoTHubError as iothub_error:
print ( "Unexpected error %s from IoTHub" % iothub_error )
return
except KeyboardInterrupt:
print ( "IoTHubClient sample stopped" )
except:
print ( "generic error" )
if __name__ == '__main__':
print ( "Simulating a file upload using the Azure IoT Hub Device SDK for Python" )
print ( " Protocol %s" % PROTOCOL )
print ( " Connection string=%s" % CONNECTION_STRING )
iothub_file_upload_sample_run()