我有一个后端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/*"
]
}
}
}
]
}
如果有人能帮我找出为什么这是不工作,这将是梦幻般的,因为我已经尝试了许多不同的修复这一点,没有工作。
2条答案
按热度按时间muk1a3rh1#
因此,经过几天的故障排除,我发现S3存储桶被配置到了错误的区域。
我把config update语句移到了S3变量定义的上方,这是一个愚蠢的错误,但也许我可以在将来为其他人节省时间。
vc6uscn92#
在我的例子中,我忘记将我的AWS环境变量添加到Vercel。