从ASP.NET应用程序直接上传文件到Amazon S3

28
我的ASP.NET MVC应用程序需要大量的带宽和存储空间。我该如何设置一个ASP.NET上传页面,使用户上传的文件直接进入Amazon S3,而不使用我的Web服务器的存储和带宽?
6个回答

27

2016年2月更新:

AWS SDK现在可以更好地处理这方面的事情。请查看如何构建表单如何构建签名。这样做应该可以避免您需要在自己的端点上使用带宽,假设您在将内容发送到S3之前不需要对其进行任何处理。


1
太好了!很难找到一些亚马逊的样例,而这正是我之前需要的。谢谢你。 - 410
5
多年以后,我写了一篇博客,因为最近我不得不设置它。我将为您进行翻译。 - floatingfrisbee
我不理解这个答案;你谈到SDK能够处理它,然后给出两个文档链接,这些链接根本没有提到SDK。那么告诉我们需要哪些NuGet包,要查看哪些对象/方法等如何? - Jez

4
如果您需要上传大文件并显示进度条,您应该考虑使用 flajaxian 组件。它使用 Flash 直接将文件上传到 Amazon S3,节省了带宽。

1
Flajaxian曾经非常好用,我在生产环境中使用了几年。但最近由于Mac设备上的Flash组件存在微妙差异,它变得很容易出现错误。我不再推荐在生产环境中使用这个组件,因为开发者已经停止支持它了。你只需要看看Codeplex上所有未回复的帖子就知道了。 - QFDev

2

通过asp.net将文件上传至亚马逊S3的最佳且最简单的方法。请参考我写的以下博客文章,我认为这篇文章会对您有所帮助。在这篇博客中,我从添加S3存储桶到创建API密钥、安装Amazon SDK和编写上传文件代码等方面进行了说明。以下是使用asp.net C#上传文件至亚马逊S3的示例代码。

using System
using System.Collections.Generic
using System.Linq
using System.Web
using Amazon
using Amazon.S3
using Amazon.S3.Transfer
/// 
/// Summary description for AmazonUploader
/// 
public class AmazonUploader
{
        public bool sendMyFileToS3(System.IO.Stream localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
        {
        // input explained :
        // localFilePath = we will use a file stream , instead of path
        // bucketName : the name of the bucket in S3 ,the bucket should be already created
        // subDirectoryInBucket : if this string is not empty the file will be uploaded to
            // a subdirectory with this name
        // fileNameInS3 = the file name in the S3
    // create an instance of IAmazonS3 class ,in my case i choose RegionEndpoint.EUWest1
    // you can change that to APNortheast1 , APSoutheast1 , APSoutheast2 , CNNorth1
    // SAEast1 , USEast1 , USGovCloudWest1 , USWest1 , USWest2 . this choice will not
    // store your file in a different cloud storage but (i think) it differ in performance
    // depending on your location


        IAmazonS3 client = new AmazonS3Client("Your Access Key", "Your Secrete Key", Amazon.RegionEndpoint.USWest2);

    // create a TransferUtility instance passing it the IAmazonS3 created in the first step
    TransferUtility utility = new TransferUtility(client);
    // making a TransferUtilityUploadRequest instance
    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 = fileNameInS3 ; //file name up in S3
    //request.FilePath = localFilePath; //local file name
    request.InputStream = localFilePath;
    request.CannedACL = S3CannedACL.PublicReadWrite;
    utility.Upload(request); //commensing the transfer

    return true; //indicate that the file was sent
}
}

您可以使用sendMyFileToS3函数将文件流上传到Amazon S3。要了解更多详细信息,请查看以下链接中的我的博客。

通过asp.net上传文件到Amazon S3

希望上述链接能够帮助到您。


这段代码是指付费解决方案,它是否直接上传到Amazon S3桶中? - Learning
是的,它会直接上传到您指定的Amazon S3存储桶中。您只需要放置API凭据和要上传文件的存储桶名称。其余的工作将由代码处理。此详细描述在博客文章中有说明。 - sambit.albus
1
我认为博客链接已经失效(或者网站出现了问题)。你能否检查并更新一下。谢谢! - Prashant Gupta

1
寻找一个JavaScript库来处理客户端上传这些文件。我偶然发现了一个JavaScript和PHP的example,Dojo似乎也提供了客户端的S3文件上传功能。

0

ThreeSharp 是一个在 .NET 环境下方便与 Amazon S3 进行交互的库。

您仍需要在 MVC 应用程序中托管上传和发送文件到 S3 的逻辑,但无需将它们持久化在您的服务器上。


5
ThreeSharp因亚马逊的努力停止了他们的项目。这是他们网站上的消息:"我们建议开发人员尽早迁移到AWS SDK,因为我们将不再维护ThreeSharp库。" - Stradas
@kyle,这是否意味着AWS SDK具有内置功能,可以直接将文件流传输到AWS S3存储桶中... - Learning

0

在 asp.net mvc 中保存和获取 aws s3 存储桶中的数据:

要在 Amazon S3 存储桶中保存纯文本数据。

1.首先,您需要在 AWS 上创建一个存储桶

2.您需要您的 AWS 凭据,如 a)aws 密钥 b) aws 秘密密钥 c) 区域

// 保存数据到 AWS 的代码 // 注意,您可能会遇到访问被拒绝的错误。为了解决这个问题,请检查 AWS 帐户并授予读写权限

需要从 NuGet 包添加名称空间

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

 var credentials = new Amazon.Runtime.BasicAWSCredentials(awsKey, awsSecretKey);          
                try`
                {
                    AmazonS3Client client = new AmazonS3Client(credentials, RegionEndpoint.APSouth1);
                    // simple object put
                    PutObjectRequest request = new PutObjectRequest()
                    {
                        ContentBody = "put your plain text here",
                        ContentType = "text/plain",
                        BucketName = "put your bucket name here",
                        Key = "1"
                        //put unique key to uniquly idenitify your data
                        // you can pass here any data with unique id like primary key 
                        //in db
                    };              
                    PutObjectResponse response = client.PutObject(request);
                   
                }
                catch(exception ex)
                {
                     //
                }

现在转到您的AWS帐户并检查您可以在AWS s3存储桶中使用“1”名称获取数据的存储桶。 注意:- 如果您遇到任何其他问题,请在此处向我提问,我会尽力解决。

从AWS s3存储桶获取数据的方法:

 try
            {              
                var credentials = new Amazon.Runtime.BasicAWSCredentials(awsKey, awsSecretKey);
                AmazonS3Client client = new AmazonS3Client(credentials, RegionEndpoint.APSouth1);
                GetObjectRequest request = new GetObjectRequest()
                {
                    BucketName = bucketName,
                    Key = "1"// because we pass 1 as unique key while save 
                            //data at the s3 bucket
                };

                using (GetObjectResponse response = client.GetObject(request))
                {
                    StreamReader reader = new 
                 StreamReader(response.ResponseStream);
                    vccEncryptedData = reader.ReadToEnd();
                }
            }
            catch (AmazonS3Exception)
            {
                throw;
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接