我正在使用Node.js和multer3s与AWS S3通信。我尝试了几种不同的设置,但仍然无法正确地将文件实际上传到存储桶。
对于S3设置,我已经遵循this教程。
在我的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"
}
}
}
]
}
<?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
部分下,没有创建访问点。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
})
})
对于我可能做错了什么,我将不胜感激。谢谢