我有一个表单可以将文件上传到我在 aws S3 中的存储桶,这就是表单
<form action="/user/fileUpload" enctype="multipart/form-data" class="ui form" method="post">
<div class="field">
<select class="ui dropdown">
<option value="">Print Agency</option>
<option value="fastprint">Fast Print</option>
<option value="printpress">Print Press</option>
</select>
</div>
<div class="field">
<input type="file" name="uploadFile" />
</div>
<div class="field">
<input type="submit" value="Upload" class="ui blue submit button" />
</div>
</form>
如果我按下提交按钮,这是将激活的操作
fileUpload: function(req, res) {
req.file('uploadFile').upload({
adapter: require('skipper-s3'),
key: 'THE-KEY',
secret: 'THE-SECRET',
bucket: 'THE-BUCKET'
}, function (err, filesUploaded) {
if (err) {
console.log(err);
return res.negotiate(err);
}
return res.ok({
files: filesUploaded,
textParams: req.params.all()
});
});}
这是我的 S3 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>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
这是我的 S3 存储桶策略
{
"Version": "2012-10-17",
"Id": "Policy1433335661532",
"Statement": [
{
"Sid": "Stmt1433335656754",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:DeleteObject",
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::BUCKET-NAME/*",
"arn:aws:s3:::BUCKET-NAME"
]
}
]
}
但结果总是被禁止。我不知道为什么。 结果图片:
我真的需要你的帮助。
更新答案
在思考@tom 的回答后,我发现我知道所有这些后我是多么愚蠢,因为我忘了写几行。
我应该将这一行放在 routes.js 中
'post /user/fileUpload': {
controller: 'UserController',
action: 'fileUpload'},
提交按钮下方的这一行,因为我使用 CSRF
<input type="hidden" name="_csrf" value="<%= _csrf %>" />
这行在 UserController 操作文件上传中,在“bucket”下面
ACL: 'public-read'
问候,
埃尔默
最佳答案
如果 console.log(err) 确实如您在评论中提到的那样为 null,则这不太可能是 s3 的问题,因为如果通过,sails 没有理由返回 403。
相反,我建议检查是否曾调用过 fileUpload
。如果不是,请确保策略设置正确并且您可以访问该 Controller 的方法。
关于javascript - 上传到 S3 返回 Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30621799/