You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -22,21 +22,15 @@ This article shows how to use the [file upload capabilities of IoT Hub](iot-hub-
22
22
23
23
The [Send telemetry from a device to an IoT hub](quickstart-send-telemetry-python.md) quickstart demonstrates the basic device-to-cloud messaging functionality of IoT Hub. However, in some scenarios you cannot easily map the data your devices send into the relatively small device-to-cloud messages that IoT Hub accepts. When you need to upland files from a device, you can still use the security and reliability of IoT Hub.
24
24
25
-
> [!NOTE]
26
-
> IoT Hub Python SDK currently only supports uploading character-based files such as **.txt** files.
27
-
28
-
At the end of this tutorial you run the Python console app:
25
+
At the end of this tutorial, you run the Python console app:
29
26
30
27
***FileUpload.py**, which uploads a file to storage using the Python Device SDK.
* Make sure that port 8883 is open in your firewall. The device sample in this article uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](iot-hub-mqtt-support.md#connecting-to-iot-hub).
42
36
@@ -46,88 +40,140 @@ At the end of this tutorial you run the Python console app:
46
40
47
41
In this section, you create the device app to upload a file to IoT hub.
48
42
49
-
1. At your command prompt, run the following command to install the **azure-iothub-device-client** package:
43
+
1. At your command prompt, run the following command to install the **azure-iot-device** package. You use this package to coordinate the file upload with your IoT hub.
50
44
51
45
```cmd/sh
52
-
pip install azure-iothub-device-client
46
+
pip install azure-iot-device
53
47
```
54
48
55
-
2. Using a text editor, create a test file that you will upload to blob storage.
49
+
1. At your command prompt, run the following command to install the [**azure.storage.blob**](https://pypi.org/project/azure-storage-blob/) package. You use this package to perform the file upload.
50
+
51
+
```cmd/sh
52
+
pip install azure.storage.blob
53
+
```
56
54
57
-
> [!NOTE]
58
-
> IoT Hub Python SDK currently only supports uploading character-based files such as **.txt** files.
55
+
1. Create a test file that you'll upload to blob storage.
59
56
60
-
3. Using a text editor, create a **FileUpload.py** file in your working folder.
57
+
1. Using a text editor, create a **FileUpload.py** file in your working folder.
61
58
62
-
4. Add the following `import` statements and variables at the start of the **FileUpload.py** file.
59
+
1. Add the following `import` statements and variables at the start of the **FileUpload.py** file.
63
60
64
61
```python
65
-
import time
66
-
import sys
67
-
import iothub_client
68
62
import os
69
-
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult, IoTHubError
63
+
import asyncio
64
+
from azure.iot.device.aio import IoTHubDeviceClient
65
+
from azure.core.exceptions import AzureError
66
+
from azure.storage.blob import BlobClient
70
67
71
68
CONNECTION_STRING = "[Device Connection String]"
72
-
PROTOCOL = IoTHubTransportProvider.HTTP
73
-
74
-
PATHTOFILE = "[Full path to file]"
75
-
FILENAME = "[File name for storage]"
69
+
PATH_TO_FILE = r"[Full path to local file]"
76
70
```
77
71
78
-
5. In your file, replace `[Device Connection String]` with the connection string of your IoT hub device. Replace `[Full path to file]` with the path to the test file that you created, or any file on your device that you want to upload. Replace `[File name for storage]` with the name that you want to give to your file after it's uploaded to blob storage.
72
+
1. In your file, replace `[Device Connection String]` with the connection string of your IoT hub device. Replace `[Full path to local file]` with the path to the test file that you created or any file on your device that you want to upload.
79
73
80
-
6. Create a callback for the **upload_blob** function:
74
+
1. Create a function to upload the file to blob storage:
print("\nUploading file: {} to Azure Storage as blob: {} in container {}\n".format(file_name, blob_info["blobName"], blob_info["containerName"]))
87
+
88
+
# Upload the specified file
89
+
with BlobClient.from_blob_url(sas_url) as blob_client:
90
+
with open(file_name, "rb") as f:
91
+
result = blob_client.upload_blob(f, overwrite=True)
92
+
return (True, result)
93
+
94
+
except FileNotFoundError as ex:
95
+
# catch file not found and add an HTTP status code to return in notification to IoT Hub
96
+
ex.status_code = 404
97
+
return (False, ex)
98
+
99
+
except AzureError as ex:
100
+
# catch Azure errors that might result from the upload operation
101
+
return (False, ex)
88
102
```
89
103
90
-
7. Add the following code to connect the client and upload the file. Also include the `main` routine:
104
+
This function parses the *blob_info* structure passed into it to create a URL that it uses to initialize an [azure.storage.blob.BlobClient](https://docs.microsoft.com/python/api/azure-storage-blob/azure.storage.blob.blobclient?view=azure-python). Then it uploads your file to Azure blob storage using this client.
105
+
106
+
1. Add the following code to connect the client and upload the file:
This code creates an asynchronous **IoTHubDeviceClient** and uses the following APIs to manage the file upload with your IoT hub:
167
+
168
+
* **get_storage_info_for_blob** gets information from your IoT hub about the linked Storage Account you created previously. This information includes the hostname, container name, blob name, and a SAS token. The storage info is passed to the **store_blob** function (created in the previous step), so the **BlobClient** in that function can authenticate with Azure storage. The **get_storage_info_for_blob** method also returns a correlation_id, which is used in the **notify_blob_upload_status** method. The correlation_id is IoT Hub's way of marking which blob you're working on.
169
+
170
+
* **notify_blob_upload_status** notifies IoT Hub of the status of your blob storage operation. You pass it the correlation_id obtained by the **get_storage_info_for_blob** method. It's used by IoT Hub to notify any service that might be listening for a notification on the status of the file upload task.
171
+
172
+
1. Save and close the **UploadFile.py** file.
127
173
128
174
## Run the application
129
175
130
-
Now you are ready to run the application.
176
+
Now you're ready to run the application.
131
177
132
178
1. At a command prompt in your working folder, run the following command:
133
179
@@ -137,11 +183,11 @@ Now you are ready to run the application.
137
183
138
184
2. The following screenshot shows the output from the **FileUpload** app:
139
185
140
-

186
+

141
187
142
188
3. You can use the portal to view the uploaded file in the storage container you configured:
* An active Azure account. (If you don't have an account, you can create a [free account](https://azure.microsoft.com/pricing/free-trial/) in just a couple of minutes.)
14
+
15
+
*[Python version 3.7 or later](https://www.python.org/downloads/) is recommended. Make sure to use the 32-bit or 64-bit installation as required by your setup. When prompted during the installation, make sure to add Python to your platform-specific environment variable. For other versions of Python supported, see [Azure IoT Device Features](https://github.com/Azure/azure-iot-sdk-python/tree/master/azure-iot-device#azure-iot-device-features) in the SDK documentation.
16
+
17
+
> [!IMPORTANT]
18
+
> Because the device code in this article uses the asynchronous API, you cannot use Python 2.7.
0 commit comments