2

ファイルを Amazon S3 にアップロードする Windows フォームがあります。組み込みの非同期メソッドを実装しようとしましたが、うまく機能していないようですので、System.Threading.Tasks を実装するのが最善の方法だと思います。

私の実際のコードは次のようになります。

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++;
            }
        }

Amazon s3 に非同期をアップロードするために、Await / Async Task を実装しようとした人はいますか?

4

2 に答える 2

4

APM および EAP メンバーを TAP メンバーにラップする手順については、MSDN を参照してください。

要約すると、次のような/メソッドのペアTask.Factory.FromAsyncをラップするために使用します。BeginEnd

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

その後、次のように使用できます。

var task = transferUtility.UploadAsync(objectRequest);
tasks.Add(task);
await Task.WhenAll(tasks);
于 2013-09-15T15:31:46.597 に答える
1

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!

于 2013-09-18T17:18:27.153 に答える