Put an object in Amazon S3 using .net async

I want to convert AWS S3 asynchronous methods to a task using something like this:

using (var client = AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey)) { var request = new PutObjectRequest(); // ... set request properties ... await Task.Factory.FromAsync<PutObjectRequest, PutObjectResponse>( client.BeginPutObject, client.EndPutObject, request, null ); } 

However, I get the following exception:

 System.ArgumentException: The IAsyncResult object was not returned from the corresponding asynchronous method on this class. Parameter name: asyncResult at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at Amazon.S3.AmazonS3Client.getResponseCallback[T](IAsyncResult result) at Amazon.S3.AmazonS3Client.endOperation[T](IAsyncResult result) at Amazon.S3.AmazonS3Client.EndPutObject(IAsyncResult asyncResult) at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization) 

Is my FromAsync call FromAsync or is something else wrong?

PS

  • .NET Framework 4.5
  • AWSSDK version 1.5.17.0
+6
source share
1 answer

I have the same problem. Your FromAsync answer is correct. The same problem occurs when calling BeginPutObject / EndPutObject directly without the FromAsync shell.

The synchronous method AmazonS3Client.PutObject () has this body:

 IAsyncResult asyncResult; asyncResult = invokePutObject(request, null, null, true); return EndPutObject(asyncResult); 

While AmazonS3Client.BeginPutObject says:

 return invokePutObject(request, callback, state, false); 

Pay attention to the last boolean parameter invokePutObject. This argument is called "synchronized." If you call it with synchronized = true, it works (by performing the operation synchronously). If you call it with synchronized = false, during parallel loading you will get the exception that you sent.

This is clearly a bug in the AWS SDK, which needs further investigation. This post on AWS forums looks similar, but it may not be the same problem; I am not satisfied with the response to the upstream there, since simultaneous synchronous downloads work.

ETA . The new AWS SDK version 2.0 (in beta at the time of writing), which requires .Net 4.5, has its own FooAsync methods (insted of Begin / EndFoo). It is based on the new System.Net.HttpClient library instead of the old HttpWebRequest. It almost certainly does not have this error, but I have not tested it myself yet.

+4
source

All Articles