I am trying to create a react app where a user can upload a file to an S3 bucket.
I have a react component that can successfully get the file from the user. And a button that calls a function where I can send the file to S3.
I am using react-aws-s3
because this is going to be the only direct functionality I am going to need so I didn't want to install the whole aws-sdk
package and bloat my application.
Now I have followed a few different blogs/instructions on how to do this ( This will give you more information on the actual problem that you are having
Your bucket policy allows only List and Get, but in the tutorial, it's s3:* So your user must have permission to upload files to this bucket.
Double check the upload logic in the demo it's like this:
const handleFileInput = (e) => {
setSelectedFile(e.target.files[0]);
}
const uploadFile = async (file) => {
const ReactS3Client = new S3(config);
// the name of the file uploaded is used to upload it to S3
ReactS3Client
.uploadFile(file, file.name)
.then(data => console.log(data.location))
.catch(err => console.error(err))
}
return <div>
<div>React S3 File Upload</div>
<input type="file" onChange={handleFileInput}/>
<br></br>
<button onClick={() => uploadFile(selectedFile)}> Upload to S3</button>
</div>