You can follow "File uploads with IoT Hub" and do it through four steps:
- 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.
- Initialize a file upload by sending a POST to the IoT hub like this:
And IoT Hub returns the following data:
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);
}
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);
}
}