0

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"
                }]

}
Vega
  • 27,856
  • 27
  • 95
  • 103
azuldev
  • 570
  • 6
  • 10
  • Are the iam user and bucket in the same account ? If so, check you have the permission on IAM user and also in bucket change the resource to arn:aws:s3:::my-buc/* – James Dean Aug 25 '19 at 14:26
  • Yes, but let me try that real quick – azuldev Aug 25 '19 at 20:12
  • @JamesDean did not work – azuldev Aug 25 '19 at 20:14
  • @JamesDean I found another Question and Answer, and the answer said that puclic URL can not be accessed during an s3.upload call, the answer said to do an additional call to get public URL....is that correct ? – azuldev Aug 25 '19 at 20:15
  • https://stackoverflow.com/questions/10975475/amazon-s3-upload-file-and-get-url – azuldev Aug 25 '19 at 20:16

1 Answers1

-1

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
};
Community
  • 1
  • 1
azuldev
  • 570
  • 6
  • 10