Stack Overflow有很多類似的問題,但是我在通過MVC.net上傳一個大文件到S3上時遇到了問題。用ASP.net將大文件上傳到s3 MVC
基於AWS的.NET SDK中的例子,還有一些其他的答案,我有以下幾點:
public static void UploadToS3(System.IO.Stream stream, string bucketName, string keyName, bool asAttachement)
{
AmazonS3 client;
string accessKey = Settings.ApplicationSettings.AccessKeyID;
string secretKey = Settings.ApplicationSettings.SecretAccessKeyID;
// This config ensures s3 works in medium trust.
AmazonS3Config s3config = new AmazonS3Config();
s3config.UseSecureStringForAwsSecretKey = false;
using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey, s3config))
{
try
{
PutObjectRequest request = new PutObjectRequest();
request.WithBucketName(bucketName)
.WithKey(keyName)
.WithInputStream(stream);
if (asAttachement)
{
// add headers to ensure asset is downloaded, not opened in browser.
request.AddHeaders(Amazon.S3.Util.AmazonS3Util.CreateHeaderEntry("Content-Disposition", "attachment"));
}
S3Response response = client.PutObject(request);
response.Dispose();
}
catch(Exception ex)
{
throw new ApplicationException("Unable to upload asset." + ex.Message);
}
}
}
的Web應用程序與小文件完美的作品,和大文件將上傳到該網站(與託管Rackspace Cloud Sites),但瀏覽器在http post後大約30秒後超時,這個時間不夠長,無法將流上傳到Amazon。瀏覽器超時沒有收到服務器的響應。
我需要做些什麼才能使這項工作適用於大文件,或者我應該如何將文件上傳到S3?
這是正確的,謝謝你的反饋。到Rackspace Cloud站點的客戶端正常工作,但從Rackspace卸載到S3所涉及的時間會導致瀏覽器問題。在超時之前,Chrome只允許大約30秒的傳輸。我目前正在將文件上傳到主機,保存到文件系統和數據庫,並嘗試運行某種後臺線程以轉移到S3。這適用於小文件,但仍然對大文件(30-50MB)有問題。我原以爲這是一個普遍的任務,並且在.net域會有一些樣本。 – Adrian 2011-02-02 04:19:38