howtoexpert200
howtoexpert200

Reputation: 81

How to corretly upload to Amazon S3 with meteor slingshot?

I am trying to upload to S3 and the error I keep getting in the web console is

Exception in delivering result of invoking 'slingshot/uploadRequest': TypeError: >Cannot read property 'response' of undefined

Here is my sever side code:

Slingshot.fileRestrictions("Test1", {
  allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
  maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited)
});


Slingshot.createDirective("Test1", Slingshot.S3Storage, {

  AWSAccessKeyId: "Key",
  AWSSecretAccessKey: "Key",
  bucket: "bucketname",


  acl: "public-read",

  authorize: function () {
    //Deny uploads if user is not logged in.

    },

  key: function (file) {
    //Store file into a directory by the user's username.
    return file.name;
  }

Client side code:

Template.first.events({
    'change .fileInput': function(event, template) {

      event.preventDefault();

var uploader = new Slingshot.Upload("Test1");
var docc = document.getElementById('fileup').files[0];
console.log(docc);

uploader.send(docc, function (error){
  if (error) {


    console.error('Error uploading', uploader.xhr.response);

    alert (error);
  }
  else{
    console.log("Worked!");
  }

  });
  }

Help would be much appreciated!

Upvotes: 1

Views: 1606

Answers (2)

NoloMokgosi
NoloMokgosi

Reputation: 1708

Failing on below line. xhr is null

console.error('Error uploading', uploader.xhr.response);

Log the error like below and debug from there

console.error('Error uploading', error);

Upvotes: 0

Andy Lorenz
Andy Lorenz

Reputation: 3084

It also appears important to reference the region in the createDirective command block, e.g.

Slingshot.createDirective("Test1", Slingshot.S3Storage, {

  bucket: "bucketname",
  region: "eu-west-1"
  ...

Without the region explicitly specified, all I get is a '400 - Bad Request".

The AWSAccessKeyId and AWSSecretAccessKey should go in a separate settings.json file that is restricted to your server's deployment and excluded from source code management (github).

settings.json:

{
  "AWSAccessKeyId":     "<insert key id here>",
  "AWSSecretAccessKey": "<insert access key here>"
}

Typically you'll have a settings file per environment (dev, acpt, live) all as part of the same code-base, and selected at runtime for the environment required.

Upvotes: 1

Related Questions