Can not upload file to S3 bucket in AWS Amplify

Viewed 779

I deployed my React project to AWS-Amplify using the following commands.

amplify init
amplify add auth api storage

And I'm facing an error while uploading images using the "aws-amplify" node module.

My code :

import { Storage } from "aws-amplify"
...

    try {
      Storage.put(file.name, file, {
        contentType: file.type,
      }).then((res) => {
        console.log(res, "Upload result")
      })
    } catch (error) {
      console.error("Error uploading file: ", error)
    }

Error status :

Error screen shot

As you can see, now Storage.put() function returns the 400 error code. Is there anyone with the solution for this?

1 Answers

ContentType is optional, if you are not sure that your code is well formated you might try to simplify it until it works and then add extra arguments.

You can look at put examples here

There is another thing that you might want to have a look at, the s3 bucket policies, check what are you allowed to do with:

aws s3api get-bucket-policy --bucket my-bucket

If you don't know how to use was cli, have a look here

Related