Skip to content

Instantly share code, notes, and snippets.

Last active May 11, 2020 10:10
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A sample of uploading files with Azure IoT Hub C# SDK
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// From the microsoft samples here
using System;
using System.IO;
using System.Threading.Tasks;
namespace Microsoft.Azure.Devices.Client.Samples
class Program
// String containing Hostname, Device Id & Device Key in one of the following formats:
// "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
// "HostName=<iothub_host_name>;CredentialType=SharedAccessSignature;DeviceId=<device_id>;SharedAccessSignature=SharedAccessSignature sr=<iot_host>/devices/<device_id>&sig=<token>&se=<expiry_time>";
private const string DeviceConnectionString = "<replace>";
private const string FilePath = "<replace>";
static void Main(string[] args)
catch (Exception ex)
Console.WriteLine("{0}\n", ex.Message);
if (ex.InnerException != null)
Console.WriteLine(ex.InnerException.Message + "\n");
static async Task SendToBlobSample()
var deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Http1);
var fileStreamSource = new FileStream(FilePath, FileMode.Open);
var fileName = Path.GetFileName(fileStreamSource.Name);
Console.WriteLine("Uploading File: {0}", fileName);
var watch = System.Diagnostics.Stopwatch.StartNew();
await deviceClient.UploadToBlobAsync(fileName, fileStreamSource);
Console.WriteLine("Time to upload file: {0}ms\n", watch.ElapsedMilliseconds);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment