【问题标题】:how to get response from AWS S3 uploader如何从 AWS S3 上传器获取响应
【发布时间】:2017-11-20 10:48:54
【问题描述】:
我正在使用 AWS S3 来存储文件。
我能够上传和删除文件。
唯一的问题是上传大文件时,如何知道上传了多少,我需要像进度条一样实现。
请帮我做。
IAmazonS3 client = new AmazonS3Client(RegionEndpoint.USEast2);
TransferUtility utility = new TransferUtility(client);
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
{
request.BucketName = bucketName; //no subdirectory just bucket name
}
else
{ // subdirectory and bucket name
request.BucketName = bucketName + @"/" + subDirectoryInBucket;
}
request.Key = RandomString()+ Path.GetExtension(e.File.FileName); //file name up in S3
request.InputStream = e.File.InputStream;
request.CannedACL=S3CannedACL.PublicRead;
utility.Upload(request); //commensing the transfer
我正在使用此代码。
【问题讨论】:
标签:
jquery
asp.net-mvc
amazon-web-services
model-view-controller
amazon-s3
【解决方案1】:
您好,您可以调用 TransferUtilityUploadRequest 方法,
下面是例子,你也可以参考这个link!希望这会有所帮助
using System;
using System.Collections.Specialized;
using System.Configuration;
using Amazon.S3;
using Amazon.S3.Transfer;
namespace s3.amazon.com.docsamples
{
class TrackMPUUsingHighLevelAPI
{
static string existingBucketName = "*** Provide bucket name ***";
static string keyName = "*** Provide key name ***";
static string filePath = "*** Provide file to upload ***";
static void Main(string[] args)
{
try
{
TransferUtility fileTransferUtility =
new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));
// Use TransferUtilityUploadRequest to configure options.
// In this example we subscribe to an event.
TransferUtilityUploadRequest uploadRequest =
new TransferUtilityUploadRequest
{
BucketName = existingBucketName,
FilePath = filePath,
Key = keyName
};
uploadRequest.UploadProgressEvent +=
new EventHandler<UploadProgressArgs>
(uploadRequest_UploadPartProgressEvent);
fileTransferUtility.Upload(uploadRequest);
Console.WriteLine("Upload completed");
}
catch (AmazonS3Exception e)
{
Console.WriteLine(e.Message, e.InnerException);
}
}
static void uploadRequest_UploadPartProgressEvent(
object sender, UploadProgressArgs e)
{
// Process event.
Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes);
}
}
}