private void PostResponseHelper(IAsyncResult result) { IAsyncExecutionContext executionContext = result.AsyncState as IAsyncExecutionContext; IWebResponseData response = executionContext.ResponseContext.HttpResponse; RuntimeAsyncResult asyncResult = executionContext.ResponseContext.AsyncResult as RuntimeAsyncResult; if (executionContext.ResponseContext.AsyncResult.Exception == null) { PostObjectResponse postResponse = new PostObjectResponse(); postResponse.HttpStatusCode = response.StatusCode; postResponse.ContentLength = response.ContentLength; if (response.IsHeaderPresent(HeaderKeys.XAmzRequestIdHeader)) { postResponse.RequestId = response.GetHeaderValue(HeaderKeys.XAmzRequestIdHeader); } if (response.IsHeaderPresent(HeaderKeys.XAmzId2Header)) { postResponse.HostId = response.GetHeaderValue(HeaderKeys.XAmzId2Header); } if (response.IsHeaderPresent(HeaderKeys.XAmzVersionIdHeader)) { postResponse.VersionId = response.GetHeaderValue(HeaderKeys.XAmzVersionIdHeader); } PostObjectRequest request = executionContext.RequestContext.OriginalRequest as PostObjectRequest; asyncResult.Request = request; asyncResult.Response = postResponse; } asyncResult.Exception = executionContext.ResponseContext.AsyncResult.Exception; asyncResult.Action = executionContext.RequestContext.Action; asyncResult.InvokeCallback(); }
public static PostObjectResponse PostObjectHelper(AmazonS3Client client, string bucketName, string key, PostObjectRequestManipulator manipulator = null) { using (MemoryStream stream = new MemoryStream()) { PostObjectResponse r = null; Exception responseException = null; StreamWriter writer = new StreamWriter(stream); writer.Write(TestContent); writer.Flush(); stream.Position = 0; AutoResetEvent ars = new AutoResetEvent(false); var request = new PostObjectRequest() { Bucket = bucketName, Key = key, InputStream = stream, }; if (manipulator != null) { manipulator(request); } client.PostObjectAsync(request, (response) => { responseException = response.Exception; if (responseException == null) { r = response.Response; } else { Debug.LogWarning(new StreamReader((responseException as Amazon.Runtime.Internal.HttpErrorResponseException).Response.ResponseBody.OpenResponse()).ReadToEnd()); } ars.Set(); }, new AsyncOptions { ExecuteCallbackOnMainThread = false }); ars.WaitOne(); if (responseException != null) { throw responseException; } return(r); } }