我正在尝试在 Meteor/Slingshot 中将图像上传到 AWS S3。上传等待很长时间,然后失败(400 Bad request)。有些文件设法上传,但大多数时候他们没有。这似乎是随机发生的。
可能导致这种情况的原因是什么?
S3 存储桶位于爱尔兰“eu-west-1”。这是我的代码:
在 settings.json 中:
{
"public": {
},
"AWSAccessKeyId" : <keyID>,
"AWSSecretAccessKey" : <Secret>,
"AWSBucket" : <bucket_name>,
"AWSRegion": "eu-west-1"
}
app/lib/_fileUploadRestrictions.js
Slingshot.fileRestrictions("imageUploads", {
allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited).
});
应用程序/服务器/fileUploadDirectives.js
Slingshot.createDirective("imageUploads", Slingshot.S3Storage, {
region: Meteor.settings.AWSRegion,
bucket: Meteor.settings.AWSBucket,
acl: "public-read",
expire: 200,
authorize: function () {
//Deny uploads if user is not logged in.
if (!this.userId) {
var message = "Please login before posting files";
throw new Meteor.Error("Login Required", message);
}
return true;
},
key: function (file) {
//Store file into a directory by the user's username.
var userId = Meteor.userId();
return "userfilies" + "/" + userId + "/" + "images" + "/" + file.name;
}
});
应用程序/客户端/fileUploader.js
Template.fileUploader.events({
'change .fileinput': function(event, template) {
var fileToUpload = event.target.files[0];
var fullUpload = new Slingshot.Upload("imageUploads");
fullUpload.send(fileToUpload, function (error, downloadUrl) {
if (error) {
toastr.error("Upload failed... please try again.");
console.log(error);
console.log(fullUpload.xhr.response);
}
else {
toastr.success('Upload succeeded!');
}
});
Template.instance().uploader.set(fullUpload);
},
});