介绍
gs-aws-helpers
是一个 NPM 包,提供了一系列基于 AWS SDK 的前端开发常用工具和帮助函数,例如对 Cognito、Lambda、S3 等 AWS 服务的高层封装、通用异常捕获和处理、请求拦截等功能。本文将详细介绍如何使用此包。
安装
第一步是在 package.json 中添加 gs-aws-helpers 的依赖,然后在项目目录下运行 npm install
。
"dependencies": { "gs-aws-helpers": "^1.0.0" }
使用
配置
在使用 gs-aws-helpers 前,需要先配置 AWS SDK 的访问密钥和部分参数,可以在项目的配置文件(如 .env
)中设置以下环境变量:
AWS_ACCESS_KEY_ID = ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY = SECRET_ACCESS_KEY AWS_REGION = REGION
Cognito
使用 gs-aws-helpers 的 Cognito 类能够让你更方便地进行用户身份验证和管理,下面是几个常用方法和示例代码:
初始化
import { Cognito } from 'gs-aws-helpers' const cognito = new Cognito({ userPoolId: 'YOUR_USER_POOL_ID', clientId: 'YOUR_APP_CLIENT_ID' })
注册
-- -------------------- ---- ------- ---------------- --------- ---------------- --------- ---------------- ------ ------------ ----------- -- - ---------------- ------------ -- - ---------------- --
登录
cognito.login({ username: 'your_username', password: 'your_password' }).then(res => { console.log(res) }).catch(err => { console.log(err) })
获取当前用户信息
cognito.getCurrentUserInfo().then(res => { console.log(res) }).catch(err => { console.log(err) })
Lambda
gs-aws-helpers 的 Lambda 类可以让你更方便地调用 AWS Lambda 函数,支持异步和同步两种调用方式,示例代码如下:
-- -------------------- ---- ------- ------ - ------ - ---- ---------------- ----- ------ - --- -------- ------------- ---------------------------- --------------- ----------------------------- -- -- ---- -------------------- -------- - ----- ------------------- - ----------- -- - ---------------- ------------ -- - ---------------- -- -- ---- --------------- -------- --- -- --- --------------- ----------------- ----------- -- - ---------------- ------------ -- - ---------------- --
S3
gs-aws-helpers 的 S3 类提供了便于使用的 S3 桶操作函数,支持上传、下载、删除等操作,示例代码如下:
-- -------------------- ---- ------- ------ - -- - ---- ---------------- ----- -- - --- ---- ----------- ---------------------- ------------- ----------------------- -- -- ----- -- --------------- --------- ----------------- ------------ ------------------- ----------- -- - ---------------- ------------ -- - ---------------- -- -- ---- ----------------- --------- ---------------- ----------- -- - ---------------- ------------ -- - ---------------- -- -- ---- --------------- --------- ---------------- ----------- -- - ---------------- ------------ -- - ---------------- --
异常处理
gs-aws-helpers 通过对 AWS SDK 的封装,能够自动捕获并处理大部分可能出现的异常,如果出现错误,将自动 throw 一个带有清晰错误信息的异常对象,例如:
-- -------------------- ---- ------- -- ------- -------- - ----- --------------------------------- -------- --- ------- ---- --- ----- ----- ------- -------- - -- ------ ---- - ----- --------------------------------- -------- --------- ----- --- ----- - -------- -
请求拦截
在每个请求发出前,gs-aws-helpers 会通过 Axios 拦截器,添加请求唯一 ID、Token 等信息。你也可以自行添加拦截器,比如打印请求日志:
-- -------------------- ---- ------- ------ ----- ---- ------- ------ - ------------ - ---- ---------------- ------------------------------------- -- - --------------------------------------------- --------------- ------ ------ -- ------------------------
总结
在本文中,我们详细介绍了如何使用 gs-aws-helpers 包来简化 Cognito、Lambda、S3 等 AWS 服务的开发,同时也介绍了如何捕获 AWS SDK 的异常并添加请求拦截器。希望此文能够对您的 AWS 前端开发工作有所帮助。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005757781e8991b448ea5cb