English | 简体中文
Alibaba Cloud Credentials for TypeScript/Node.js 是帮助 Node.js 开发者管理凭据的工具。
本文将介绍如何获取和使用 Alibaba Cloud Credentials for TypeScript/Node.js。
使用 npm
下载安装
npm install @alicloud/credentials
##快速使用 在您开始之前,您需要注册阿里云帐户并获取您的凭证。
通过用户信息管理设置 access_key,它们具有该账户完全的权限,请妥善保管。有时出于安全考虑,您不能把具有完全访问权限的主账户 AccessKey 交于一个项目的开发者使用,您可以创建RAM子账户并为子账户授权,使用RAM子用户的 AccessKey 来进行API调用。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'access_key', // 凭证类型
accessKeyId: 'accessKeyId', // AccessKeyId
accessKeySecret: 'accessKeySecret', // AccessKeySecret
}
const cred = new Credential(config);
let accessKeyId: string = await cred.getAccessKeyId();
let accessKeySecret: string = await cred.getAccessKeySecret();
let type: string = cred.getType();
通过安全令牌服务(Security Token Service,简称 STS),申请临时安全凭证(Temporary Security Credentials,简称 TSC),创建临时安全凭证。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'sts', // 凭证类型
accessKeyId: 'accessKeyId', // AccessKeyId
accessKeySecret: 'accessKeySecret', // AccessKeySecret
securityToken: 'securityToken', // STS Token
}
const cred = new Credential(config);
let accessKeyId: string = await cred.getAccessKeyId();
let accessKeySecret: string = await cred.getAccessKeySecret();
let type: string = cred.getType();
通过指定RAM角色,让凭证自动申请维护 STS Token。你可以通过为 Policy
赋值来限制获取到的 STS Token 的权限。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'ram_role_arn', // 凭证类型
accessKeyId: 'accessKeyId', // AccessKeyId
accessKeySecret: 'accessKeySecret', // AccessKeySecret
roleArn: 'roleArn', // 格式: acs:ram::用户ID:role/角色名
roleSessionName: 'roleSessionName', // 角色会话名称
policy: 'policy', // 可选, 限制 STS Token 的权限
roleSessionExpiration: 3600, // 可选, 限制 STS Token 的有效时间
}
const cred = new Credential(config);
let accessKeyId: string = await cred.getAccessKeyId();
let accessKeySecret: string = await cred.getAccessKeySecret();
let securityToken: string = await cred.getSecurityToken();
let type: string = cred.getType();
通过指定[OIDC 角色][OIDC Role],让凭证自动申请维护 STS Token。你可以通过为 Policy
赋值来限制获取到的 STS Token 的权限。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'oidc_role_arn', // 凭证类型
accessKeyId: 'accessKeyId', // 可选, AccessKeyId
accessKeySecret: 'accessKeySecret', // 可选, AccessKeySecret
roleArn: 'roleArn', // 格式: acs:ram::用户ID:role/角色名
oidcProviderArn: 'oidcProviderArn', // 格式: acs:ram::用户Id:oidc-provider/角色名
oidcTokenFilePath: '/Users/xxx/xxx', // 格式: path OIDCTokenFilePath 可不设,但需要通过设置 ALIBABA_CLOUD_OIDC_TOKEN_FILE 来代替
roleSessionName: 'roleSessionName', // 角色会话名称
policy: 'policy', // 可选, 限制 STS Token 的权限
roleSessionExpiration: 3600, // 可选, 限制 STS Token 的有效时间
}
const cred = new Credential(config);
let accessKeyId: string = await cred.getAccessKeyId();
let accessKeySecret: string = await cred.getAccessKeySecret();
let securityToken: string = await cred.getSecurityToken();
let type: string = cred.getType();
通过指定角色名称,让凭证自动申请维护 STS Token
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'ecs_ram_role', // 凭证类型
roleName: 'roleName', // 账户RoleName,非必填,不填则自动获取,建议设置,可以减少请求
}
const cred = new Credential(config);
let accessKeyId: string = await cred.getAccessKeyId();
let accessKeySecret: string = await cred.getAccessKeySecret();
let securityToken: string = await cred.getSecurityToken();
let type: string = cred.getType();
通过指定公钥ID和私钥文件,让凭证自动申请维护 AccessKey。仅支持日本站。 By specifying the public key ID and the private key file, the credential will be able to automatically request maintenance of the AccessKey before sending the request. Only Japan station is supported.
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'rsa_key_pair', // 凭证类型
privateKeyFile: 'privateKeyFile', // PrivateKey文件路径
publicKeyId: 'publicKeyId', // 账户PublicKeyId
}
const cred = new Credential(config);
let accessKeyId: string = await cred.getAccessKeyId();
let accessKeySecret: string = await cred.getAccessKeySecret();
let securityToken: string = await cred.getSecurityToken();
let type: string = cred.getType();
通过本地或者远程的 URI,来获取凭证,并支持自动刷新。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'credentials_uri',
credentialsURI: 'http://a_local_or_remote_address/'
};
const cred = new Credential(config);
let accessKeyId: string = await cred.getAccessKeyId();
let accessKeySecret: string = await cred.getAccessKeySecret();
let securityToken: string = await cred.getSecurityToken();
let type: string = cred.getType();
该地址必须满足如下条件:
{
"Code": "Success",
"AccessKeySecret": "AccessKeySecret",
"AccessKeyId": "AccessKeyId",
"Expiration": "2021-09-26T03:46:38Z",
"SecurityToken": "SecurityToken"
}
如呼叫中心(CCC)需用此凭证,请自行申请维护 Bearer Token。
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
type: 'bearer', // 凭证类型
bearerToken: 'bearerToken', // BearerToken
}
const cred = new Credential(config);
let bearerToken: string = cred.getBearerToken();
let type: string = cred.getType();
如果你调用 new Credential()
时传入空, 将通过凭证提供链来为你获取凭证。
程序首先会在环境变量里寻找环境凭证,如果定义了 ALICLOUD_ACCESS_KEY
和 ALICLOUD_SECRET_KEY
环境变量且不为空,程序将使用他们创建凭证。如否则,程序会在配置文件中加载和寻找凭证。
如果用户主目录存在默认文件 ~/.alibabacloud/credentials
(Windows 为 C:\Users\USER_NAME\.alibabacloud\credentials
),程序会自动创建指定类型和名称的凭证。默认文件可以不存在,但解析错误会抛出异常。不同的项目、工具之间可以共用这个配置文件,因为超出项目之外,也不会被意外提交到版本控制。Windows 上可以使用环境变量引用到主目录 %UserProfile%。类 Unix 的系统可以使用环境变量 $HOME 或 ~ (tilde)。 可以通过定义 ALIBABA_CLOUD_CREDENTIALS_FILE
环境变量修改默认文件的路径。
[default] # 默认凭证
type = access_key # 认证方式为 access_key
access_key_id = foo # access key id
access_key_secret = bar # access key secret
如果定义了环境变量 ALIBABA_CLOUD_ECS_METADATA
且不为空,程序会将该环境变量的值作为角色名称,请求 http://100.100.100.200/latest/meta-data/ram/security-credentials/
获取临时安全凭证。
如果定义了环境变量 ALIBABA_CLOUD_CREDENTIALS_URI
且不为空,程序会将该环境变量的值作为 credentials_uri 模式的地址,在调用时获取临时安全凭证。
Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。