Ad

Using Aws-api S3.upload Gives 403 Error On File Location

- 1 answer

How I can access data.Location which is a response from s3.upload from an audio src?

Right now when I try

<audio src"{pathToFile}" ></audio>

I get this error in the console

GET https://my-buc.s3.amazonaws.com/693v36g6j9o7pdi6qg4gafClean.mp3
403 (Forbidden)

Here is my code:

    const s3 = new AWS.S3({
      accessKeyId: process.env.REACT_APP_S3_KEY,
      secretAccessKey: process.env.REACT_APP_S3_SECRET
    });

    const params = {
      Bucket: "my-buc", // pass your bucket name
      Key: this.state.filename, 
      Body: this.state.uploadedFile
    };


    s3.upload(
      params,
      function(err, data) {
        console.log(err, data);
        console.log(data.Location);
        this.setState({ pathToFile: data.Location }); //extracting data.Location
      }.bind(this)
    );

my-buc policy

{
    "Version": "2012-10-17",
        "Id": "Policy##########",
            "Statement": [
                {
                    "Sid": "St##########",
                    "Effect": "Allow",
                    "Principal": {
                        "AWS": "arn:aws:iam::699######:user/user"
                    },
                    "Action": "s3:*",
                    "Resource": "arn:aws:s3:::my-buc"
                }]

}
Ad

Answer

Easy Easy Fix

const params = {
  Bucket: "my-buc", // pass your bucket name
  Key: this.state.filename, 
  Body: this.state.uploadedFile
  ACL: "public-read" //public read allows you to use the return URL
};
Ad
source: stackoverflow.com
Ad