1

I try to upload file to my s3 bucket use this library https://github.com/Fausto95/aws-s3 and I get this error in the console:

enter image description here

this is code of component:

<template>
  <input type="file" @change="uploadFile(fieldName, $event.target.files)"/>
</template>

<script>
import S3 from 'aws-s3';

export default {
  computed: {
    config() {
      return {
        bucketName: 'in-converter-bucket',
        dirName: '', /* optional */
        region: 'eu-west-2',
        accessKeyId: 'AKIAZSUESKSZVZTI55PV',
        secretAccessKey: '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',
        s3Url: 'https://s3.console.aws.amazon.com/s3/buckets/in-converter-bucket?region=us-west-2&tab=objects', /* optional */
      }
    },
    S3Client() {
      return new S3(this.config);
    }
  },
  methods: {
    uploadFile(fieldName, files) {
      let file = files[0]
      this.S3Client
      .uploadFile(file, '324')
      .then(data => console.log(data))
      .catch(err => console.error(err))
    }
  },
  props: ['fieldName', 'obj']
}

</script>

Any help will be very useful

5
  • Have you setup cors for your bucket? Commented Jan 4, 2021 at 10:44
  • Thanks for comment, I don't know what is it. I will be reading about it. Commented Jan 4, 2021 at 12:19
  • I added this json to Cross-origin resource sharing (CORS) Commented Jan 12, 2021 at 12:08
  • [ { "AllowedHeaders": [ "" ], "AllowedMethods": [ "PUT", "POST", "HEAD", "GET", "DELETE" ], "AllowedOrigins": [ "" ], "ExposeHeaders": [] } ] Commented Jan 12, 2021 at 12:08
  • But it still don't work. Commented Jan 12, 2021 at 12:09

2 Answers 2

2

There were three problems:

First: I didn't add CORS, something like this:

[
{
    "AllowedHeaders": [
        "*"
    ],
    "AllowedMethods": [
        "PUT",
        "POST",
        "HEAD",
        "GET",
        "DELETE"
    ],
    "AllowedOrigins": [
        "*"
    ],
    "ExposeHeaders": []
    }
]

Second I didn't add Bucket policy, something like this:

{
"Version": "2012-10-17",
"Id": "http referer policy example",
"Statement": [
    {
        "Sid": "Allow admins to put and delete objects",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::658481773507:user/Admin"
        },
        "Action": [
            "s3:PutObject",
            "s3:PutObjectAcl",
            "s3:GetObject",
            "s3:GetObjectAcl",
            "s3:DeleteObject"
        ],
        "Resource": [
            "arn:aws:s3:::js-bucket",
            "arn:aws:s3:::js-bucket/*"
        ],
        "Condition": {}
    },
    {
        "Sid": "Allow anyone to get objects",
        "Effect": "Allow",
        "Principal": "*",
        "Action": [
            "s3:GetObject",
            "s3:GetObjectAcl"
        ],
        "Resource": [
            "arn:aws:s3:::js-bucket",
            "arn:aws:s3:::js-bucket/*"
        ],
        "Condition": {}
        }
    ]
}

And I mixed up region, I wrote: eu-west-2, but I use: us-west-2;

Sign up to request clarification or add additional context in comments.

Comments

0

To anyone still having trouble, adding "Access-Control-Allow-Origin" in "ExposeHeaders" fixed mine. Here's the refernece I used: How do I configure CORS in Amazon S3 and confirm the CORS rules using cURL?

[{
    "AllowedHeaders": [
        "*",
        "Authorization"
    ],
    "AllowedMethods": [
        "HEAD",
        "GET",
        "PUT",
        "POST",
        "DELETE"
    ],
    "AllowedOrigins": [
        "*",
        "http://localhost:3000",
    ],
    "ExposeHeaders": [
        "Access-Control-Allow-Origin"
    ],
    "MaxAgeSeconds": 3000
}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.