无需使用Azure客户端SDK即可连接到IoT中心 [英] Connect to IoT Hub without using the Azure Client SDK
问题描述
我想不使用客户端SDK连接到Azure Iot Hub.
在
https://azure .microsoft.com/nb-no/blog/upload-files-from-devices-with-azure-iot-hub/
有关于如何执行此操作的文档
1)获取用于存储的SAS URI
2)将完成的上传通知IoT中心
I want to connect to the Azure Iot Hub not using the Client SDK.
On
https://azure.microsoft.com/nb-no/blog/upload-files-from-devices-with-azure-iot-hub/
there are documentation on how to do this by
1) get the SAS URI for storage
2) to notify the IoT hub of a completed upload
但是在此之前,您需要使用DeviceConnectionString连接到IoT中心.有没有人举过例子/提示如何完成和上传文件?
But before this can be done you need to connect to the IoT Hub using the DeviceConnectionString. Does anyone have an example / hints of how this and uploading a file can be done?
推荐答案
You can follow "File uploads with IoT Hub" and do it through four steps:
- 将Azure存储帐户与IoT中心相关联.在这里,我们通过Azure门户进行此操作.在您的物联网中心中找到文件上传",然后选择您的存储帐户的存储容器.
- 通过将POST发送到IoT中心来初始化文件上传,如下所示:
并且IoT中心返回以下数据:
And IoT Hub returns the following data:
-
设备使用Azure存储SDK将文件上传到存储.您可以关注本教程.代码如下:
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("azureportaldeploy");
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("device1/testfileupload2");
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"{your file path}\testfileupload2.txt"))
{
blockBlob.UploadFromStream(fileStream);
}
上传完成后,设备将向IoT中心发送POST,如下所示:
Once the upload is completed, the device sends a POST to the IoT hub like this:
Besides, you can download the blob to check the file uploaded. And check the file upload notification using the following code:
private async static Task ReceiveFileUploadNotificationAsync()
{
var notificationReceiver = serviceClient.GetFileNotificationReceiver();
Console.WriteLine("\nReceiving file upload notification from service");
while (true)
{
var fileUploadNotification = await notificationReceiver.ReceiveAsync();
if (fileUploadNotification == null) continue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received file upload noticiation: {0}", string.Join(", ", fileUploadNotification.BlobName));
Console.ResetColor();
await notificationReceiver.CompleteAsync(fileUploadNotification);
}
}
这篇关于无需使用Azure客户端SDK即可连接到IoT中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!