Azure IoT Hub 十分钟入门系列 (10)- 实现云端接收设备文件上传通知(file upload notification,Node.js示例)

131 篇文章 10 订阅
121 篇文章 5 订阅

本文主要分享一个案例:

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

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

 

本文主要有如下内容:

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

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

 

视频介绍:https://www.51azure.cloud/post/2020/6/7/azure-iot-hub-10-file-upload-notification-nodejs

 

 

图文介绍:

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

2. 使用如下示例代码:

3.安装SDK:

npm init

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

 

执行

npm install azure-iothub --save

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

修改后的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

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

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

执行:

node upload_to_blob.js

Service 侧立刻收到通知:

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值