-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (100 loc) · 4.49 KB
/
Program.cs
File metadata and controls
131 lines (100 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Client.Transport;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.IO;
using System.Text;
namespace device_upload
{
class Program
{
static string DeviceConnectionString = "";
static DeviceClient Client = null;
static FileUploadSasUriResponse sasUploadUri = null;
static Uri fileUploadUri = null;
static async Task Main(string[] args)
{
Console.WriteLine("*************************************************");
Console.WriteLine("Welcome to the Azure IoT Hub Device Upload Tester");
Console.WriteLine();
Console.WriteLine("Author: Pete Gallagher");
Console.WriteLine("Twitter: @pete_codes");
Console.WriteLine("Date: 22nd December 2020");
Console.WriteLine();
Console.WriteLine("*************************************************");
Console.WriteLine();
try
{
Console.WriteLine("Enter the Device Connection String");
DeviceConnectionString = Console.ReadLine();
InitClient();
await UploadFile();
}
catch (System.Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
}
public static void InitClient()
{
try
{
Console.WriteLine("Connecting to hub");
Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, Microsoft.Azure.Devices.Client.TransportType.Mqtt);
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
}
public static async Task UploadFile() {
Console.WriteLine("Uploading test.csv to the IoT Hub");
var fileName = "test.csv";
await GetFileUploadUri(fileName);
await UploadFileToBlobStorage(fileName);
}
private static async Task GetFileUploadUri(String fileName) {
var fileUploadSasUriRequest = new FileUploadSasUriRequest();
fileUploadSasUriRequest.BlobName = fileName;
Console.WriteLine("Retrieving SAS URI for File Upload from the IoT Hub");
sasUploadUri = await Client.GetFileUploadSasUriAsync(fileUploadSasUriRequest);
fileUploadUri = new Uri($"https://{sasUploadUri.HostName}/{sasUploadUri.ContainerName}/{Uri.EscapeDataString(sasUploadUri.BlobName)}{sasUploadUri.SasToken}");
Console.WriteLine($"Successfully retrieved SAS URI ({fileUploadUri}) for File upload from IoT Hub");
}
private static async Task UploadFileToBlobStorage(String fileName) {
using var fileContentsStream = new FileStream(fileName, FileMode.Open);
try
{
Console.WriteLine($"Uploading file {fileName} to Blob Storage");
var blob = new CloudBlockBlob(fileUploadUri);
await blob.UploadFromStreamAsync(fileContentsStream);
}
catch (Exception ex)
{
Console.WriteLine($"File Upload Failed {ex.ToString()} - Sending Failure Notification to IoT Hub");
await SendFailureNotification(ex.Message);
}
Console.WriteLine("File Uploaded Successfully - Sending Success Notification to IoT Hub");
}
private static async Task SendSuccessNotification() {
var successfulUploadNotification = new FileUploadCompletionNotification();
successfulUploadNotification.CorrelationId = sasUploadUri.CorrelationId;
successfulUploadNotification.IsSuccess = false;
successfulUploadNotification.StatusCode = 200;
successfulUploadNotification.StatusDescription = "Success";
await Client.CompleteFileUploadAsync(successfulUploadNotification);
}
private static async Task SendFailureNotification(String exceptionMessage) {
var failedUploadNotification = new FileUploadCompletionNotification();
failedUploadNotification.CorrelationId = sasUploadUri.CorrelationId;
failedUploadNotification.IsSuccess = false;
failedUploadNotification.StatusCode = 500;
failedUploadNotification.StatusDescription = exceptionMessage;
await Client.CompleteFileUploadAsync(failedUploadNotification);
}
}
}