AWS S3 PresignedPost 在本地工作但在生产中失败 ERR_CONNECTION_RESET
AWS S3 PresignedPost works locally but fails in production with ERR_CONNECTION_RESET
我有一个后端 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);
}
我有一个 public 存储桶策略,如下所示:
{
"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/*"
]
}
}
}
]
}
如果有人能帮我弄清楚为什么这不起作用,那就太好了,因为我已经尝试了许多不同的修复方法,但没有任何效果。
经过几天的故障排除后,我发现 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 });
我通过将配置更新语句移动到 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();
我有一个后端 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);
}
我有一个 public 存储桶策略,如下所示:
{
"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/*"
]
}
}
}
]
}
如果有人能帮我弄清楚为什么这不起作用,那就太好了,因为我已经尝试了许多不同的修复方法,但没有任何效果。
经过几天的故障排除后,我发现 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 });
我通过将配置更新语句移动到 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();