Connect to IoT Hub without using the Azure Client

2019-02-20 00:28发布

问题:

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

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?

回答1:

If you want to go without the SDKs (which I am curious to know why), you can find all the REST API reference docs here. The specifics about the SAS URI for Storage are here. And for the file upload notifications, it's here. With the authentication + these you should be able to implement File Upload through IoT Hub.



回答2:

Here's my ATWINC1500 Arduino AVR implementation of reading from IoT Hub (amend the endpoint and change to a 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();
  }
}

I just used Device Explorer to generate a 2-year valid SAS key.

I'm pretty sure i can't calculate my own SAS without a real time clock, something the AVR doesn't have.. Oliver can confirm.



回答3:

You can follow "File uploads with IoT Hub" and do it through four steps:

  1. Associate an Azure Storage account with IoT Hub. Here we do this through the Azure portal. Find File upload in your iot hub and select the storage container of your storage account.
  2. Initialize a file upload by sending a POST to the IoT hub like this:

And IoT Hub returns the following data:

  1. The device uploads the file to storage using the Azure Storage SDKs. You can follow this tutorial. The code looks like this:

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


回答4:

Here is an example of a client testing app with source code to interact with Azure Iot Hub without using the SDK. It covers among other things file uploads. It uses the M2MQTT library