简体   繁体   中英

Access IAM Roles S3 Buckets images via amazon s3 sdk for javascript

I am using IAM role for s3 bucket. I want to access images from s3 without using any secret key and access key . I am using aws s3 javascript sdk.

You will need to setup STS and assume the role to get temporary credentials to access AWS Services.

/*  */

 var params = {
  DurationSeconds: 3600, 
  RoleArn: "arn:aws:iam::123456789012:role/demo", 
  RoleSessionName: "Bob"
 };
 sts.assumeRole(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    AssumedRoleUser: {
     Arn: "arn:aws:sts::123456789012:assumed-role/demo/Bob", 
     AssumedRoleId: "ARO123EXAMPLE123:Bob"
    }, 
    Credentials: {
     AccessKeyId: "AKIAIOSFODNN7EXAMPLE", 
     Expiration: <Date Representation>, 
     SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY", 
     SessionToken: "AQoDYXdzEPT//////////wEXAMPLEtc764bNrC9SAPBSM22wDOk4x4HIZ8j4FZTwdQWLWsKWHGBuFqwAeMicRXmxfpSPfIeoIYRqTflfKD8YUuwthAx7mSEI/qkPpKPi/kMcGdQrmGdeehM4IC1NtBmUpp2wUE8phUZampKsburEDy0KPkyQDYwT7WZ0wq5VSXDvp75YU9HFvlRd8Tx6q6fE8YQcHNVXAkiY9q6d+xo0rKwT38xVqr7ZD0u0iPPkUL64lIZbqBAz+scqKmlzm8FDrypNC9Yjc8fPOLn9FX9KSYvKTr4rvx3iSIlTJabIQwj2ICCR/oLxBA=="
    }, 
    PackedPolicySize: 6
   }
   */
 });

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/STS.html#assumeRole-property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM