Skip to content

Commit efd734e

Browse files
committed
Edits
1 parent db16b91 commit efd734e

5 files changed

+173
-31
lines changed

includes/iot-hub-howto-auth-device-cert-dotnet.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@ To connect a device to IoT Hub using an X.509 certificate:
1616

1717
1. Use [DeviceAuthenticationWithX509Certificate](/dotnet/api/microsoft.azure.devices.client.deviceauthenticationwithx509certificate) to create an object that contains device and certificate information. `DeviceAuthenticationWithX509Certificate` is passed as the second parameter to `DeviceClient.Create` (step 2).
1818

19-
1. Use [DeviceClient.Create](/dotnet/api/microsoft.azure.devices.client.deviceclient.create?view=azure-dotnet&branch=main#microsoft-azure-devices-client-deviceclient-create(system-string-microsoft-azure-devices-client-iauthenticationmethod-microsoft-azure-devices-client-transporttype)) to connect the device to IoT Hub using an X.509 certificate.
19+
1. Use [DeviceClient.Create](/dotnet/api/microsoft.azure.devices.client.deviceclient.create?view=azure-dotnet&#microsoft-azure-devices-client-deviceclient-create(system-string-microsoft-azure-devices-client-iauthenticationmethod-microsoft-azure-devices-client-transporttype)) to connect the device to IoT Hub using an X.509 certificate.
2020

21-
In this example, the device and certificate information has been populated in the `auth` `DeviceAuthenticationWithX509Certificate` object that is passed to `DeviceClient.Create`. An example of `DeviceAuthenticationWithX509Certificate` information population is omitted because of the flexible nature of how the certificate information can be added.
21+
In this example, the device and certificate information are populated in the `auth` `DeviceAuthenticationWithX509Certificate` object that is passed to `DeviceClient.Create`.
2222

2323
```csharp
24+
RootCertPath = "~/certificates/certs/sensor-thl-001-device.cert.pem";
25+
Intermediate1CertPath = "~/certificates/certs/sensor-thl-001-device.intermediate1.cert.pem";
26+
Intermediate2CertPath = "~/certificates/certs/sensor-thl-001-device.intermediate2.cert.pem";
27+
DevicePfxPath = "~/certificates/certs/sensor-thl-001-device.cert.pfx";
28+
DevicePfxPassword = "1234";
29+
DeviceName = "MyDevice";
2430
HostName = "xxxxx.azure-devices.net";
31+
32+
var chainCerts = new X509Certificate2Collection();
33+
chainCerts.Add(new X509Certificate2(RootCertPath));
34+
chainCerts.Add(new X509Certificate2(Intermediate1CertPath));
35+
chainCerts.Add(new X509Certificate2(Intermediate2CertPath));
36+
using var deviceCert = new X509Certificate2(DevicePfxPath, DevicePfxPassword);
37+
using var auth = new DeviceAuthenticationWithX509Certificate(DeviceName, deviceCert, chainCerts);
38+
2539
using var deviceClient = DeviceClient.Create(
2640
HostName,
2741
auth,

includes/iot-hub-howto-file-upload-dotnet.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,47 @@ You can create a backend service to receive file upload notification messages fr
120120

121121
The [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class contains methods that services can use to receive file upload notifications.
122122

123-
To receive file upload notification:
123+
### Add service NuGet Package
124124

125-
1. Call [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.serviceclient.createfromconnectionstring) to connect to IoT hub. Pass the IoT hub primary connection string.
126-
1. Create a [CancellationToken](/dotnet/api/azure.core.httpmessage.cancellationtoken?#azure-core-httpmessage-cancellationtoken).
127-
1. Call [GetFileNotificationReceiver](/dotnet/api/microsoft.azure.devices.serviceclient.getfilenotificationreceiver?#microsoft-azure-devices-serviceclient-getfilenotificationreceiver) to create a notification receiver.
128-
1. 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.
125+
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
126+
127+
### Connect to IoT hub
128+
129+
You can connect a backend service to IoT Hub using the following methods:
130+
131+
* Shared access policy
132+
* Microsoft Entra
133+
134+
[!INCLUDE [iot-authentication-service-connection-string.md](iot-authentication-service-connection-string.md)]
135+
136+
#### Connect using a shared access policy
137+
138+
Connect a backend application to a device using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.registrymanager.createfromconnectionstring). Your application needs **service connect** permission. Supply this shared access policy connection string as a parameter to `fromConnectionString`. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
129139

130140
For example:
131141

132142
```csharp
133143
using Microsoft.Azure.Devices;
134144
static ServiceClient serviceClient;
135-
static string connectionString = "{IoT hub connection string}";
145+
static string connectionString = "{Shared access policy connection string}";
136146
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
147+
```
148+
149+
#### Connect using Microsoft Entra
137150

151+
[!INCLUDE [iot-hub-howto-connect-service-iothub-entra-dotnet](iot-hub-howto-connect-service-iothub-entra-dotnet.md)]
152+
153+
### Receive file upload notification
154+
155+
To receive file upload notification:
156+
157+
1. Create a [CancellationToken](/dotnet/api/azure.core.httpmessage.cancellationtoken?#azure-core-httpmessage-cancellationtoken).
158+
1. Call [GetFileNotificationReceiver](/dotnet/api/microsoft.azure.devices.serviceclient.getfilenotificationreceiver?#microsoft-azure-devices-serviceclient-getfilenotificationreceiver) to create a notification receiver.
159+
1. 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.
160+
161+
For example:
162+
163+
```csharp
138164
// Define the cancellation token
139165
CancellationTokenSource source = new CancellationTokenSource();
140166
CancellationToken token = source.Token;

includes/iot-hub-howto-file-upload-java.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,13 @@ Free the `client` resources.
123123
client.closeNow();
124124
```
125125

126-
## Receive a file upload notification in a backend application
126+
## Create a backend application
127127

128-
You can create a backend application to receive file upload notifications.
129-
130-
To create a file upload notification application:
131-
132-
1. Connect to the IoT hub service client
133-
1. Check for a file upload notification
128+
This section describes how to receive a file upload notification in a backend application.
134129

135130
The [ServiceClient](/java/api/com.azure.core.annotation.serviceclient) class contains methods that services can use to receive file upload notifications.
136131

137-
### Connect to the IoT hub service client
132+
### Connect to the IoT Hub
138133

139134
Create a `IotHubServiceClientProtocol` object. The connection uses the `AMQPS` protocol.
140135

includes/iot-hub-howto-file-upload-node.md

Lines changed: 121 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ ms.custom: mqtt, devx-track-js
1313

1414
## Overview
1515

16-
This how-to contains two sections:
16+
This article describes how to use the [Azure IoT SDK for Node.js](https://github.com/Azure/azure-iot-sdk-node) to create a device app to upload a file and backend service application receive file upload notification.
1717

18-
* Upload a file from a device application
19-
* Receive file upload notification in a backend application
20-
21-
## Upload a file from a device application
18+
## Create a device application
2219

2320
This section describes how to upload a file from a device to an IoT hub using the [azure-iot-device](/javascript/api/azure-iot-device) package in the Azure IoT SDK for Node.js.
2421

@@ -32,7 +29,7 @@ npm install azure-iot-device azure-iot-device-mqtt @azure/storage-blob --save
3229

3330
The [azure-iot-device](/javascript/api/azure-iot-device) package contains objects that interface with IoT devices.
3431

35-
Follow this procedure for uploading a file from a device to IoT hub:
32+
Follow this procedure to upload a file from a device to IoT hub:
3633

3734
1. Get Blob shared access signatures
3835
1. Upload the file to Azure Storage
@@ -43,15 +40,97 @@ Follow this procedure for uploading a file from a device to IoT hub:
4340
Create Client, Protocol, errors, and path modules using the installed packages.
4441

4542
```javascript
46-
const Client = require('azure-iot-device').Client;
4743
const Protocol = require('azure-iot-device-mqtt').Mqtt;
4844
const errors = require('azure-iot-common').errors;
4945
const path = require('path');
5046
```
5147

52-
### Get a SAS URI from IoT hub
48+
### Connect a device to IoT Hub
49+
50+
A device app can authenticate with IoT Hub using the following methods:
51+
52+
* X.509 certificate
53+
* Shared access key
54+
55+
#### Authenticate using an X.509 certificate
56+
57+
[!INCLUDE [iot-hub-howto-auth-device-cert-node](iot-hub-howto-auth-device-cert-node.md)]
58+
59+
#### Authenticate using a shared access key
60+
61+
##### Choose a transport protocol
62+
63+
The `Client` object supports these protocols:
64+
65+
* `Amqp`
66+
* `Http` - When using `Http`, the `Client` instance checks for messages from IoT Hub infrequently (a minimum of every 25 minutes).
67+
* `Mqtt`
68+
* `MqttWs`
69+
* `AmqpWs`
70+
71+
Install needed transport protocols on your development machine.
72+
73+
For example, this command installs the `Amqp` protocol:
74+
75+
```cmd/sh
76+
npm install azure-iot-device-amqp --save
77+
```
78+
79+
For more information about the differences between MQTT, AMQP, and HTTPS support, see [Cloud-to-device communications guidance](../articles/iot-hub/iot-hub-devguide-c2d-guidance.md) and [Choose a communication protocol](../articles/iot-hub/iot-hub-devguide-protocols.md).
80+
81+
##### Create a client object
82+
83+
Create a `Client` object using the installed package.
84+
85+
For example:
86+
87+
```javascript
88+
const Client = require('azure-iot-device').Client;
89+
```
90+
91+
##### Create a protocol object
92+
93+
Create a `Protocol` object using an installed transport package.
94+
95+
This example assigns the AMQP protocol:
96+
97+
```javascript
98+
const Protocol = require('azure-iot-device-amqp').Amqp;
99+
```
100+
101+
##### Add the device connection string and transport protocol
102+
103+
Call [fromConnectionString](/javascript/api/azure-iot-device/client?#azure-iot-device-client-fromconnectionstring) to supply device connection parameters:
104+
105+
* **connStr** - The device connection string.
106+
* **transportCtor** - The transport protocol.
107+
108+
This example uses the `Amqp` transport protocol:
109+
110+
```javascript
111+
const deviceConnectionString = "{IoT hub device connection string}"
112+
const Protocol = require('azure-iot-device-mqtt').Amqp;
113+
let client = Client.fromConnectionString(deviceConnectionString, Protocol);
114+
```
115+
116+
##### Open the connection to IoT Hub
53117

54-
Use [getBlobSharedAccessSignature](/javascript/api/azure-iot-device/client?#azure-iot-device-client-getblobsharedaccesssignature) to get the linked storage account SAS token from IoT hub. As described in prerequisites, the IoT hub is linked to the Blob Storage.
118+
Use the [open](/javascript/api/azure-iot-device/client?#azure-iot-device-client-open) method to open connection between an IoT device and IoT Hub.
119+
120+
For example:
121+
122+
```javascript
123+
client.open(function(err) {
124+
if (err) {
125+
console.error('error connecting to hub: ' + err);
126+
process.exit(1);
127+
}
128+
})
129+
```
130+
131+
#### Get a SAS URI from IoT hub
132+
133+
Use [getBlobSharedAccessSignature](/javascript/api/azure-iot-device/client?#azure-iot-device-client-getblobsharedaccesssignature) to get the linked storage account SAS token from IoT hub.
55134

56135
For example:
57136

@@ -65,7 +144,7 @@ throw new errors.ArgumentError('Invalid upload parameters');
65144
}
66145
```
67146

68-
### Upload the file to IoT hub
147+
#### Upload the file to IoT hub
69148

70149
To upload a file from a device to IoT hub:
71150

@@ -125,6 +204,38 @@ console.log('uploadStreamToBlockBlob success');
125204
await client.notifyBlobUploadStatus(blobInfo.correlationId, isSuccess, statusCode, statusDescription);
126205
```
127206

207+
### Upload the local file to blob storage
208+
209+
You can upload a local file to blob storage from a computer
210+
211+
```javascript
212+
const deviceClient = Client.fromConnectionString(deviceConnectionString, Protocol);
213+
uploadToBlob(localFilePath, deviceClient)
214+
.catch((err) => {
215+
console.log(err);
216+
})
217+
.finally(() => {
218+
process.exit();
219+
});
220+
221+
```
222+
223+
#### SDK file upload sample
224+
225+
The SDK includes an [upload to blob advanced](https://github.com/Azure/azure-iot-sdk-node/blob/main/device/samples/javascript/upload_to_blob_advanced.js) sample.
226+
227+
## Create a backend application
228+
229+
This section describes how to receive file upload notification in a backend application.
230+
231+
### Install service SDK package
232+
233+
Run this command to install **azure-iothub** on your development machine:
234+
235+
```cmd/sh
236+
npm install azure-iothub --save
237+
```
238+
128239
## Receive file upload notification in a backend application
129240

130241
You can create a backend application to check the IoT hub service client for device file upload notifications.
@@ -185,7 +296,3 @@ if (err) {
185296
});
186297
}
187298
```
188-
189-
### SDK file upload sample
190-
191-
The SDK includes an [upload to blob advanced](https://github.com/Azure/azure-iot-sdk-node/blob/main/device/samples/javascript/upload_to_blob_advanced.js) sample.

includes/iot-hub-howto-file-upload-python.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.author: kgremban
77
ms.service: azure-iot-hub
88
ms.devlang: python
99
ms.topic: include
10-
ms.date: 07/01/2024
10+
ms.date: 12/12/2024
1111
ms.custom: mqtt, devx-track-python, py-fresh-zinc
1212
---
1313

0 commit comments

Comments
 (0)