在不使用Azure Client SDK

时间:2017-03-13 14:18:11

标签: c# azure azure-iot-hub

我想连接到不使用Client SDK的Azure Iot Hub。 上 https://azure.microsoft.com/nb-no/blog/upload-files-from-devices-with-azure-iot-hub/ 有关于如何做到这一点的文档 1)获取存储的SAS URI 2)通知IoT集线器完成上传

但在此之前,您需要使用DeviceConnectionString连接到IoT Hub。有没有人有关于如何以及如何上传文件的示例/提示?

3 个答案:

答案 0 :(得分:2)

如果您想要没有SDK(我很想知道原因),您可以找到所有REST API参考文档here。 有关存储SAS URI的详细信息是here。 对于文件上传通知,它是here。 使用身份验证+这些,您应该能够通过IoT Hub实现文件上载。

答案 1 :(得分:1)

这是从IoT Hub读取的ATWINC1500 Arduino AVR实现(修改端点并更改为POST):

#define NAMESPACE "{your-iot-hub}.azure-devices.net"
#define AUTHORIZATION_HEADER "Authorization: SharedAccessSignature sr=xxxxxxxxxxxxxxxxxxxx"

void httpRequest() {
  Serial.println("\nConnecting to IoT Hub...");
  if (client.connect(NAMESPACE, 443)) {
    Serial.println("Connected.");
    // Send HTTP request:
    client.println("GET /devices/{your_device_id}/messages/devicebound?api-version=2016-02-03 HTTP/1.1");
    client.println("Host: {your-iot-hub}.azure-devices.net");
    client.println(AUTHORIZATION_HEADER);
    client.println("User-Agent: Atmel ATWINC1500");
    client.println("Connection: close");
    client.println();
  }
}

我刚用Device Explorer生成了2年有效的SAS密钥。

我很确定如果没有实时时钟我就无法计算自己的SAS,这是AVR没有的...... Oliver可以确认。

答案 2 :(得分:1)

您可以按照“Comment on bug report from Sep 1, 2014”进行操作,然后执行以下四个步骤:

  1. 将Azure存储帐户与IoT Hub关联。这里我们通过Azure门户网站完成此操作。在iot集线器中查找文件上载,然后选择存储帐户的存储容器。
  2. 通过向IoT集线器发送POST来初始化文件上载,如下所示:
  3. File uploads with IoT Hub

    IoT Hub返回以下数据:

    enter image description here

    1. 设备使用Azure Storage SDK将文件上载到存储。您可以关注enter image description here。代码如下所示:

          // 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);
          }
      
    2. 上传完成后,设备会向IoT集线器发送POST,如下所示:

    3. this tutorial

      此外,您可以enter image description here检查上传的文件。 并使用以下代码检查文件上载通知:

          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);
              }
          }