AWS S3 transferutility 上传从不上传,但显示完整且没有错误

naw*_*rus 5 c# amazon-s3 amazon-web-services

我收到了很多没有上传到我的 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...");

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

naw*_*rus 2

找到修复方法了!我添加了transferUtility.EndUpload(ar),如果出现错误,它会将异常写入我的日志并重试放置请求。

发出请求 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;
    }
}
Run Code Online (Sandbox Code Playgroud)