2

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

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.