简体   繁体   中英

reactJS file Upload to AWS S3

what is the esiest way to upload files to AWS S3 in plain javascript from my ReactJS application?

Is there a way only do this with REST or the aws sdk?

thanks

Many Days ago I uploaded a file on Amazon S3 using putObject method. I achived this functionality in my MVC architecture project. 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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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