-
Notifications
You must be signed in to change notification settings - Fork 1
/
Uploader.cs
38 lines (30 loc) · 1.23 KB
/
Uploader.cs
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
using System.Net;
using Newtonsoft.Json;
using System.Text;
using Amazon.Lambda.Core;
using System.IO;
namespace CFN_CustomResource
{
public class Uploader
{
// Class to upload response JSON to the pre-signed URL
public static void UploadResponse(string url, cfnResponse response)
{
string json = JsonConvert.SerializeObject(response);
byte[] byteArray = Encoding.UTF8.GetBytes(json);
LambdaLogger.Log($"trying to upload json {json}");
HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
httpRequest.Method = "PUT";
//If ContentType is set to anything but "" your upload will fail
httpRequest.ContentType = "";
httpRequest.ContentLength = byteArray.Length;
LambdaLogger.Log($"Starting upload of {byteArray.Length}");
using (Stream datastream = httpRequest.GetRequestStream())
{
datastream.Write(byteArray,0,byteArray.Length);
}
HttpWebResponse result = httpRequest.GetResponse() as HttpWebResponse;
LambdaLogger.Log($"Result of upload is {result.StatusCode}");
}
}
}