C# 不使用Azure客户端SDK连接到IoT集线器

C# 不使用Azure客户端SDK连接到IoT集线器,c#,azure,azure-iot-hub,C#,Azure,Azure Iot Hub,我想不使用客户端SDK连接到Azure Iot集线器。 在…上 有关于如何通过以下方式执行此操作的文档: 1) 获取存储的SAS URI 2) 通知物联网中心已完成上传 但在此之前,您需要使用DeviceConnectionString连接到IoT集线器。有没有人有这样做和上传文件的示例/提示?如果您不想使用SDK(我很想知道为什么),您可以找到所有REST API参考文档。 有关存储的SAS URI的详细信息如下。 对于文件上传通知,它是。 通过认证+这些,您应该能够通过IoT Hub实现文件

我想不使用客户端SDK连接到Azure Iot集线器。 在…上 有关于如何通过以下方式执行此操作的文档: 1) 获取存储的SAS URI
2) 通知物联网中心已完成上传


但在此之前,您需要使用DeviceConnectionString连接到IoT集线器。有没有人有这样做和上传文件的示例/提示?

如果您不想使用SDK(我很想知道为什么),您可以找到所有REST API参考文档。 有关存储的SAS URI的详细信息如下。 对于文件上传通知,它是。 通过认证+这些,您应该能够通过IoT Hub实现文件上传。

以下是我的Arduino AVR从IoT Hub读取的实现(修改端点并更改为帖子):

我只是用来生成一个2年有效的SAS密钥

我很确定,如果没有实时时钟,我无法计算自己的SAS,而AVR没有这一点。。可以确认。

您可以按照“”进行操作,并通过四个步骤完成:

  • 将Azure存储帐户与物联网中心关联。在这里,我们通过Azure门户进行此操作。在iot中心中查找文件上载,并选择存储帐户的存储容器
  • 通过向物联网中心发送帖子初始化文件上载,如下所示:
  • IoT Hub返回以下数据:

  • 设备使用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);
        }
    
  • 上传完成后,设备会向物联网中心发送POST,如下所示:

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

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

        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);
            }
        }
    
    是一个客户端测试应用程序的示例,其源代码用于在不使用SDK的情况下与Azure Iot Hub交互。其中包括文件上传。它使用


    有些人想知道,为什么不使用SDK就要这么做。事实上,并不是所有的物联网设备都支持SDK。

    事实上,我认为@w00zert询问的是用于文件上传的Azure存储SAS密钥,而不是用于建立与Azure物联网的设备连接的SAS令牌。他似乎想知道这两个问题,@w00zert,你能澄清一下你的目的吗?更重要的是,您针对什么设备编写代码,并且无法利用SDK?@oliverbloch“我很想知道为什么”并非所有物联网设备都与SDK兼容。请解释您为什么投了否决票。我的答案回答了这个问题,这个问题是一个常见的用例。