一段时间以来,我一直在使用流星弹弓将文件上传到亚马逊 S3。突然之间,我的上传功能似乎都不起作用。尝试上传文件时出现以下错误。
Error uploading errorClass {error: "Forbidden - 403", reason: "Failed to upload file to cloud storage", details: undefined, message: "Failed to upload file to cloud storage [Forbidden - 403]", errorType: "Meteor.Error"…}
上周所有上传工作正常,突然间我在所有项目中都遇到了上述错误。我不知道 slingshot 包是否有问题,或者是否对 s3 策略进行了一些更改。
这是我正在使用的代码
Slingshot.fileRestrictions("myFileUploads", {
allowedFileTypes: ["image/png", "image/jpeg", "image/gif" ,],
maxSize: null // 10 MB (use null for unlimited).
});
Slingshot.createDirective("myFileUploads", Slingshot.S3Storage, {
bucket: "*****",
acl: "public-read",
region : "ap-southeast-1",
AWSAccessKeyId : "MyAccessKey",
AWSSecretAccessKey : "MySecretKey",
authorize: function () {
return true;
},
key: function (file) {
var imageName = getUniqueID()
return "images/" + imageName;
}
});
getUniqueID = function(){
this.length = 8;
this.timestamp = +new Date;
var ts = this.timestamp.toString();
var parts = ts.split( "" ).reverse();
var id = "";
var _getRandomInt = function( min, max ) {
return Math.floor( Math.random() * ( max - min + 1 ) ) + min;
}
for( var i = 0; i < this.length; ++i ) {
var index = _getRandomInt( 0, parts.length - 1 );
id += parts[index];
}
return id;
}
这是我的 CORS 配置
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
还有我的存储桶政策
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::MyBucketName/*"
}
]
}