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