Amazon SDK for JavaScriptV3 API 参考指南详细描述了Amazon SDK for JavaScript版本 3 (V3) 的所有 API 操作。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Amazon STS使用适用于 JavaScript V3 的 SDK 的示
以下代码示例向您展示如何通过Amazon SDK for JavaScript V3 执行操作和实施常见场景Amazon STS。
操作是展示如何调用具体服务函数的代码节选。
场景是展示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。
每个示例都包含一个链接 GitHub,其中包含了有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例显示如何代入具有的角色Amazon STS。
- 适用于 Java JavaScript V3
-
注意
还有更多 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 创建客户端。
import { STSClient } from "@aws-sdk/client-sts"; // Set the AWS Region. const REGION = "us-east-1"; // Create an AWS STS service client object. export const client = new STSClient({ region: REGION });
代入 IAM 角色。
import { AssumeRoleCommand } from "@aws-sdk/client-sts"; import { client } from "../libs/client.js"; export const main = async () => { try { // Returns a set of temporary security credentials that you can use to // access Amazon Web Services resources that you might not normally // have access to. const command = new AssumeRoleCommand({ // The Amazon Resource Name (ARN) of the role to assume. RoleArn: "ROLE_ARN", // An identifier for the assumed role session. RoleSessionName: "session1", // The duration, in seconds, of the role session. The value specified // can range from 900 seconds (15 minutes) up to the maximum session // duration set for the role. DurationSeconds: 900, }); const response = await client.send(command); console.log(response); } catch (err) { console.error(err); } };
-
有关 API 的详细信息,请参阅 AssumeRoleAmazon SDK for JavaScriptAPI 参考中的。
-
- 适用于 JavaScript V2 的开发工具包
-
注意
还有更多 GitHub。查找完整示例,学习如何在 Amazon 代码示例存储库
中进行设置和运行。 // Load the AWS SDK for Node.js const AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); var roleToAssume = {RoleArn: 'arn:aws:iam::123456789012:role/RoleName', RoleSessionName: 'session1', DurationSeconds: 900,}; var roleCreds; // Create the STS service object var sts = new AWS.STS({apiVersion: '2011-06-15'}); //Assume Role sts.assumeRole(roleToAssume, function(err, data) { if (err) console.log(err, err.stack); else{ roleCreds = {accessKeyId: data.Credentials.AccessKeyId, secretAccessKey: data.Credentials.SecretAccessKey, sessionToken: data.Credentials.SessionToken}; stsGetCallerIdentity(roleCreds); } }); //Get Arn of current identity function stsGetCallerIdentity(creds) { var stsParams = {credentials: creds }; // Create STS service object var sts = new AWS.STS(stsParams); sts.getCallerIdentity({}, function(err, data) { if (err) { console.log(err, err.stack); } else { console.log(data.Arn); } }); }
-
有关 API 的详细信息,请参阅 AssumeRoleAmazon SDK for JavaScriptAPI 参考中的。
-