Created
March 18, 2014 16:43
-
-
Save Shrulik/9624014 to your computer and use it in GitHub Desktop.
This is a simple example of how to use STS.getFederationToken and its result to perform actions with the new credentials, in this case S3.putObject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var aws = require('aws-sdk') | |
aws.config.update({accessKeyId: nconf.get('AWS_KEY'), | |
secretAccessKey: nconf.get('AWS_SECRET')}); | |
var STS = new aws.STS({apiVersion: '2011-06-15'}); | |
var tempCred = STS.getFederationToken( | |
{'Name': token, 'Policy': JSON.stringify(policy), 'DurationSeconds': 90000}, | |
function (err, data) { | |
if (err) { | |
return callback(err, null); | |
} | |
callback(null, data); | |
}); | |
var s3 = new aws.S3({ | |
accessKeyId: tempCred.Credentials.AccessKeyId, | |
secretAccessKey: tempCred.Credentials.SecretAccessKey, | |
sessionToken: tempCred.Credentials.SessionToken | |
// credentials : tempCred.Credentials // Didn't work, not sure why | |
}); | |
s3.putObject({Bucket: nconf.get('AWS_BUCKET'), | |
Body: 'Hello Stranger, I\'m temp ' + Date.now(), | |
Key: nconf.get('BUCKET_DIR') + Date.now() + ".txt", | |
ACL: 'public-read' | |
}, function (err, data) { | |
if (!err) | |
console.log("Success, file saved") | |
console.log(err || data) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment