123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- const argv = require('yargs').argv
- /**
- * @desc
- curl -X POST -H "Content-Type: application/json" -H 'X-Parse-Application-Id: edu-textbook' http://8.140.98.43/parse/functions/aliOssSTS
- *
- */
- export function defineAliOssSTS(){
- // function defineAuthingLogin(){
- Parse.Cloud.define("aliOssSTS", async (request) => {
- let result = await getAliyunOssAccessToken()
- return result
- });
- }
- const ALI_OSS_BUCKET = process.env.ALI_OSS_BUCKET || "hep-textbook"
- const ALI_OSS_ACCESS_KEY_ID = process.env.ALI_OSS_ACCESS_KEY_ID || "LTAI5t6AbTiAvXmeoVdJZhL3"
- const ALI_OSS_ACCESS_KEY_SECRET = process.env.ALI_OSS_ACCESS_KEY_SECRET || "KLtQRdIW69KLP7jnzHNUf7eKmdptxH"
- // 通过STS服务生成临时访问凭证。临时访问凭证包括临时访问密钥(AccessKeyId和AccessKeySecret)和安全令牌(SecurityToken)。
- const { STS } = require('ali-oss');
- const sts = new STS({
- // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量ALI_OSS_ACCESS_KEY_ID和ALI_OSS_ACCESS_KEY_SECRET。
- accessKeyId: ALI_OSS_ACCESS_KEY_ID,
- accessKeySecret: ALI_OSS_ACCESS_KEY_SECRET
- });
- function getAliyunOssAccessToken(){
- // roleArn填写角色ARN。
- // policy填写自定义权限策略。
- // expiration用于设置临时访问凭证有效时间单位为秒,最小值为900,最大值以当前角色设定的最大会话时间为准。
- // sessionName用于自定义角色会话名称,用来区分不同的令牌,例如填写为SessionTest。
- // let result = await sts.assumeRole('acs:ram::137918634953****:role/ossram', `{
- // "Version": "1",
- // "Statement": [
- // {
- // "Effect": "Allow",
- // "Action": [
- // "oss:*"
- // ],
- // "Resource": [
- // "acs:oss:*:*:${ALI_OSS_BUCKET}",
- // "acs:oss:*:*:${ALI_OSS_BUCKET}/*"
- // ]
- // }
- // ]
- // }`, '3600', 'SessionTest' )
- // return result.credentials
- return {
- bucket: ALI_OSS_BUCKET,
- AccessKeyId: ALI_OSS_ACCESS_KEY_ID,
- AccessKeySecret:ALI_OSS_ACCESS_KEY_SECRET,
- SecurityToken: null,
- Expiration: null
- }
- // AccessKeyId: result.credentials.AccessKeyId,
- // AccessKeySecret: result.credentials.AccessKeySecret,
- // SecurityToken: result.credentials.SecurityToken,
- // Expiration: result.credentials.Expiration
-
- }
- module.exports.getAliyunOssAccessToken = getAliyunOssAccessToken
- if(argv?.test){
- getAliyunOssAccessToken()
- }
|