简体   繁体   English

reactJS文件上传到AWS S3

[英]reactJS file Upload to AWS S3

what is the esiest way to upload files to AWS S3 in plain javascript from my ReactJS application? 从我的ReactJS应用程序以普通javascript的形式将文件上传到AWS S3的最简单方法是什么?

Is there a way only do this with REST or the aws sdk? 有没有办法只用REST或aws sdk这样做?

thanks 谢谢

Many Days ago I uploaded a file on Amazon S3 using putObject method. 很多天前,我使用putObject方法在Amazon S3上上传了一个文件。 I achived this functionality in my MVC architecture project. 我在MVC架构项目中实现了这一功能。 See this sample code as below, I hope, It might be helpful to you. 请参阅下面的示例代码,我希望,它可能对您有所帮助。

Note: May be you have different way in ReactJs but to upload a file on S3 uisng PutObjectRequest() method based IAmazon interface will be remain same. 注意:可能你在ReactJs中有不同的方式但是在S3上上传文件uisng基于IAmazon接口的PutObjectRequest()方法将保持不变。

Below configuration method is in my base controller. 下面的配置方法在我的基本控制器中。

public static IAmazonS3 Configuration()
{
     var config = new Amazon.S3.AmazonS3Config();
     config.RegionEndpoint = RegionEndpoint.USEast1;

     var credentials = new Amazon.Runtime.BasicAWSCredentials(ConfigurationManager.AppSettings["AWSAccessKey"], ConfigurationManager.AppSettings["AWSSecurityKey"]);
            return Amazon.AWSClientFactory.CreateAmazonS3Client(credentials, config);
}

Below code that I used in my controller which have file upload action. 我在控制器中使用的代码下面有文件上传操作。

HttpPostedFileBase uploadFile = uploadfileCollection[i];
var fileName = Path.GetFileName(uploadFile.FileName);
fileforAPI += fileName + ',';

var putrequest = new PutObjectRequest()
{
   BucketName = ConfigItems.AWSServiceFinanceSampleFileBucket,
   Key = "FinanceData" + finalUploadFilePathOnS3 + fileName,
   InputStream = uploadFile.InputStream,
   CannedACL = S3CannedACL.PublicReadWrite
};


PutObjectResponse response = client.PutObject(putrequest);

if (!response.HttpStatusCode.ToString().Equals("OK"))
{
   isUploadedSuccessfully = false;
   break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM