public void UploadToAmazon(PreSignedUrl presignedUrl, byte[] imageData) { using (System.IO.MemoryStream fileStream = new System.IO.MemoryStream(imageData)) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(presignedUrl.signed_url); request.Method = "PUT"; request.ContentType = "application/octet-stream"; request.Timeout = -1; //Infinite wait for the response. request.AllowWriteStreamBuffering = false; request.ContentLength = fileStream.Length; // Create 32KB buffer which is file page size. byte[] tempBuffer = new byte[1024 * 64]; int bytesRead = 0; int totalBytesRead = 0; // Write the source data to the network stream. Stream requestStream = request.GetRequestStream(); // Loop till the file content is read completely. while ((bytesRead = fileStream.Read(tempBuffer, 0, tempBuffer.Length)) > 0) { totalBytesRead += bytesRead; // Write the 8 KB data in the buffer to the network stream. requestStream.Write(tempBuffer, 0, bytesRead); } requestStream.Close(); WebResponse response = request.GetResponse(); } }
protected async Task <UploadedFile> UploadOccasionImage(HavenSDK sdk, Guid faction_id, string image_url) { byte[] original = this.GetImageDataFromUrl(image_url); if (original != null) { // -- Retrieve an endpoint to upload to [ensure the verb and mimetype match your actual uplaoder] PreSignedUrl uploadLocation = await sdk.Media.GetTemporaryUploadUrl(AssetType.Image, MediaTarget.faction, System.IO.Path.GetFileName(image_url), faction_id, faction_id, "PUT", "application/octet-stream").DemoUnPack(); // -- Upload the file this.UploadToAmazon(uploadLocation, original); // -- Return the info return(new UploadedFile() { id = uploadLocation.id, name = System.IO.Path.GetFileName(image_url), url = uploadLocation.url }); } return(null); }