5

AWS バケットにアップロードされていないファイルを大量に取得していますが、以下のコードは常に完了を示しています。これどうなったか知ってる人いますか?アップロードが常に完了して戻ってくる場合、アップロードが失敗したかどうかを確認するにはどうすればよいですか? transferutility.EndUpload の使用について何か見ましたが、使用方法がわかりません。また、再試行をどのように実装しますか? オブジェクトの状態を BeginUpload に渡しますか? 何か助けはありますか?

public class S3Upload
{
    private string awsAccessKeyId = "XXXXXX";
    private string awsSecretAccessKey = "XXXXXX";
    private string bucketName = System.Configuration.ConfigurationManager.AppSettings.Get("BucketName");
    private Amazon.S3.Transfer.TransferUtility transferUtility;
    private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public S3Upload()
    {
        // Initialize log4net.
        log4net.Config.XmlConfigurator.Configure();
        this.transferUtility = new Amazon.S3.Transfer.TransferUtility(awsAccessKeyId, awsSecretAccessKey);
        log.Info("S3 instance initiated");

    }

    public void UploadFile(string filePath, string toPath)
    {

        try
        {
            AsyncCallback callback = new AsyncCallback(uploadComplete);

            log.Info("S3 upload started...");
            log.InfoFormat("S3 filePath: {0}", filePath);
            log.InfoFormat("S3 toPath: {0}", toPath);


            var uploadRequest = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
            uploadRequest.FilePath = filePath;
            uploadRequest.BucketName = bucketName;
            uploadRequest.Key = toPath;
            uploadRequest.StorageClass = Amazon.S3.Model.S3StorageClass.ReducedRedundancy;
            uploadRequest.AddHeader("x-amz-acl", "public-read");
            transferUtility.BeginUpload(uploadRequest, callback, null);
        }
        catch (AmazonS3Exception amazonS3Exception)
        {
              log.ErrorFormat("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);    
        }
    }

    private void uploadComplete(IAsyncResult result)
    {
        var x = result;

        if (x.IsCompleted)
        {
            log.Info("S3 upload completed...");

        }
    }
}
4

2 に答える 2

2

修正が見つかりました!transferUtility.EndUpload(ar) を追加しました。エラーが発生した場合は、ログに例外を書き込み、put リクエストを再試行します。

リクエスト PutObject の作成中にエラーが発生しました。System.IO.IOException: トランスポート接続にデータを書き込めません: 既存の接続がリモート ホストによって強制的に閉じられました。--->

public class S3Upload
{
    private string awsAccessKeyId = "XXXXX";
    private string awsSecretAccessKey = "XXXXX";
    private string bucketName = System.Configuration.ConfigurationManager.AppSettings.Get("BucketName");
    private Amazon.S3.Transfer.TransferUtility transferUtility;
    private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public S3Upload()
    {
        // Initialize log4net.
        log4net.Config.XmlConfigurator.Configure();
        this.transferUtility = new Amazon.S3.Transfer.TransferUtility(awsAccessKeyId, awsSecretAccessKey);
        log.Info("S3 instance initiated");

    }

    public void UploadFile(string filePath, string toPath)
    {

        try
        {
            AsyncCallback callback = new AsyncCallback(uploadComplete);

            log.Info("S3 upload started...");
            log.InfoFormat("S3 filePath: {0}", filePath);
            log.InfoFormat("S3 toPath: {0}", toPath);


            var uploadRequest = new Amazon.S3.Transfer.TransferUtilityUploadRequest();
            uploadRequest.FilePath = filePath;
            uploadRequest.BucketName = bucketName;
            uploadRequest.Key = toPath;
            uploadRequest.StorageClass = Amazon.S3.Model.S3StorageClass.ReducedRedundancy;
            uploadRequest.AddHeader("x-amz-acl", "public-read");
            IAsyncResult ar = transferUtility.BeginUpload(uploadRequest, callback, null);
            transferUtility.EndUpload(ar);
        }
        catch (AmazonS3Exception amazonS3Exception)
        {
              log.ErrorFormat("An Error, number {0}, occurred when creating a bucket with the message '{1}", amazonS3Exception.ErrorCode, amazonS3Exception.Message);
        }
    }

    private void uploadComplete(IAsyncResult result)
    {
        var x = result;
    }
}
于 2012-11-25T17:13:38.427 に答える