postman上传文件接收不到_Azure IoT Hub 十分钟入门系列 (10) 实现云端接收设备文件上传通知...

e0530b5a05919cc29e4a88554e6950ca.png

本文主要分享一个案例:

通过Service SDK获取文件上传通知;

本文的前提是《Azure IoT Hub 十分钟入门系列 (4)- 实现从设备上传日志文件/图片到 Azure Storage(Node.js示例)》。

本文主要有如下内容:

1. 在IoT Hub中打开文件上传通知

2. 使用Node.js Service SDK 接受文件上传通知

视频介绍:

点击文末阅读原文观看视频介绍。

图文介绍:
1. 在IoT Hub中打开文件上传通知:

c515392bac95c2c7126cad2de922cd8a.png

2. 使用如下示例代码:

542c83386b258a50f78b38a64ffe5368.png

3.安装SDK:
npm init

bf6129c965b5e15e4c2a0baffc31c7a4.png

回车->回车确认,直到出现如下的package.json界面:

2fbb9c09aa93fa8fc04e7bdba201afd6.png

执行

npm install azure-iothub --save

51aa735b4536f9d842406efa9e1ac6ad.png

4.修改Service 侧代码中的连接字符串:

c2158d658ca310aa72a011fa4aab14cf.png

修改后的Service 侧代码如下:

'use strict';

var Client = require('azure-iothub').Client;

//var connectionString = process.env.IOTHUB_CONNECTION_STRING;

var connectionString = "your iot hub connection string";
if (!connectionString) {
console.log('Please set the IOTHUB_CONNECTION_STRING environment variable.');
process.exit(-1);
}

var client = Client.fromConnectionString(connectionString);

client.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
process.exit(-1);
} else {
console.log('Client connected');

client.getFileNotificationReceiver(function(err, receiver) {
if(err) {
console.error('Could not get file notification receiver: ' + err.message);
process.exit(-1);
} else {
receiver.on('message', function(msg) {
console.log('File uploaded: ');
console.log(msg.data.toString());
receiver.complete(msg, function(err) {
if (err) {
console.error('Could not complete the message: ' + err.message);
process.exit(-1);
} else {
console.log('Message completed');
process.exit(0);
}
});
});
}
});
}
});
5.运行Service 侧代码:
node receive_file_notifications.js

8f665fe134b9045f454e65708331b0b7.png

6.运行Device 侧代码,上传文件,从Service 侧接收文件上传通知:

注意,device侧代码运行参照文档《Azure IoT Hub 十分钟入门系列 (4)- 实现从设备上传日志文件/图片到 Azure Storage(Node.js示例)》

454950f0e174e6d2569d3e1a3118c1bb.png

执行:

node upload_to_blob.js

d340e5da5a84858db194498359b1e2bd.png

Service 侧立刻收到通知:

c35937f84852b96c0f65f6da09b7bb0b.png

c0f1a3260222b76aec6f9eadf97016fc.gif

e78589ba5ff76bc91a1e33438864229e.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值