我的ASP.NET MVC应用程序需要大量的带宽和存储空间。我该如何设置一个ASP.NET上传页面,使用户上传的文件直接进入Amazon S3,而不使用我的Web服务器的存储和带宽?
通过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。要了解更多详细信息,请查看以下链接中的我的博客。
希望上述链接能够帮助到您。
ThreeSharp 是一个在 .NET 环境下方便与 Amazon S3 进行交互的库。
您仍需要在 MVC 应用程序中托管上传和发送文件到 S3 的逻辑,但无需将它们持久化在您的服务器上。
在 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;
}