# @aws-sdk/credential-providers
[![NPM version](https://img.shields.io/npm/v/@aws-sdk/credential-providers/latest.svg)](https://www.npmjs.com/package/@aws-sdk/credential-providers)
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/credential-providers.svg)](https://www.npmjs.com/package/@aws-sdk/credential-providers)
A collection of all credential providers, with default clients.
# Table of Contents
1. [From Cognito Identity](#fromcognitoidentity)
1. [From Cognito Identity Pool](#fromcognitoidentitypool)
1. [From Temporary Credentials](#fromtemporarycredentials)
1. [From Web Token](#fromwebtoken)
1. [Examples](#examples)
1. [From Token File](#fromtokenfile)
1. [From Instance and Container Metadata Service](#fromcontainermetadata-and-frominstancemetadata)
1. [From Shared INI files](#fromini)
1. [Sample Files](#sample-files)
1. [From Environmental Variables](#fromenv)
1. [From Credential Process](#fromprocess)
1. [Sample files](#sample-files-1)
1. [From Single Sign-On Service](#fromsso)
1. [Supported Configuration](#supported-configuration)
1. [SSO login with AWS CLI](#sso-login-with-the-aws-cli)
1. [Sample Files](#sample-files-2)
1. [From Node.js default credentials provider chain](#fromNodeProviderChain)
## `fromCognitoIdentity()`
The function `fromCognitoIdentity()` returns `CredentialsProvider` that retrieves credentials for
the provided identity ID. See [GetCredentialsForIdentity API][getcredentialsforidentity_api]
for more information.
```javascript
import { fromCognitoIdentity } from "@aws-sdk/credential-providers"; // ES6 import
// const { fromCognitoIdentity } = require("@aws-sdk/credential-providers"); // CommonJS import
const client = new FooClient({
region,
credentials: fromCognitoIdentity({
// Required. The unique identifier for the identity against which credentials
// will be issued.
identityId: "us-east-1:128d0a74-c82f-4553-916d-90053example",
// Optional. The ARN of the role to be assumed when multiple roles were received in the token
// from the identity provider.
customRoleArn: "arn:aws:iam::1234567890:role/MYAPP-CognitoIdentity",
// Optional. A set of name-value pairs that map provider names to provider tokens.
// Required when using identities associated with external identity providers such as Facebook.
logins: {
"graph.facebook.com": "FBTOKEN",
"www.amazon.com": "AMAZONTOKEN",
"accounts.google.com": "GOOGLETOKEN",
"api.twitter.com": "TWITTERTOKEN'",
"www.digits.com": "DIGITSTOKEN",
},
// Optional. Custom client config if you need overwrite default Cognito Identity client
// configuration.
clientConfig: { region },
}),
});
```
## `fromCognitoIdentityPool()`
The function `fromCognitoIdentityPool()` returns `AwsCredentialIdentityProvider` that calls [GetId API][getid_api]
to obtain an `identityId`, then generates temporary AWS credentials with
[GetCredentialsForIdentity API][getcredentialsforidentity_api], see
[`fromCognitoIdentity()`](#fromcognitoidentity).
Results from `GetId` are cached internally, but results from `GetCredentialsForIdentity` are not.
```javascript
import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers"; // ES6 import
// const { fromCognitoIdentityPool } = require("@aws-sdk/credential-providers"); // CommonJS import
const client = new FooClient({
region,
credentials: fromCognitoIdentityPool({
// Required. The unique identifier for the identity pool from which an identity should be
// retrieved or generated.
identityPoolId: "us-east-1:1699ebc0-7900-4099-b910-2df94f52a030",
// Optional. A standard AWS account ID (9+ digits)
accountId: "123456789",
// Optional. A cache in which to store resolved Cognito IdentityIds.
cache: custom_storage,
// Optional. A unique identifier for the user used to cache Cognito IdentityIds on a per-user
// basis.
userIdentifier: "user_0",
// Optional. The ARN of the role to be assumed when multiple roles were received in the token
// from the identity provider.
customRoleArn: "arn:aws:iam::1234567890:role/MYAPP-CognitoIdentity",
// Optional. A set of name-value pairs that map provider names to provider tokens.
// Required when using identities associated with external identity providers such as Facebook.
logins: {
"graph.facebook.com": "FBTOKEN",
"www.amazon.com": "AMAZONTOKEN",
"accounts.google.com": "GOOGLETOKEN",
"api.twitter.com": "TWITTERTOKEN",
"www.digits.com": "DIGITSTOKEN",
},
// Optional. Custom client config if you need overwrite default Cognito Identity client
// configuration.
clientConfig: { region },
}),
});
```
## `fromTemporaryCredentials()`
The function `fromTemporaryCredentials` returns `AwsCredentialIdentityProvider` that retrieves temporary
credentials from [STS AssumeRole API][assumerole_api].
```javascript
import { fromTemporaryCredentials } from "@aws-sdk/credential-providers"; // ES6 import
// const { fromTemporaryCredentials } = require("@aws-sdk/credential-providers"); // CommonJS import
const client = new FooClient({
region,
credentials: fromTemporaryCredentials({
// Optional. The master credentials used to get and refresh temporary credentials from AWS STS.
// If skipped, it uses the default credential resolved by internal STS client.
masterCredentials: fromTemporaryCredentials({
params: { RoleArn: "arn:aws:iam::1234567890:role/RoleA" },
}),
// Required. Options passed to STS AssumeRole operation.
params: {
// Required. ARN of role to assume.
RoleArn: "arn:aws:iam::1234567890:role/RoleB",
// Optional. An identifier for the assumed role session. If skipped, it generates a random
// session name with prefix of 'aws-sdk-js-'.
RoleSessionName: "aws-sdk-js-123",
// Optional. The duration, in seconds, of the role session.
DurationSeconds: 3600,
// ... For more options see https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html
},
// Optional. Custom STS client configurations overriding the default ones.
clientConfig: { region },
// Optional. A function that returns a promise fulfilled with an MFA token code for the provided
// MFA Serial code. Required if `params` has `SerialNumber` config.
mfaCodeProvider: async (mfaSerial) => {
return "token";
},
}),
});
```
## `fromWebToken()`
The function `fromWebToken` returns `AwsCredentialIdentityProvider` that gets credentials calling
[STS AssumeRoleWithWebIdentity API][assumerolewithwebidentity_api]
```javascript
import { fromWebToken } from "@aws-sdk/credential-providers"; // ES6 import
// const { fromWebToken } = require("@aws-sdk/credential-providers"); // CommonJS import
const client = new FooClient({
region,
credentials: fromWebToken({
// Required. ARN of the role that the caller is assuming.
roleArn: "arn:aws:iam::1234567890:role/RoleA",
// Required. The OAuth 2.0 access token or OpenID Connect ID token that is provided by the
// identity provider.
webIdentityToken: await openIdProvider(),
// Optional. Custom STS client configurations overriding the default ones.
clientConfig: { region },
// Optional. A function that assumes a role with web identity and returns a promise fulfilled
// with credentials for the assumed role.
roleAssumerWithWebIdentity,
// Optional. An identifier for the assumed role session.
roleSessionName: "session_123",
// Optional. The fully qualified host component of the domain name of the identity provider.
providerId: "graph.facebook.com",
// Optional. ARNs of the IAM managed policies that you want to use as managed session.
policyArns: [{ arn: "arn:aws:iam::1234567890:policy/SomePolicy" }],
// Optional. An IAM policy in JSON format that you want to use as an inline session policy.
policy: "JSON_STRING",
// Optional. The dur
node.js后台搭建
需积分: 0 150 浏览量
更新于2023-02-04
收藏 5.02MB RAR 举报
Node.js是一种基于Chrome V8引擎的JavaScript运行环境,它允许开发者使用JavaScript进行服务器端编程,极大地推动了Web开发领域的创新。在"node.js后台搭建"这个主题中,我们将深入探讨如何利用Node.js来构建强大的后端服务。
我们要了解Node.js的核心特性:非阻塞I/O模型和事件驱动。这使得Node.js在处理高并发请求时表现出色,因为它能够有效地管理多个并发连接,而无需为每个连接创建新的线程。这种轻量级和高效的工作方式使得Node.js成为构建实时应用的理想选择,如聊天应用、协作工具或实时数据更新的应用。
搭建Node.js后台的第一步是安装Node.js。你可以访问Node.js官方网站下载适合你操作系统的版本,并按照安装向导进行操作。安装完成后,确保在命令行环境中可以运行`node -v`命令,检查Node.js是否成功安装。
接下来,我们需要创建一个新的项目目录。在命令行中,使用`mkdir`命令创建一个名为“my_node_backend”的目录,然后使用`cd`命令进入该目录。在项目目录中,通过`npm init`初始化一个新的Node.js项目。这个命令会引导你输入项目的基本信息,如项目名、版本、作者等,并生成一个`package.json`文件,用于管理项目的依赖。
为了实现后端功能,我们需要引入必要的模块。Node.js的包管理器npm提供了大量的开源库。例如,`express`是一个流行的Web应用框架,用于简化HTTP服务器的创建。在项目目录下,使用`npm install express --save`安装Express。安装完成后,你可以在项目中引入并使用它。
在项目中创建一个名为`app.js`的文件,这是你的主入口文件。在其中,你可以写入以下代码来创建一个基本的Express服务器:
```javascript
const express = require('express');
const app = express();
// 设置一个简单的路由
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// 监听指定端口
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
```
这段代码导入了Express模块,创建了一个应用实例,定义了一个返回"Hello, World!"的路由,并启动了一个监听3000端口的服务器。运行`node app.js`,你的Node.js后台服务就启动了。
对于"dbServer"这个子文件夹,我们可以推测这是一个与数据库交互的组件。在Node.js中,有许多数据库适配器,如`mongoose`(用于MongoDB)、`sequelize`(用于SQL数据库)等。假设我们使用`mongoose`,首先需要安装`mongoose`库,通过`npm install mongoose --save`。然后在`dbServer`目录下的文件中,可以配置数据库连接,定义数据模型,并实现增删查改的操作。
以上只是Node.js后台搭建的基础步骤,实际项目中可能涉及更多复杂功能,如中间件、路由分发、错误处理、API设计、认证授权、日志管理等。Node.js的生态系统丰富多样,不断有新的库和工具涌现,让开发者能够构建出高效、可扩展的后台系统。随着你对Node.js理解的深入,你可以根据项目需求选择合适的库和最佳实践,构建出满足业务需求的Node.js后台服务。
.桃子摇摇冰
- 粉丝: 24
- 资源: 3
最新资源
- 基于强化学习的多目标跟踪器Matlab代码.rar
- 基于线的扩展卡尔曼滤波器用于机器人在V-Rep上的定位Matlab代码.rar
- 基于有限差分法计算固结过程中单层和双层排水的孔隙水压力随时间的变化Matlab实现.rar
- 基于通信的无人机集群任务Matlab代码.rar
- 基于最大最小特征值(CMME)组合的认知无线电频谱感知 matlab代码.rar
- 计算高斯光束束腰半径调节Matlab代码.rar
- 计算无人机航程和续航力的matlab项目 matlab代码.rar
- 建立了连续时间EKF、混合EKF和离散时间EKF的性能比较Matlab程序.rar
- 卡尔曼滤波器设计与Matlab仿真。考虑了静态和时变卡尔曼滤波器.rar
- 卡尔曼滤波器 EKF 和 SLAM附Matlab代码.rar
- 考虑表面形貌对研究的影响时,模拟出生高斯或非高斯表面 matlab代码.rar
- 扩展卡尔曼滤波器Matlab代码.rar
- 开发计算扭矩控制器、计算扭矩控制器以及具有恒定、抛物线和正弦参考的 EKF 和 Li-Slotine 控制器.rar
- 离散E的电容式设施选址问题的深度强化学习Matlab代码.rar
- 扩展卡尔曼滤波器同步本地化方法的情感评价模块(ECF-SAEM)Matlab代码.rar
- 田螺去尾巴机3D图纸和工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip