Azure IoT Hub ten minutes Starter Series (4) - to achieve from the device to upload log files / images to Azure Storage

In this paper, a case Share:

IoTHub 10 minutes to upload files through Device SDK

 Video Station B: https: //www.bilibili.com/video/av90224073/

The main contents of this paper are as follows:

1. Understand the IoT Hub where the files are stored in the

2. Use Node.js Device SDK upload TXT file

3. IOT device to view files uploaded in Storage

 

 

Graphic content:

This case Reference: https://docs.azure.cn/zh-cn/iot-hub/iot-hub-node-node-file-upload

 

1. Device Device SDK to upload a file Azure IoT Hub stored in the Storage, configure Storage containers and store files with a good advance:

2. Use Node.js SDK upload files

Download and install Node.js  http://nodejs.cn/

Installation Node.js SDK:

npm install azure-iot-device azure-iot-device-mqtt --save

FIG installation process is as follows:

 

 

New folder, the new upload_to_blob.js, the following sample code in the copy-in upload_to_blob.js

'use strict';

var Protocol = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').Client; var fs = require('fs'); var connectionString = 'YOUR DEIVCE CONNECT STRING'; if (!connectionString) { console.log('Please set the DEVICE_CONNECTION_STRING environment variable.'); process.exit(-1); } var filePath = 'log.txt'; var client = Client.fromConnectionString(connectionString, Protocol); fs.stat(filePath, function (err, fileStats) { if (err) { console.error('could not read file: ' + err.toString()); process.exit(-1); } else { var fileStream = fs.createReadStream(filePath); client.uploadToBlob('testblob.txt', fileStream, fileStats.size, function (err) { fileStream.destroy(); if (err) { console.error('error uploading file: ' + err.constructor.name + ': ' + err.message); process.exit(-1); } else { console.log('Upload successful'); process.exit(0); } }); } });

 

Creating package.json file using the following command. Accept all the defaults:

npm init

 

Creating log.txt in the folder, the content freely.

Thus, should the folder as shown below:

 

Run the following command to run the client code:

node upload_to_blob.js

Program prompts as follows, indicating successful upload files:

 

Enter Azure Storage container, check the upload results:

 

Guess you like

Origin www.cnblogs.com/shuzhenyu/p/12334257.html