S3 bucket with credentials error

半世苍凉 提交于 2020-01-06 08:06:12

问题


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:


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" 
 }
}


来源:https://stackoverflow.com/questions/48619756/s3-bucket-with-credentials-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!