React JS : Upload CSV file To Amazon S3 using AWS credentials

别来无恙 提交于 2021-01-28 08:30:35

问题


I want to upload CSV file using react S3 Uploader. I have AWS credentials as well. But I don't know how to use it in React JS.

Below is the code I have used.

import React, { PureComponent } from "react";
import ReactS3Uploader from "react-s3-uploader";


 saveUploaderReference = uploader => {
    if (uploader) {
      this.uploaderReference = uploader;
    }
  };
  
  
  getSignedUrl = (file) => {
     console.log("File : " , file)
  };

  


<ReactS3Uploader
            ref={this.saveUploaderReference}
            getSignedUrl={this.getSignedUrl}
            s3path="temp/"
            uploadRequestHeaders={{}}
            contentDisposition="auto"
            scrubFilename={filename => this.filename}
            autoUpload={true}
            multiple={false}
          />

I don't know where to use AWS credentials.

I have accessKeyID & secretAccessID. I don't know where to use it.

Any help would be great.

Thank You.


回答1:


You don't pass the credentials to react-s3-uploader directly. This is would be a HUGE security problem.

You have at least 2 ways of handling this:

  • use presigned urls. Which is a way that you give access to an S3 item to upload items without having credentials (credentials are actually in the url). You'll need a backend for doing this. This is also the way that react-s3-uploader recommends to do it
  • use an AWS Cognito Identity pool for unauth users.



回答2:


As Radu Diță mentioned already in the second option, use AWS Cognito for that. No need to setup your own backend server + API.

Also, Amplify helps you when you develop with React + AWS.

Here is a good example for your scenario: https://serverless-stack.com/chapters/create-a-login-page.html



来源:https://stackoverflow.com/questions/60994199/react-js-upload-csv-file-to-amazon-s3-using-aws-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!