NodeJS AWS S3 PresignedPost在本地工作,但在生产中失败,并出现ERR_CONNECTION_RESET

3okqufwl  于 2023-04-05  发布在  Node.js
关注(0)|答案(2)|浏览(121)

我有一个后端lambda函数,它使用下面的代码创建了预签名的post url,它可以很好地返回生产和本地计划的url。

router.post("/file_upload", async (req, res) => {
try {
var name = `${uuidv4()}`;
var params = {
    Bucket: process.env.S3_BUCKET,
    Fields: {
        key: name, // File name you want to save as in S3,
        //acl: "public-read"
    },
    Expires: 3600,
    Conditions:[
        ["content-length-range", 0, 52428800]
    ]
};

s3.createPresignedPost(params, (err, data) => {
    if(err) {
      throw err;
    }
    Object.assign(data, { name });
    console.log(data);
    res.send(data);
});
} catch (error) {
res.status(500);
res.end(`Error: ${error}`);
console.log(error);
}
});

然而,当下面的代码执行这个url的post请求时,我收到一个错误ERR_CONNECTION_RESET,每次都会发生这种情况:

let results = await triggerAWSS3("file_upload", name);
if (results && results.url && results.fields) {
  name = results.name;
  let form = new FormData();
  let keys = Object.keys(results.fields);
  await Promise.all(keys.map((key) => {
    form.append(key, results.fields[key]);
    return true;
  }));
  form.append('file', file);
  let response = '';
  try {
    //This line generates the error
    response = await Axios.post(results.url, form, { headers: { 'content-type': 'multipart/form-data' } });
    console.log(response);
  } catch (error) {
    console.log(error);
  }

我有一个公共桶政策,如下所示:

{
"Version": "2012-10-17",
"Id": "http referer policy example",
"Statement": [
    {
        "Sid": "Allow get requests originating from www.example.com and example.com.",
        "Effect": "Allow",
        "Principal": "*",
        "Action": [
            "s3:GetObject",
            "s3:PutObject",
            "s3:PutObjectAcl"
        ],
        "Resource": "arn:aws:s3:::example-s3-bucket/*",
        "Condition": {
            "StringLike": {
                "aws:Referer": [
                    "https://www.example.com/*",
                    "https://example.com/*",
                    "https://localhost:3000/*"
                ]
            }
        }
    }
]
}

如果有人能帮我找出为什么这是不工作,这将是梦幻般的,因为我已经尝试了许多不同的修复这一点,没有工作。

muk1a3rh

muk1a3rh1#

因此,经过几天的故障排除,我发现S3存储桶被配置到了错误的区域。

const AWS = require('aws-sdk');
const { v4: uuidv4 } = require('uuid');
const s3 = new AWS.S3();
AWS.config.update({ signatureVersion: 'v4',
                    accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY,   
                    secretAccessKey: process.env.REACT_APP_AWS_SECRET_KEY, 
                    region: process.env.REACT_APP_AWS_REGION });

我把config update语句移到了S3变量定义的上方,这是一个愚蠢的错误,但也许我可以在将来为其他人节省时间。

const AWS = require('aws-sdk');
const { v4: uuidv4 } = require('uuid');
AWS.config.update({ signatureVersion: 'v4',
                    accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY,   
                    secretAccessKey: process.env.REACT_APP_AWS_SECRET_KEY, 
                    region: process.env.REACT_APP_AWS_REGION });
const s3 = new AWS.S3();
vc6uscn9

vc6uscn92#

在我的例子中,我忘记将我的AWS环境变量添加到Vercel。

相关问题