Amazon S3 .NET Core如何上传文件
我想在.NET Core项目中使用Amazon S3上传文件。有没有关于如何创建和使用AmazonS3客户端的参考?我在AmazonS3文档中找到的所有.Net Core都是这样(http://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-netcore.html),这不是非常有用。Amazon S3 .NET Core如何上传文件
每AWS SDK文档,在2016年后期
https://aws.amazon.com/sdk-for-net/
所以指令增加了对文件上传到S3应该是相同的为.NET任何其他指令对.NET核心的支持。
“getting started" guide for the AWS SDK for .Net实际上就是您描述的将文件连接并上传到S3的情况 - 并且如果您安装了”AWS Toolkit for Visual Studio“(它应该是。支持.Net AWS SDK)
安装因此,所有你需要做的就是打开Visual Studio,发现其样本S3的项目,或you can look at it here:
// simple object put
PutObjectRequest request = new PutObjectRequest()
{
ContentBody = "this is a test",
BucketName = bucketName,
Key = keyName
};
PutObjectResponse response = client.PutObject(request);
这里假设你有实例化的Amazon.S3。 AmazonS3Client包含名称空间后,并使用您自己的凭据配置它。
对于.netcore项目中的简单文件上传,我遵循以下link。
完成简单的文件上传程序后,我按照this和this链接的文档,这些链接非常有帮助。以下两个链接也有助于快速启动。
这是在控制器中进行文件上传我的最终代码段(I跳过视图部分,其在所述连杆精心解释上述共享) 。
[HttpPost("UploadFiles")]
public IActionResult UploadFiles(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
foreach (var formFile in files)
{
if (formFile.Length > 0)
{
var filename = ContentDispositionHeaderValue
.Parse(formFile.ContentDisposition)
.FileName
.TrimStart().ToString();
filename = _hostingEnvironment.WebRootPath + [email protected]"\uploads" + [email protected]"\{formFile.FileName}";
size += formFile.Length;
using (var fs = System.IO.File.Create(filename))
{
formFile.CopyTo(fs);
fs.Flush();
}//these code snippets saves the uploaded files to the project directory
uploadToS3(filename);//this is the method to upload saved file to S3
}
}
return RedirectToAction("Index", "Library");
}
这是将文件上传到亚马逊S3的方法:
private IHostingEnvironment _hostingEnvironment;
private AmazonS3Client _s3Client = new AmazonS3Client(RegionEndpoint.EUWest2);
private string _bucketName = "mis-pdf-library";//this is my Amazon Bucket name
private static string _bucketSubdirectory = String.Empty;
public UploadController(IHostingEnvironment environment)
{
_hostingEnvironment = environment;
}
public void uploadToS3(string filePath)
{
try
{
TransferUtility fileTransferUtility = new
TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.EUWest2));
string bucketName;
if (_bucketSubdirectory == "" || _bucketSubdirectory == null)
{
bucketName = _bucketName; //no subdirectory just bucket name
}
else
{ // subdirectory and bucket name
bucketName = _bucketName + @"/" + _bucketSubdirectory;
}
// 1. Upload a file, file name is used as the object key name.
fileTransferUtility.Upload(filePath, bucketName);
Console.WriteLine("Upload 1 completed");
}
catch (AmazonS3Exception s3Exception)
{
Console.WriteLine(s3Exception.Message,
s3Exception.InnerException);
}
}
这一切都是为了在亚马逊S3存储桶上传文件。我在.netcore 2.0上工作,并且不要忘记添加必要的依赖关系来使用Amazon API。它们是:
- AWSSDK.Core
- AWSSDK.Extensions.NETCore.Setup
- AWSSDK.S3
希望,这将有助于。
@ LP13我已经看到了你的问题,所以我认为你可以对我的问题有帮助。 – kostas
关于如何上传文件,你是什么意思?上传到AWS/S3的文件是相同的,与您使用哪种语言无关。它是一个Restful API,可以与纯HTTP协同工作。因此,任何编程语言或环境中的任何Web客户端都可以工作,甚至可以从命令行中卷曲。一个简单的谷歌查询结束了[这里](http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpREST.html) – Tseng
在.NET Framework中,它可以很好地处理AWSSDK。在.NET Core中,我应该将它安装为中间件,当我尝试时,没有任何工作正常。 – kostas