S3 bucket with credentials error

746 views Asked by At

I'm having trouble using the meteor slingshot component with the S3 with temporary AWS Credentials component. I keep getting the error Exception while invoking method 'slingshot/uploadRequest' InvalidClientTokenId: The security token included in the request is invalid.

Absolutely no idea what I'm doing wrong. If I use slingshot normally without credentials it works fine.

import { Meteor } from 'meteor/meteor';
import moment from 'moment';
const cryptoRandomString = require('crypto-random-string');

var AWS = require('aws-sdk');

var sts = new AWS.STS();

Slingshot.createDirective('UserProfileResumeUpload', Slingshot.S3Storage.TempCredentials, {
  bucket: 'mybuckname', // change this to your s3's bucket name
  region: 'ap-southeast-2',
  acl: 'private',

  temporaryCredentials: Meteor.wrapAsync(function (expire, callback) {
    //AWS dictates that the minimum duration must be 900 seconds:
    var duration = Math.max(Math.round(expire / 1000), 900);

    sts.getSessionToken({
        DurationSeconds: duration
    }, function (error, result) {
        callback(error, result && result.Credentials);
    });
  }),

  authorize: function () {
    //Deny uploads if user is not logged in.
    if (!this.userId) {
      const message = 'Please login before posting files';
      throw new Meteor.Error('Login Required', message);
    }

    return true;
  },

  key: function () {
    return 'mydirectory' + '/' + cryptoRandomString(10) + moment().valueOf();
  }
});

Path: Settings.json

{
  "AWSAccessKeyId": "myAWSKEYID",
  "AWSSecretAccessKey": "MyAWSSeceretAccessKey"
}
1

There are 1 answers

7
iiro On

I've done it in server side like this :

Slingshot.createDirective("UserProfileResumeUpload", Slingshot.S3Storage, {
  AWSAccessKeyId: Meteor.settings.AWS.AccessKeyId,
  AWSSecretAccessKey: Meteor.settings.AWS.SecretAccessKey,
  bucket: 'mybuckname', // change this to your s3's bucket name
  region: 'ap-southeast-2',
  acl: 'private',
  ...
}

and in settings.json

{
"AWS":  {
  "AccessKeyId": "myAWSKEYID",
  "SecretAccessKey": "MyAWSSeceretAccessKey" 
 }
}