首页 > 解决方案 > 使用 Next js 将文件上传到 s3 存储桶

问题描述

使用下一个 js 将文件上传到 s3 存储桶得到 cors 错误

Access to fetch at 'https://*****.s3-us-***.amazonaws.com/' from origin 'http://*****.amazonaws.com:8900' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

没有得到任何适当的解决方案如何解决此类错误,并且还提供了 s3 存储桶中的跨域资源共享 (CORS) 是他们在本地机器上解决此类错误的任何方法

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "x-amz-server-side-encryption",
            "x-amz-request-id",
            "x-amz-id-2"
        ],
        "MaxAgeSeconds": 3000
    }
]

在选择文件时需要将文件上传到 s3 存储桶下面是我的上传代码

aws.config.update({
    accessKeyId: "##",
    secretAccessKey: "###",
    region: "###",
    signatureVersion: 'v4',
  });
const s3 = new aws.S3();
const params = {
         Bucket: '####', // pass your bucket name
         Key: 'Zip/'+fileName, // file will be saved as testBucket/contacts.csv
         ContentType: 'application/zip',
         Body: data,
     };
     s3.upload(params, function(s3Err, data) {
         
         if (s3Err) throw s3Err
         console.log(`File uploaded successfully at ${data.Location}`)
     });

提前致谢

标签: reactjsamazon-web-servicesamazon-s3corsnext.js

解决方案


推荐阅读