1

我正在尝试将一个大文件上传到 Amazon S3。我第一次使用PutObject它,它工作得很好,但上传一个 2GB 的文件大约需要 5 个小时。所以我阅读了一些在线建议并尝试使用TransferUtility.

我增加了超时但是这个 TransferUtility API 总是给我"The request was aborted. The request was canceled."错误。

代码示例:

public void UploadWithMultiPart(string BucketName, string s3_key, string fileName)
{
    var fileTransferUtility = new Amazon.S3.Transfer.TransferUtility(_accessKey, _secretKey);
    var request = new Amazon.S3.Transfer.TransferUtilityUploadRequest()
        .WithBucketName(BucketName)
        .WithKey(s3_key)
        .WithFilePath(fileName)
        .WithTimeout(60*60*1000*100)
        .WithPartSize(1024 * 1024 * 100)
        .WithCannedACL(S3CannedACL.PublicRead)
        .WithStorageClass(S3StorageClass.ReducedRedundancy);

    request.Timeout = 60*60*1000*100;

    fileKey = s3_key;

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

    //.with = 30000
    // .AddHeader("x-amz-acl", "public-read")

    fileTransferUtility.Upload(request);
}

public void Upload(string BucketName, string s3_key, string fileName)
{
    Amazon.S3.Model.PutObjectRequest request = new Amazon.S3.Model.PutObjectRequest();
    request.WithBucketName(BucketName);
    request.WithKey(s3_key);
    request.WithFilePath(fileName);
    request.Timeout = -1;
    request.ReadWriteTimeout = 30000;
    request.AddHeader("x-amz-acl", "public-read"); 

    s3Client.PutObject(request);
}
4

1 回答 1

1

尝试这个

private TransferUtility transferUtility;
transferUtility = new TransferUtility(awsAccessKey, awsSecretKey);

AsyncCallback callback = new AsyncCallback(UploadComplete);


var putObjectRequest = new Amazon.S3.Transfer.TransferUtilityUploadRequest()
{
   FilePath = filePath,
   BucketName = awsBucketName,
   Key = awsFilePath,
   ContentType = contentType,
   StorageClass = S3StorageClass.ReducedRedundancy,
   ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256,
   CannedACL = S3CannedACL.Private
};
IAsyncResult ar = transferUtility.BeginUpload(putObjectRequest, callback, null);
ThreadPool.QueueUserWorkItem(c =>
{
   transferUtility.EndUpload(ar);
});
于 2013-12-30T09:32:09.950 回答