Amazon.S3.AmazonS3Client.PostObjectAsync C# (CSharp) Метод

PostObjectAsync() публичный Метод

Upload data to Amazon S3 using HTTP POST.
For more information,
public PostObjectAsync ( PostObjectRequest request, PostObjectResponse>.AmazonServiceCallback callback, AsyncOptions options = null ) : void
request PostObjectRequest Request object which describes the data to POST
callback PostObjectResponse>.AmazonServiceCallback An Action delegate that is invoked when the operation completes.
options AsyncOptions A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.
Результат void
        public void PostObjectAsync(PostObjectRequest request, AmazonServiceCallback<PostObjectRequest, PostObjectResponse> callback, AsyncOptions options = null)
        {
            options = options == null ? new AsyncOptions() : options;

            Action<AmazonWebServiceRequest, AmazonWebServiceResponse, Exception, AsyncOptions> callbackHelper
                = (AmazonWebServiceRequest req, AmazonWebServiceResponse res, Exception ex, AsyncOptions ao) =>
                {
                    AmazonServiceResult<PostObjectRequest, PostObjectResponse> responseObject
                        = new AmazonServiceResult<PostObjectRequest, PostObjectResponse>((PostObjectRequest)req, (PostObjectResponse)res, ex, ao.State);
                    if (callback != null)
                        callback(responseObject);
                };

            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
            {
                // Provide a default policy if user doesn't set it.
                try
                {
                    InferContentType(request);
                    if (request.SignedPolicy == null)
                    {
                        CreateSignedPolicy(request);
                    }
                    PostObject(request, options, callbackHelper);
                }
                catch (Exception e)
                {
                    callback(new AmazonServiceResult<PostObjectRequest, PostObjectResponse>(request, null, e, options.State));
                }
            }));

        }

Usage Example

Пример #1
0
 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;
     }
 }
AmazonS3Client