Skip to content

Instantly share code, notes, and snippets.

@QiMata
Last active May 11, 2020 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QiMata/940a00a94038cdd557e64f3b91767e6d to your computer and use it in GitHub Desktop.
Save QiMata/940a00a94038cdd557e64f3b91767e6d to your computer and use it in GitHub Desktop.
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 https://github.com/Azure/azure-iot-sdk-csharp/blob/master/device/samples/DeviceClientFileUploadSample/Program.cs
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)
{
try
{
SendToBlobSample().Wait();
}
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);
watch.Stop();
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