Skip to content

Latest commit

 

History

History
183 lines (130 loc) · 9.94 KB

iot-hub-csharp-csharp-file-upload.md

File metadata and controls

183 lines (130 loc) · 9.94 KB
title description services documentationcenter author manager editor ms.assetid ms.service ms.devlang ms.topic ms.tgt_pltfrm ms.workload ms.date ms.author
Upload files from devices using Azure IoT Hub | Microsoft Docs
How to upload files from a device to the cloud using Azure IoT device SDK for .NET. Uploaded files are stored in an Azure storage blob container.
iot-hub
.net
fsautomata
timlt
4759d229-f856-4526-abda-414f8b00a56d
iot-hub
dotnet
article
na
na
11/16/2016
elioda

Upload files from devices to the cloud with IoT Hub

Introduction

Azure IoT Hub is a fully managed service that enables reliable and secure bi-directional communications between millions of devices and a solution back end. Previous tutorials (Get started with IoT Hub and Send Cloud-to-Device messages with IoT Hub) illustrate the basic device-to-cloud and cloud-to-device messaging functionality of IoT Hub, and the Process Device-to-Cloud messages tutorial describes a way to reliably store device-to-cloud messages in Azure blob storage. However, in some scenarios you cannot easily map the data your devices send into the relatively small device-to-cloud messages that IoT Hub accepts. Examples include large files that contain images, videos, vibration data sampled at high frequency, or that contain some form of preprocessed data. These files are typically batch processed in the cloud using tools such as Azure Data Factory or the Hadoop stack. When file uploads from a device are preferred to sending events, you can still use IoT Hub security and reliability functionality.

This tutorial builds on the code in the Send Cloud-to-Device messages with IoT Hub tutorial to show you how to use the file upload capabilities of IoT Hub. It shows you how to:

  • Securely provide a device with an Azure blob URI for uploading a file.
  • Use the IoT Hub file upload notifications to trigger processing the file in your app back end.

At the end of this tutorial you run two .NET console apps:

  • SimulatedDevice, a modified version of the app created in the Send Cloud-to-Device messages with IoT Hub tutorial. This app uploads a file to storage using a SAS URI provided by your IoT hub.
  • ReadFileUploadNotification, which receives file upload notifications from your IoT hub.

Note

IoT Hub supports many device platforms and languages (including C, Java, and Javascript) through Azure IoT device SDKs. Refer to the Azure IoT Developer Center for step-by-step instructions on how to connect your device to Azure IoT Hub.

To complete this tutorial, you need the following:

  • Microsoft Visual Studio 2015,
  • An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)

Associate an Azure Storage account to IoT Hub

Because the simulated device app uploads a file to a blob, you must have an Azure Storage account associated to IoT Hub. When you associate an Azure Storage account with an IoT hub, the IoT hub can generate a SAS URI that a device can use to securely upload a file to a blob container. The IoT Hub service and the device SDKs coordinate the process that generates the SAS URI and makes it available to a device to use to upload a file.

Follow the instructions in Configure file uploads using the Azure portal to associate an Azure Storage account to your IoT hub.

Upload a file from a simulated device app

In this section, you modify the simulated device app you created in Send Cloud-to-Device messages with IoT Hub to receive cloud-to-device messages from the IoT hub.

  1. In Visual Studio, right-click the SimulatedDevice project, click Add, and then click Existing Item. Navigate to an image file and include it in your project. This tutorial assumes the image is named image.jpg.

  2. Right-click on the image, and then click Properties. Make sure that Copy to Output Directory is set to Copy always.

  3. In the Program.cs file, add the following statements at the top of the file:

     using System.IO;
    
  4. Add the following method to the Program class:

     private static async void SendToBlobAsync()
     {
         string fileName = "image.jpg";
         Console.WriteLine("Uploading file: {0}", fileName);
         var watch = System.Diagnostics.Stopwatch.StartNew();
    
         using (var sourceData = new FileStream(@"image.jpg", FileMode.Open))
         {
             await deviceClient.UploadToBlobAsync(fileName, sourceData);
         }
    
         watch.Stop();
         Console.WriteLine("Time to upload file: {0}ms\n", watch.ElapsedMilliseconds);
     }
    

    The UploadToBlobAsync method takes in the file name and stream source of the file to be uploaded and handles the upload to storage. The console app displays the time it takes to upload the file.

  5. Add the following method in the Main method, right before the Console.ReadLine() line:

     SendToBlobAsync();
    

Note

For simplicity's sake, this tutorial does not implement any retry policy. In production code, you should implement retry policies (such as exponential backoff), as suggested in the MSDN article Transient Fault Handling.

Receive a file upload notification

In this section, you write a .NET console app that receives file upload notification messages from IoT Hub.

  1. In the current Visual Studio solution, create a Visual C# Windows project by using the Console Application project template. Name the project ReadFileUploadNotification.

    New project in Visual Studio

  2. In Solution Explorer, right-click the ReadFileUploadNotification project, and then click Manage NuGet Packages.

    This action displays the Manage NuGet Packages window.

  3. Search for Microsoft.Azure.Devices, click Install, and accept the terms of use.

    This action downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package in the ReadFileUploadNotification project.

  4. In the Program.cs file, add the following statements at the top of the file:

     using Microsoft.Azure.Devices;
    
  5. Add the following fields to the Program class. Substitute the placeholder value with the IoT hub connection string from Get started with IoT Hub:

     static ServiceClient serviceClient;
     static string connectionString = "{iot hub connection string}";
    
  6. Add the following method to the Program class:

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

    Note that the receive pattern is the same one used to receive cloud-to-device messages from the device app.

  7. Finally, add the following lines to the Main method:

     Console.WriteLine("Receive file upload notifications\n");
     serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
     ReceiveFileUploadNotificationAsync().Wait();
     Console.ReadLine();
    

Run the applications

Now you are ready to run the applications.

  1. In Visual Studio, right-click your solution, and select Set StartUp projects. Select Multiple startup projects, then select the Start action for ReadFileUploadNotification and SimulatedDevice.

  2. Press F5. Both applications should start. You should see the upload completed in one console app and the upload notification message being received by the other console app. You can use the Azure portal or Visual Studio Server Explorer to check for the presence of the uploaded file in your Azure Storage account.

Next steps

In this tutorial, you learned how to leverage the file upload capabilities of IoT Hub to simplify file uploads from devices. You can continue to explore IoT hub features and scenarios with the following articles:

To further explore the capabilities of IoT Hub, see: