0

我在将流星弹弓组件与组件一起使用时遇到问题S3 with temporary AWS Credentials。我不断收到错误Exception while invoking method 'slingshot/uploadRequest' InvalidClientTokenId: The security token included in the request is invalid.

完全不知道我做错了什么。如果我在没有凭据的情况下正常使用弹弓,它可以正常工作。

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();
  }
});

小路:Settings.json

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

1 回答 1

0

我已经在服务器端这样做了:

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',
  ...
}

并在 settings.json

{
"AWS":  {
  "AccessKeyId": "myAWSKEYID",
  "SecretAccessKey": "MyAWSSeceretAccessKey" 
 }
}
于 2018-02-06T06:50:11.547 回答