title | description | author | manager | ms.author | ms.service | services | ms.devlang | ms.topic | ms.date | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Upload files from devices to Azure IoT Hub with Node | Microsoft Docs |
How to upload files from a device to the cloud using Azure IoT device SDK for Node.js. Uploaded files are stored in an Azure storage blob container. |
wesmc7777 |
philmea |
wesmc |
iot-hub |
iot-hub |
nodejs |
conceptual |
06/28/2017 |
mqtt, devx-track-js |
[!INCLUDE iot-hub-file-upload-language-selector]
The tutorial shows you how to:
-
Securely provide a device with an Azure blob URI for uploading a file.
-
Use the IoT Hub file upload notifications to trigger processing the file in your app back end.
The Send telemetry from a device to an IoT hub 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. For example:
- Large files that contain images
- Videos
- Vibration data sampled at high frequency
- Some form of pre-processed data.
These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack. When you need to upland files from a device, you can still use the security and reliability of IoT Hub.
At the end of this tutorial you run two Node.js console apps:
-
FileUpload.js, which uploads a file to storage using a SAS URI provided by your IoT hub.
-
ReadFileUploadNotification.js, which receives file upload notifications from your IoT hub.
Note
IoT Hub supports many device platforms and languages (including C, .NET, Javascript, Python, and Java) through Azure IoT device SDKs. Refer to the [Azure IoT Developer Center] for step-by-step instructions on how to connect your device to Azure IoT Hub.
[!INCLUDE iot-hub-include-x509-ca-signed-file-upload-support-note]
-
Node.js version 10.0.x or later. Prepare your development environment describes how to install Node.js for this tutorial on either Windows or Linux.
-
An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)
-
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).
[!INCLUDE iot-hub-include-create-hub]
[!INCLUDE iot-hub-associate-storage]
In this section, you copy the device app from GitHub to upload a file to IoT hub.
-
There are two file upload samples available on GitHub for Node.js, a basic sample and one that is more advanced. Copy the basic sample from the repository here. The advanced sample is located here.
-
Create an empty folder called
fileupload
. In thefileupload
folder, create a package.json file using the following command at your command prompt. Accept all the defaults:npm init
-
At your command prompt in the
fileupload
folder, run the following command to install the azure-iot-device Device SDK package and azure-iot-device-mqtt package:npm install azure-iot-device azure-iot-device-mqtt --save
-
Using a text editor, create a FileUpload.js file in the
fileupload
folder, and copy the basic sample into it.// Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. 'use strict'; var Protocol = require('azure-iot-device-mqtt').Mqtt; var Client = require('azure-iot-device').Client; var fs = require('fs'); var deviceConnectionString = process.env.ConnectionString; var filePath = process.env.FilePath; var client = Client.fromConnectionString(deviceConnectionString, Protocol); fs.stat(filePath, function (err, fileStats) { var fileStream = fs.createReadStream(filePath); client.uploadToBlob('testblob.txt', fileStream, fileStats.size, function (err, result) { if (err) { console.error('error uploading file: ' + err.constructor.name + ': ' + err.message); } else { console.log('Upload successful - ' + result); } fileStream.destroy(); }); });
-
Add environment variables for your device connection string and the path to the file that you want to upload.
-
Save and close the FileUpload.js file.
-
Copy an image file to the
fileupload
folder and give it a name such asmyimage.png
. Place the path to the file in theFilePath
environment variable.
In this article you create a backend service to receive file upload notification messages from the IoT hub you created. To receive file upload notification messages, your service needs the service connect permission. By default, every IoT Hub is created with a shared access policy named service that grants this permission.
[!INCLUDE iot-hub-include-find-service-connection-string]
In this section, you create a Node.js console app that receives file upload notification messages from IoT Hub.
You can use the iothubowner connection string from your IoT Hub to complete this section. You will find the connection string in the Azure portal on the Shared access policy blade.
-
Create an empty folder called
fileuploadnotification
. In thefileuploadnotification
folder, create a package.json file using the following command at your command prompt. Accept all the defaults:npm init
-
At your command prompt in the
fileuploadnotification
folder, run the following command to install the azure-iothub SDK package:npm install azure-iothub --save
-
Using a text editor, create a FileUploadNotification.js file in the
fileuploadnotification
folder. -
Add the following
require
statements at the start of the FileUploadNotification.js file:'use strict'; var Client = require('azure-iothub').Client;
-
Add a
iothubconnectionstring
variable and use it to create a Client instance. Replace the{iothubconnectionstring}
placeholder value with the IoT hub connection string that you copied previously in Get the IoT hub connection string:var connectionString = '{iothubconnectionstring}';
[!NOTE] For the sake of simplicity the connection string is included in the code: this is not a recommended practice, and depending on your use-case and architecture you may want to consider more secure ways of storing this secret.
-
Add the following code to connect the client:
var serviceClient = Client.fromConnectionString(connectionString);
-
Open the client and use the getFileNotificationReceiver function to receive status updates.
serviceClient.open(function (err) { if (err) { console.error('Could not connect: ' + err.message); } else { console.log('Service client connected'); serviceClient.getFileNotificationReceiver(function receiveFileUploadNotification(err, receiver){ if (err) { console.error('error getting the file notification receiver: ' + err.toString()); } else { receiver.on('message', function (msg) { console.log('File upload from device:') console.log(msg.getData().toString('utf-8')); }); } }); } });
-
Save and close the FileUploadNotification.js file.
Now you are ready to run the applications.
At a command prompt in the fileuploadnotification
folder, run the following command:
node FileUploadNotification.js
At a command prompt in the fileupload
folder, run the following command:
node FileUpload.js
The following screenshot shows the output from the FileUpload app:
The following screenshot shows the output from the FileUploadNotification app:
You can use the portal to view the uploaded file in the storage container you configured:
In this tutorial, you learned how to use the file upload capabilities of IoT Hub to simplify file uploads from devices. You can continue to explore IoT hub features and scenarios with the following articles: