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
* An IoT hub. Create one with the [CLI](iot-hub-create-using-cli.md) or the [Azure portal](iot-hub-create-through-portal.md). Some SDK calls require the IoT Hub connection string.
43
+
***An IoT hub**. Create one with the [Azure CLI](iot-hub-create-using-cli.md) or the [Azure portal](iot-hub-create-through-portal.md). Some SDK calls require the IoT Hub connection string.
48
44
49
-
* A registered device. Register one in the [Azure portal](iot-hub-create-through-portal.md).
45
+
***A registered device**. Register one in the [Azure portal](iot-hub-create-through-portal.md).
50
46
51
-
* IoT Hub **Service Connect** permission - To receive file upload notification messages, your backend service needs the **Service Connect** permission. By default, every IoT Hub is created with a shared access policy named **service** that grants this permission. For more information, see [Create an IoT hub using the Azure portal](/azure/iot-hub/iot-hub-create-through-portal).
47
+
* IoT Hub **Service Connect** permission - To receive file upload notification messages, your backend service needs the **Service Connect** permission. By default, every IoT Hub is created with a shared access policy named **service** that grants this permission. For more information, see [Create an IoT hub using the Azure portal](/azure/iot-hub/iot-hub-create-through-portal?#shared-access-policies).
52
48
53
-
* An Azure Storage account and Azure Blob Storage container associated with IoT Hub. You can configure this using the [Azure portal](/azure/iot-hub/iot-hub-configure-file-upload), [Azure CLI](/azure/iot-hub/iot-hub-configure-file-upload-cli), or [Azure PowerShell](/azure/iot-hub/iot-hub-configure-file-upload-powershell).
49
+
* An **Azure Storage account** and **Azure Blob Storage container** associated with IoT Hub. You can configure these using the [Azure portal](/azure/iot-hub/iot-hub-configure-file-upload), [Azure CLI](/azure/iot-hub/iot-hub-configure-file-upload-cli), or [Azure PowerShell](/azure/iot-hub/iot-hub-configure-file-upload-powershell).
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-file-upload-dotnet.md
+12-7Lines changed: 12 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,10 +20,7 @@ Follow this procedure for uploading a file from a device to IoT Hub:
20
20
* Upload the file to Azure storage
21
21
* Notify IoT Hub that it completed the upload
22
22
23
-
There are two SDK classes that are used to upload files to IoT Hub.
24
-
25
-
* The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class contains methods that a device can use to upload files to IoT Hub.
26
-
* The [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class contains methods that services can use to receive file upload notification.
23
+
The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class contains methods that a device can use to upload files to IoT Hub.
Call [GetFileNotificationReceiver](/dotnet/api/microsoft.azure.devices.serviceclient.getfilenotificationreceiver?#microsoft-azure-devices-serviceclient-getfilenotificationreceiver) to create a notification receiver. Then use a loop with [ReceiveAsync](/dotnet/api/microsoft.azure.devices.receiver-1.receiveasync?#microsoft-azure-devices-receiver-1-receiveasync(system-threading-cancellationtoken)) to wait for the file upload notification.
115
+
You can create a separate backend service to receive file upload notifications.
116
+
117
+
The [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class contains methods that services can use to receive file upload notification.
118
+
119
+
To receive file upload notification:
120
+
121
+
* Create a [CancellationToken](/dotnet/api/azure.core.httpmessage.cancellationtoken?view=azure-dotnet&branch=main#azure-core-httpmessage-cancellationtoken).
122
+
* Call [GetFileNotificationReceiver](/dotnet/api/microsoft.azure.devices.serviceclient.getfilenotificationreceiver?#microsoft-azure-devices-serviceclient-getfilenotificationreceiver) to create a notification receiver.
123
+
* Use a loop with [ReceiveAsync](/dotnet/api/microsoft.azure.devices.receiver-1.receiveasync?#microsoft-azure-devices-receiver-1-receiveasync(system-threading-cancellationtoken)) to wait for the file upload notification.
119
124
120
125
```csharp
121
126
// Define the cancellation token.
@@ -138,4 +143,4 @@ while (true)
138
143
139
144
### Sample
140
145
141
-
The SDK includes one[sample](https://github.com/Azure/azure-iot-sdk-csharp/blob/main/iothub/device/samples/getting%20started/FileUploadSample/FileUploadSample.cs).
146
+
The SDK includes this[sample](https://github.com/Azure/azure-iot-sdk-csharp/blob/main/iothub/device/samples/getting%20started/FileUploadSample/FileUploadSample.cs).
There are two SDK classes that are used to upload files to IoT Hub.
16
+
Follow this procedure for uploading a file from a device to IoT Hub:
17
17
18
-
* The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class contains methods that a device can use to upload files to IoT Hub.
19
-
* The [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class contains methods that services can use to receive file upload notification.
18
+
* Connect to IoT Hub
19
+
* Get a SAS URI from IoT Hub
20
+
* Upload the file to Azure Storage
21
+
* Send file upload status notification to IoT Hub
22
+
23
+
The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient) class contains methods that a device can use to upload files to IoT Hub.
Call [getFileUploadSasUri](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?#com-microsoft-azure-sdk-iot-device-deviceclient-getfileuploadsasuri(com-microsoft-azure-sdk-iot-deps-serializer-fileuploadsasurirequest)) to obtain a [FileUploadSasUriResponse](/java/api/com.microsoft.azure.sdk.iot.deps.serializer.fileuploadsasuriresponse) object.
41
45
42
-
FileUploadSasUriResponse includes these methods, which are passed to file upload methods. The Blob URI (SAS URI) is used to upload a file to blob storage.
46
+
`FileUploadSasUriResponse` includes these methods and return values. The return values can be passed to file upload methods.
Pass the blob URI endpoint to [BlobClientBuilder](/java/api/com.azure.storage.blob.blobclientbuilder?#com-azure-storage-blob-blobclientbuilder-buildclient()) to create the [BlobClient](/java/api/com.azure.storage.blob.blobclient) object.
## Send file upload status notification to IoT Hub
80
84
85
+
Send an upload status notification to IoT hub after a file upload attempt.
86
+
81
87
Create a [FileUploadCompletionNotification](/java/api/com.microsoft.azure.sdk.iot.deps.serializer.fileuploadcompletionnotification?#com-microsoft-azure-sdk-iot-deps-serializer-fileuploadcompletionnotification-fileuploadcompletionnotification(java-lang-string-java-lang-boolean)) object. Pass the `correlationId` and `isSuccess` file upload success status. Pass an `isSuccess``true` value when file upload was successful, `false` when not.
82
88
83
89
`FileUploadCompletionNotification` must be called even when the file upload fails. IoT Hub has a fixed number of SAS URI allowed to be active at any given time. Once you're done with the file upload, you should free your SAS URI so that other SAS URI can be generated. If a SAS URI isn't freed through this API, then it frees itself eventually based on how long SAS URI are configured to live on an IoT Hub.
@@ -97,6 +103,50 @@ Free the `client` resources.
97
103
client.closeNow();
98
104
```
99
105
100
-
### Samples
106
+
## Receive a file upload notification
107
+
108
+
You can create an application to receive file upload notifications.
109
+
110
+
The [ServiceClient](/java/api/com.azure.core.annotation.serviceclient) class contains methods that services can use to receive file upload notifications.
111
+
112
+
### Connect the client to IoT hub
113
+
114
+
Create a `IotHubServiceClientProtocol` object. The connection uses the `AMQPS` protocol.
115
+
116
+
Call `createFromConnectionString` to connect to IoT hub.
117
+
118
+
```java
119
+
privatestaticfinalString connectionString ="{Your service connection string here}";
There are two Java file upload [samples](https://github.com/Azure/azure-iot-sdk-java/tree/main/iothub/device/iot-device-samples/file-upload-sample/src/main/java/samples/com/microsoft/azure/sdk/iot).
Follow this procedure for uploading a file from a device to IoT Hub:
30
+
Follow this procedure for uploading a file from a device to IoT hub:
31
31
32
-
* Connect to IoT Hub
33
-
* Get a SAS URI from IoT Hub
34
-
* Upload the file to Azure storage
35
-
* Notify IoT Hub that it completed the upload
32
+
* Connect the client to IoT hub and get storage information
33
+
* Upload the file to BloB Storage
34
+
* Notify IoT hub of upload status
36
35
37
36
### Import statements
38
37
@@ -45,18 +44,18 @@ from azure.core.exceptions import AzureError
45
44
from azure.storage.blob import BlobClient
46
45
```
47
46
48
-
### Connect the client and get storage information
47
+
### Connect the client to IoT hub and get storage information
49
48
50
-
Call [create_from_connection_string](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-create-from-connection-string) to connect to IoT Hub.
49
+
Call [create_from_connection_string](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-create-from-connection-string) to connect to IoT hub.
Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client to an Azure IoT Hub.
58
+
Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client to an Azure IoT hub.
Use [from_blob_url](/python/api/azure-storage-blob/azure.storage.blob.blobclient?#azure-storage-blob-blobclient-from-blob-url) to create a [BlobClient](/python/api/azure-storage-blob/azure.storage.blob.blobclient?#azure-storage-blob-blobclient-from-blob-url) object from a blob URL.
80
+
To upload a file into Blob Storage:
82
81
83
-
Then call [upload_blob](/python/api/azure-storage-blob/azure.storage.blob.blobclient?#azure-storage-blob-blobclient-upload-blob) to upload the file into the blob storage.
82
+
* Use [from_blob_url](/python/api/azure-storage-blob/azure.storage.blob.blobclient?#azure-storage-blob-blobclient-from-blob-url) to create a [BlobClient](/python/api/azure-storage-blob/azure.storage.blob.blobclient?#azure-storage-blob-blobclient-from-blob-url) object from a blob URL.
83
+
* Call [upload_blob](/python/api/azure-storage-blob/azure.storage.blob.blobclient?#azure-storage-blob-blobclient-upload-blob) to upload the file into the Blob Storage.
84
84
85
-
This example function parses the passed `blob_info` structure passed to create a URL that it uses to initialize an [BlobClient](/python/api/azure-storage-blob/azure.storage.blob.blobclient). Then it calls `upload_blob` to upload the file into Azure blob storage.
85
+
This example function parses the passed `blob_info` structure to create a URL that it uses to initialize an [BlobClient](/python/api/azure-storage-blob/azure.storage.blob.blobclient). Then it calls `upload_blob` to upload the file into Blob Storage.
Use [notify_blob_upload_status](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-notify-blob-upload-status) to notify IoT Hub of the status of the blob storage operation. Pass the `correlation_id` obtained by the `get_storage_info_for_blob` method. The `correlation_id` is used by IoT Hub to notify any service that might be listening for a notification regarding the status of the file upload task.
117
+
Use [notify_blob_upload_status](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-notify-blob-upload-status) to notify IoT hub of the status of the Blob Storage operation. Pass the `correlation_id` obtained by the `get_storage_info_for_blob` method. The `correlation_id` is used by IoT Hub to notify any service that might be listening for a notification regarding the status of the file upload task.
Shut down the clientfor graceful exit. Once this method is called, any attempts at further client calls result in a [ClientError](/python/api/azure-iot-device/azure.iot.device.exceptions.clienterror) being raised.
127
+
Shut down the client. Once this method is called, any attempts at further client calls result in a [ClientError](/python/api/azure-iot-device/azure.iot.device.exceptions.clienterror) being raised.
128
128
129
129
```python
130
-
# Graceful exit
131
-
device_client.shutdown()
130
+
device_client.shutdown()
132
131
```
133
132
134
133
### Error handling example
135
134
136
-
This example shows a function that contains the previous steps with error handling logic.
135
+
This example shows a function that contains the previous steps including error handling logic.
0 commit comments