Amazon S3 Async Upload using Task library

别来无恙 提交于 2019-12-21 21:20:02

问题


I have a windows form that upload file to Amazon S3. I tried to implement built in async method but seems not working fine so I think the best way would be to implement System.Threading.Tasks.

My actual code looks like this:

public void UploadFileAsync(string bucketName, CloudDocument doc, bool publicRead)
{
config = new AmazonS3Config();
            config.CommunicationProtocol = Protocol.HTTP;
            client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID, config);

// Load stream from file location
            FileMode mode = FileMode.Open;
            using (FileStream fs = new FileStream(doc.FullName, mode, FileAccess.Read))
            {
                // Create put object request
                TransferUtilityUploadRequest objectRequest = new TransferUtilityUploadRequest();
                objectRequest.InputStream = fs;
                objectRequest.BucketName = bucketName;

                if (publicRead) objectRequest.CannedACL = S3CannedACL.PublicRead;

                objectRequest.Key = doc.KeyName + doc.FileName.Replace(' ', '_');

                objectRequest.UploadProgressEvent += new EventHandler<UploadProgressArgs>(UploadProgressEvent);

                transferUtility = new TransferUtility(client);
                IAsyncResult asyncResult = transferUtility.BeginUpload(objectRequest, new AsyncCallback(UploadCallBack), results);

                waitHandles.Add(asyncResult.AsyncWaitHandle);



                // Wait till all the requests that were started are completed.
                WaitHandle.WaitAll(waitHandles.ToArray());
            }

            client.Dispose();

        }

}
private void UploadProgressEvent(object sender, UploadProgressArgs e)
        {
            if (UploadProgressChanged != null)
                UploadProgressChanged(this, e);
        }

private void UploadCallBack(IAsyncResult result)
        {

            Results results = result.AsyncState as Results;
            try
            {

                // If there was an error during the put attributes operation it will be thrown as part of the EndPutAttributes method.
                transferUtility.EndUpload(result);

                results.Successes++;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                results.Errors++;
            }
        }

Have anyone tried to implement Await / Async Task in order to upload async to amazon s3?


回答1:


You can find instructions for wrapping APM and EAP members into TAP members on MSDN.

In summary, you use Task.Factory.FromAsync to wrap Begin/End method pairs, something like this:

public static Task UploadAsync(this TransferUtility @this, TransferUtilityUploadRequest request)
{
    return Task.Factory.FromAsync(@this.BeginUpload, @this.EndUpload, request, null);
}

Then you can use it as such:

var task = transferUtility.UploadAsync(objectRequest);
tasks.Add(task);
await Task.WhenAll(tasks);



回答2:


You may also be interested in the Developer Preview of the AWS SDK 2.0 for .NET. The source for the async extension class for the TransferUtility can be found here. Let us know if you have any feedback on the preview!

Thanks!



来源:https://stackoverflow.com/questions/18807204/amazon-s3-async-upload-using-task-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!