I'm trying to upload an image to AWS S3 in Meteor/Slingshot. The upload is pending for a long time and then failing (400 Bad request). Some files managed to upload but most of the time they don't. This seems to be happening randomly.
What are the reasons that may cause this?
The S3 bucket is in Ireland "eu-west-1". Here's the code I have:
In 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).
});
app/server/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;
}
});
app/client/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);
},
});