我正在使用 Node.js 和 multer3s 与 AWS S3 进行通信。我尝试了一些不同的设置,但仍然无法正确地将文件实际上传到我的存储桶。
对于 S3 设置,我遵循了本教程。
在我的 IAM 服务中,我有一个用户myuser
有这个
许可政策
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets",
"s3:PutObject",
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::*"
]
}
]
}
然后我有一个设置Block all public access
为off
(我也尝试过)的桶。
桶有:
- 桶策略
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddCannedAcl",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::228522121793:user/myuser"
},
"Action": [
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": [
"arn:aws:s3:::mybucket/*",
"arn:aws:s3:::mybucket"
],
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "public-read"
}
}
}
]
}
- CORS 配置
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
- 在
Access points
部分下没有创建访问点。
在 Node.js 中
aws.config.update({
accessId: ACCESS_KEY,
secretAccessKey: SECRET_KEY,
region: 'eu-west-3'
})
const s3 = new aws.S3()
const upload = multer({
storage: multerS3({
s3: s3,
bucket: BUCKET,
acl: 'public-read',
key(req, file, cb) {
cb(null, Date.now() + shortid.generate())
}
}),
limits: {
files: 1,
fileSize: 2580 * 1944
},
fileFilter(req, file, cb) {
// filter
cb(undefined, true)
}
})
router.post('/test/img', upload.single('img'), async (req, res) => {
console.log("success")
console.log(req.file.location)
res.send()
}, (error, req, res, next) => {
res.status(400).send({
error: error.message
})
})
对于我可能做错的任何建议,我将不胜感激。谢谢