public async Task <PresignedURL> PerformPresignedURLFetch(Tokens tokens, string bucketName, string deviceID, string fileExt, string patientName, string doctorUsername, string fileName) { using (var httpClient = new HttpClient { BaseAddress = new Uri(config.BasePath) }) { HashAlgorithm hashAlgorithm = MD5.Create(); StringBuilder sb = new StringBuilder(); var hashArray = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(fileName)); foreach (byte b in hashArray) { sb.Append(b); } var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); var presignedURLInfo = new PresignedURLPostInfo() { BucketName = bucketName, DeviceID = deviceID, FileExtension = fileExt, PatientName = patientName, PatientDoctorUserName = doctorUsername, FileName = sb.ToString() }; var jsonPresignedURLInfo = JsonConvert.SerializeObject(presignedURLInfo); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", tokens.AccessToken); var content = new StringContent(jsonPresignedURLInfo.ToString(), Encoding.UTF8, "application/json"); using (var resp = await httpClient.PostAsync(endpoint, content)) { if (resp.IsSuccessStatusCode) { var presignedURL = await resp.Content.ReadAsStringAsync(); PresignedURL url = JsonConvert.DeserializeObject <PresignedURL>(presignedURL); return(url); } else { return(null); } } } }
public static async Task <bool> PerformUploadS3DirectAsync(Stream stream, PresignedURL url) { HttpWebRequest httpRequest = WebRequest.Create(url.UploadURL) as HttpWebRequest; httpRequest.Method = "PUT"; using (Stream uploadStream = httpRequest.GetRequestStream()) { var buffer = new byte[1000000]; int bytesRead = 0; while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) { await uploadStream.WriteAsync(buffer, 0, bytesRead); } } HttpWebResponse response = await httpRequest.GetResponseAsync() as HttpWebResponse; return(response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.NoContent); }