# @alicloud/credentials 中文文档

规范地址：https://npmcn.com/package/%40alicloud/credentials/

版本：2\.4\.7
npm 元数据更新时间：2026-08-18T04:38:59.062Z
中文译文更新时间：2026-11-05T17:23:43.337Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40alicloud/credentials
原文对照：https://npmcn.com/package/%40alicloud/credentials/?lang=original
项目主页：https://github.com/aliyun/nodejs-credentials#readme
源码仓库：https://github.com/aliyun/nodejs-credentials

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：410 kB，172 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：4 个
- 首次发布：2019-03-01T05:11:31.977Z
- 最新版本发布：2026-08-18T04:38:58.889Z
- 近 30 天下载量：581033（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：25（近 12 个月 2 个）

## 中文简介

alibaba 云端.js sdk 证书

## 安装

```sh
npm install @alicloud/credentials
```

也可以使用：`pnpm add @alicloud/credentials`、`yarn add @alicloud/credentials`、`bun add @alicloud/credentials`

## README

页:1 [简体中文](https://npmcn.com/package/%40alicloud/credentials/README-CN.md)

# Alibaba Cloud 类型脚本/节点的证书

[![npm version](https://badge.fury.io/js/@alicloud%2fcredentials.svg)](https://www.npmjs.com/package/@alicloud/credentials) [![NodeJS](https://github.com/aliyun/credentials-nodejs/actions/workflows/ci.yml/badge.svg)](https://github.com/aliyun/credentials-nodejs/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/aliyun/credentials-nodejs/branch/master/graph/badge.svg)](https://codecov.io/gh/aliyun/credentials-nodejs) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://npmcn.com/package/%40alicloud/credentials/LICENSE)

## 安装

```bash
npm install @alicloud/credentials
```

**节点.js 12** 所需经费.

## 快速示例

在你开始之前,你需要报名 一个阿里巴巴云账户 并收回你的 [全权证书](https://usercenter.console.aliyun.com/#/manage/ak)。 。 。 .

### 证书类型

#### 访问键

设置访问权限 密钥证书通过 [用户信息管理](https://usercenter.console.aliyun.com/#/manage/ak)它对账户有全权 请保持安全 有时出于安全考虑,您无法将一个主账户AccessKey完全接入到项目开发者手中. 您可以创建子账户 [RAM 子账户](https://ram.console.aliyun.com/users) ,给予 [授权](https://ram.console.aliyun.com/permissions),并使用RAM子账户的 AccessKey.

```ts
import Credential, { Config } from '@alicloud/credentials';

const config: Config = {
  type:               'access_key',       // credential type
  accessKeyId:        'accessKeyId',      // AccessKeyId of your account
  accessKeySecret:    'accessKeySecret',  // AccessKeySecret of your account
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  type
} = await cred.getCredential();
```

#### 标准

通过安全托肯服务(STS)应用临时安全证书(TSC)创建临时安全证书.

```ts
import Credential, { Config } from '@alicloud/credentials';

const config: Config = {
  type:                  'sts',             // credential type
  accessKeyId:           'accessKeyId',     // AccessKeyId of your account
  accessKeySecret:       'accessKeySecret', // AccessKeySecret of your account
  securityToken:         'securityToken',   // Temporary Security Token
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();
```

#### 公尺( r)

通过指定 [内存角色](https://ram.console.aliyun.com/#/role/list),证书可以自动请求STS Token的维护. 如果您想要限制权限( I)[如何制定政策](https://help.aliyun.com/zh/ram/user-guide/policy-management/)STS)),该型号. Token, 您可以为 `Policy`。 。 。 .

```ts
import Credential, { Config } from '@alicloud/credentials';

const config: Config = {
  type:                 'ram_role_arn',     // credential type
  accessKeyId:          'accessKeyId',      // AccessKeyId of your account
  accessKeySecret:      'accessKeySecret',  // AccessKeySecret of your account
  roleArn:              'roleArn',          // Format: acs:ram::USER_ID:role/ROLE_NAME
  roleSessionName:      'roleSessionName',  // Role Session Name
  policy:               'policy',           // Not required, limit the permissions of STS Token
  roleSessionExpiration: 3600,              // Not required, limit the Valid time of STS Token
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();
```

#### oidc 作用 arn

通过指定 [OIDC 角色](https://help.aliyun.com/zh/ram/user-guide/role-based-sso-by-using-oidc),证书可以自动请求STS Token的维护. 如果您想要限制权限( I)[如何制定政策](https://help.aliyun.com/zh/ram/user-guide/policy-management/)STS)),该型号. Token, 您可以为 `Policy`。 。 。 .

```ts
import Credential, { Config } from '@alicloud/credentials';

const config: Config = {
  type:                   'oidc_role_arn',    // credential type
  roleArn:                'roleArn',          // Format: acs:ram::USER_Id:role/ROLE_NAME roleArn can be replaced by setting environment variable: ALIBABA_CLOUD_ROLE_ARN
  oidcProviderArn:        'oidcProviderArn',  // Format: acs:ram::USER_Id:oidc-provider/ROLE_NAME oidcProviderArn can be replaced by setting environment variable: ALIBABA_CLOUD_OIDC_PROVIDER_ARN
  oidcTokenFilePath:      '/Users/xxx/xxx',   // Format: path  OIDCTokenFilePath can be replaced by setting environment variable: ALIBABA_CLOUD_OIDC_TOKEN_FILE
  roleSessionName:        'roleSessionName',  // Role Session Name
  policy:                 'policy',           // Not required, limit the permissions of STS Token
  roleSessionExpiration:   3600,              // Not required, limit the Valid time of STS Token
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();
```

#### 区域( r)

通过指定角色名,证书可以自动请求STS Token的维护.

```ts
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
  type:                 'ecs_ram_role',       // credential type
  roleName:             'roleName',           // `roleName` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests.
  disableIMDSv1:         true,                 // `disableIMDSv1` is optional and is recommended to be turned on. It can be replaced by setting environment variable: ALIBABA_CLOUD_IMDSV1_DISABLED
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();
```

#### rsa key pair

通过指定公钥ID和私钥文件,证书可以在发送请求前自动请求AccessKey的维护. 仅支持日本站.

```ts
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
  type:                     'rsa_key_pair',       // credential type
  privateKeyFile:           'privateKeyFile',     // The file path to store the PrivateKey
  publicKeyId:              'publicKeyId',        // PublicKeyId of your account
}
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();
```

#### 证书 uri

通过指定本地或远程 URI 获取证书并自动刷新 .

```ts
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
  type: 'credentials_uri',
  credentialsURI: 'http://a_local_or_remote_address/'  //credentialsURI can be replaced by setting environment variable: ALIBABA_CLOUD_CREDENTIALS_URI
};
const cred = new Credential(config);
const {
  accessKeyId,
  accessKeySecret,
  securityToken,
  type
} = await cred.getCredential();
```

URI必须满足以下条件:

-   响应状态代码为 200
-   反应机构结构必须是:

```json
{
  "Code": "Success",
  "AccessKeySecret": "AccessKeySecret",
  "AccessKeyId": "AccessKeyId",
  "Expiration": "2021-09-26T03:46:38Z",
  "SecurityToken": "SecurityToken"
}
```

#### 无记名者

如果云召中心(CCC)需要证明,请自行申请Bearer Token维修.

```ts
import Credential, { Config } from '@alicloud/credentials';
const config: Config = {
  type:                 'bearer',            // credential type
  bearerToken:          'bearerToken',       // BearerToken of your account
}
const cred = new Credential(config);
let bearerToken: string = await cred.getBearerToken();
let type: string = cred.getType();
```

### 提供者

如果你打电话 `new Credential()` 空的,它会使用提供者链来获取证书.

#### 1\. 环境证书

程序首先在环境变量中查找环境证书. 如果 `ALIBABA_CLOUD_ACCESS_KEY_ID` 财务报告和审定财务报表 `ALIBABA_CLOUD_ACCESS_KEY_SECRET` 环境变量被定义且不是空的,程序将使用它们来创建默认的证书. 如果没有,程序会在配置文件中加载并寻找客户端.

#### 2\. 配置文件

如果有的话 `~/.alibabacloud/credentials` 默认文件( 窗口显示) `C:\Users\USER_NAME\.alibabacloud\credentials`),该程序会自动以"默认"的名称创建证书. 默认文件可能不存在, 但解析错误会丢出例外 。 指定的文件也可以被无限期地加载: `AlibabaCloud::load('/data/credentials', 'vfs://AlibabaCloud/credentials', ...);` 这个配置文件可以在不同的项目之间和不同的工具之间共享. 因为它在项目之外,不会被意外地承诺用于版本控制. 环境变量可以在Windows上用于指代主目录 %UserProfile%. Unix-like系统可以使用环境变量$HOME或~(tilde). 默认文件的路径可以通过定义 `ALIBABA_CLOUD_CREDENTIALS_FILE` 环境变量.

```ini
[default]                          # Default credential
type = access_key                  # Certification type: access_key
access_key_id = foo                # access key id
access_key_secret = bar            # access key secret
```

#### 3\. 实例记录和档案管理作用

如果环境变量 `ALIBABA_CLOUD_ECS_METADATA` 定义而非空,程序将把环境变量的值作为角色名并请求 `http://100.100.100.200/latest/meta-data/ram/security-credentials/` 拿到临时安全证书.

#### 4\. 全权证书统一登记制度

如果环境变量 `ALIBABA_CLOUD_CREDENTIALS_URI` 定义而非空, 程序将把环境变量的值作为证书 ur.

## 测试覆盖( S)

-   运行测试

```sh
npm run test
```

-   运行代码覆盖

```sh
npm run cov
```

## 许可证

[麻省理工学院](https://npmcn.com/package/%40alicloud/credentials/LICENSE)

版权 (c) 2009年至今,阿里巴巴云 版权所有.

## 常见问题

### @alicloud/credentials 是什么？

alibaba 云端\.js sdk 证书

### 如何安装 @alicloud/credentials？

在项目目录运行 npm install @alicloud/credentials，或使用 pnpm add / yarn add / bun add。

### @alicloud/credentials 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @alicloud/credentials 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @alicloud/credentials 可以商用吗？

@alicloud/credentials 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @alicloud/credentials 的下载量有多少？

根据 npm 官方下载统计，@alicloud/credentials 在 2026/10/06 – 2026/11/04 这 30 天内被下载 581,033 次，日均约 19,368 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @alicloud/credentials 还在维护吗？

最新版本 2\.4\.7 发布于 2026/08/18（2 个月前）；至今共发布 25 个版本，其中过去 12 个月发布了 2 个；首次发布于 2019/03/01。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

