From 9135fc097be38a1d09a215df29d1b6e88c2587b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E6=9C=9D=E4=BC=9F?= <2211960668@qq.com> Date: Tue, 21 Nov 2023 15:35:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=A9=E7=BF=BCyun?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es/CTYunSDK.d.ts | 9 +++ es/CTYunSDK.js | 20 ++++++ es/index.d.ts | 3 +- es/index.js | 3 +- es/service/ctyun/CTYun.d.ts | 13 ++++ es/service/ctyun/CTYun.js | 91 ++++++++++++++++++++++++ es/types/ctyun.d.ts | 6 ++ es/types/ctyun.js | 1 + es/types/index.d.ts | 1 + es/types/index.js | 1 + lib/CTYunSDK.d.ts | 9 +++ lib/CTYunSDK.js | 23 +++++++ lib/index.d.ts | 3 +- lib/index.js | 5 +- lib/service/ctyun/CTYun.d.ts | 13 ++++ lib/service/ctyun/CTYun.js | 96 ++++++++++++++++++++++++++ lib/types/ctyun.d.ts | 6 ++ lib/types/ctyun.js | 2 + lib/types/index.d.ts | 1 + lib/types/index.js | 1 + package.json | 1 + src/CTYunSDK.ts | 29 ++++++++ src/index.ts | 3 + src/service/ctyun/CTYun.ts | 129 +++++++++++++++++++++++++++++++++++ src/types/CTYun.ts | 8 +++ src/types/index.ts | 3 +- 26 files changed, 475 insertions(+), 5 deletions(-) create mode 100644 es/CTYunSDK.d.ts create mode 100644 es/CTYunSDK.js create mode 100644 es/service/ctyun/CTYun.d.ts create mode 100644 es/service/ctyun/CTYun.js create mode 100644 es/types/ctyun.d.ts create mode 100644 es/types/ctyun.js create mode 100644 lib/CTYunSDK.d.ts create mode 100644 lib/CTYunSDK.js create mode 100644 lib/service/ctyun/CTYun.d.ts create mode 100644 lib/service/ctyun/CTYun.js create mode 100644 lib/types/ctyun.d.ts create mode 100644 lib/types/ctyun.js create mode 100644 src/CTYunSDK.ts create mode 100644 src/service/ctyun/CTYun.ts create mode 100644 src/types/CTYun.ts diff --git a/es/CTYunSDK.d.ts b/es/CTYunSDK.d.ts new file mode 100644 index 0000000..3cb786a --- /dev/null +++ b/es/CTYunSDK.d.ts @@ -0,0 +1,9 @@ +import { CTYunInstance } from './service/ctyun/CTYun'; +declare class CTYunSDK { + ctyunMap: Record; + constructor(); + getInstance(accessKey: string, accessSecret: string): CTYunInstance; +} +declare const SDK: CTYunSDK; +export default SDK; +export { CTYunInstance }; diff --git a/es/CTYunSDK.js b/es/CTYunSDK.js new file mode 100644 index 0000000..4f2bad0 --- /dev/null +++ b/es/CTYunSDK.js @@ -0,0 +1,20 @@ +import { CTYunInstance } from './service/ctyun/CTYun'; +class CTYunSDK { + ctyunMap; + constructor() { + this.ctyunMap = {}; + } + getInstance(accessKey, accessSecret) { + if (this.ctyunMap[accessKey]) { + return this.ctyunMap[accessKey]; + } + const instance = new CTYunInstance(accessKey, accessSecret); + Object.assign(this.ctyunMap, { + [accessKey]: instance, + }); + return instance; + } +} +const SDK = new CTYunSDK(); +export default SDK; +export { CTYunInstance }; diff --git a/es/index.d.ts b/es/index.d.ts index 20fd28f..beb8eee 100644 --- a/es/index.d.ts +++ b/es/index.d.ts @@ -2,6 +2,7 @@ import WechatSDK, { WechatMpInstance, WechatPublicInstance, WechatWebInstance } import AmapSDK from './AmapSDK'; import QiniuSDK, { QiniuCloudInstance } from './QiniuSDK'; import SmsSdk, { TencentSmsInstance, AliSmsInstance } from './SmsSdk'; +import CTYunSDk, { CTYunInstance } from './CTYunSDK'; export * from './service/amap/Amap'; -export { AmapSDK, QiniuSDK, WechatSDK, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, }; +export { AmapSDK, QiniuSDK, WechatSDK, CTYunSDk, CTYunInstance, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, }; export * from './types'; diff --git a/es/index.js b/es/index.js index 20fd28f..beb8eee 100644 --- a/es/index.js +++ b/es/index.js @@ -2,6 +2,7 @@ import WechatSDK, { WechatMpInstance, WechatPublicInstance, WechatWebInstance } import AmapSDK from './AmapSDK'; import QiniuSDK, { QiniuCloudInstance } from './QiniuSDK'; import SmsSdk, { TencentSmsInstance, AliSmsInstance } from './SmsSdk'; +import CTYunSDk, { CTYunInstance } from './CTYunSDK'; export * from './service/amap/Amap'; -export { AmapSDK, QiniuSDK, WechatSDK, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, }; +export { AmapSDK, QiniuSDK, WechatSDK, CTYunSDk, CTYunInstance, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, }; export * from './types'; diff --git a/es/service/ctyun/CTYun.d.ts b/es/service/ctyun/CTYun.d.ts new file mode 100644 index 0000000..86d7b56 --- /dev/null +++ b/es/service/ctyun/CTYun.d.ts @@ -0,0 +1,13 @@ +import { Action, CTYunZone } from '../../types/CTYun'; +export declare class CTYunInstance { + private accessKey; + private secretKey; + constructor(accessKey: string, secretKey: string); + getUploadInfo(bucket: string, zone: CTYunZone, key?: string, actions?: Action[]): { + key: string | undefined; + uploadToken: void; + uploadHost: string; + bucket: string; + }; + getToken(zone: CTYunZone, bucket: string, actions?: Action[]): void; +} diff --git a/es/service/ctyun/CTYun.js b/es/service/ctyun/CTYun.js new file mode 100644 index 0000000..ec063ff --- /dev/null +++ b/es/service/ctyun/CTYun.js @@ -0,0 +1,91 @@ +import AWS from 'aws-sdk'; +const CTYun_ENDPOINT_LIST = { + hazz: { + ul: 'oos-hazz.ctyunapi.cn', + }, + lnsy: { + ul: 'oos-lnsy.ctyunapi.cn', + }, + sccd: { + ul: 'oos-sccd.ctyunapi.cn', + }, + xjwlmq: { + ul: 'oos-xjwlmq.ctyunapi.cn', + }, + gslz: { + ul: 'oos-gslz.ctyunapi.cn', + }, + sdqd: { + ul: 'oos-sdqd.ctyunapi.cn', + }, + gzgy: { + ul: 'oos-gzgy.ctyunapi.cn', + }, + hbwh: { + ul: 'oos-gslz.ctyunapi.cn', + }, + xzls: { + ul: 'oos-xzls.ctyunapi.cn', + }, + ahwh: { + ul: 'oos-ahwh.ctyunapi.cn', + }, + gdsz: { + ul: 'oos-gdsz.ctyunapi.cn', + }, + jssz: { + ul: 'oos-jssz.ctyunapi.cn', + }, + sh2: { + ul: 'oos-sh2.ctyunapi.cn', + }, +}; +export class CTYunInstance { + accessKey; + secretKey; + constructor(accessKey, secretKey) { + this.accessKey = accessKey; + this.secretKey = secretKey; + } + getUploadInfo(bucket, zone, key, actions) { + try { + const uploadToken = this.getToken(zone, bucket, actions); + return { + key, + uploadToken, + uploadHost: `https://${CTYun_ENDPOINT_LIST[zone].ul}`, + bucket, + }; + } + catch (err) { + throw err; + } + } + getToken(zone, bucket, actions) { + const config = { + accessKeyId: this.accessKey, + secretAccessKey: this.secretKey, + endpoint: `http://${CTYun_ENDPOINT_LIST[zone].ul}`, + region: "ctyun", + }; + const stsClient = new AWS.STS(config); + const actions2 = actions ? actions.map((ele) => `s3:${ele}`) : ['s3:*']; + const params = { + Policy: `{"Version":"2012-10-17","Statement":{"Effect":"Allow","A +ction":${actions2},"Resource":["arn:aws:s3:::${bucket}","arn:aws:s +3:::${bucket}/*"]}}`, + RoleArn: "arn:aws:iam:::role/oak", + RoleSessionName: "oak", + DurationSeconds: 900, // 过期时间 + }; + stsClient.assumeRole(params, (err, data) => { + if (err) { + throw err; + } + else { + console.log('success', data); + return data; + } + }); + } +} diff --git a/es/types/ctyun.d.ts b/es/types/ctyun.d.ts new file mode 100644 index 0000000..f131fab --- /dev/null +++ b/es/types/ctyun.d.ts @@ -0,0 +1,6 @@ +/** + * 此处记录ctyun操作权限名称,更多操作权限参考下方链接 + * https://www.ctyun.cn/document/10306929/10136179 + */ +export type Action = '*' | 'PutObject' | 'GetObject' | 'DeleteObject' | 'ListBucket'; +export type CTYunZone = 'hazz' | 'lnsy' | 'sccd' | 'xjwlmq' | 'gslz' | 'sdqd' | 'gzgy' | 'hbwh' | 'xzls' | 'ahwh' | 'gdsz' | 'jssz' | 'sh2'; diff --git a/es/types/ctyun.js b/es/types/ctyun.js new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/es/types/ctyun.js @@ -0,0 +1 @@ +export {}; diff --git a/es/types/index.d.ts b/es/types/index.d.ts index 69902fc..0763376 100644 --- a/es/types/index.d.ts +++ b/es/types/index.d.ts @@ -1,2 +1,3 @@ export * from './Wechat'; export * from './Qiniu'; +export * from './CTYun'; diff --git a/es/types/index.js b/es/types/index.js index 69902fc..0763376 100644 --- a/es/types/index.js +++ b/es/types/index.js @@ -1,2 +1,3 @@ export * from './Wechat'; export * from './Qiniu'; +export * from './CTYun'; diff --git a/lib/CTYunSDK.d.ts b/lib/CTYunSDK.d.ts new file mode 100644 index 0000000..3cb786a --- /dev/null +++ b/lib/CTYunSDK.d.ts @@ -0,0 +1,9 @@ +import { CTYunInstance } from './service/ctyun/CTYun'; +declare class CTYunSDK { + ctyunMap: Record; + constructor(); + getInstance(accessKey: string, accessSecret: string): CTYunInstance; +} +declare const SDK: CTYunSDK; +export default SDK; +export { CTYunInstance }; diff --git a/lib/CTYunSDK.js b/lib/CTYunSDK.js new file mode 100644 index 0000000..b75a368 --- /dev/null +++ b/lib/CTYunSDK.js @@ -0,0 +1,23 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.CTYunInstance = void 0; +const CTYun_1 = require("./service/ctyun/CTYun"); +Object.defineProperty(exports, "CTYunInstance", { enumerable: true, get: function () { return CTYun_1.CTYunInstance; } }); +class CTYunSDK { + ctyunMap; + constructor() { + this.ctyunMap = {}; + } + getInstance(accessKey, accessSecret) { + if (this.ctyunMap[accessKey]) { + return this.ctyunMap[accessKey]; + } + const instance = new CTYun_1.CTYunInstance(accessKey, accessSecret); + Object.assign(this.ctyunMap, { + [accessKey]: instance, + }); + return instance; + } +} +const SDK = new CTYunSDK(); +exports.default = SDK; diff --git a/lib/index.d.ts b/lib/index.d.ts index 20fd28f..beb8eee 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -2,6 +2,7 @@ import WechatSDK, { WechatMpInstance, WechatPublicInstance, WechatWebInstance } import AmapSDK from './AmapSDK'; import QiniuSDK, { QiniuCloudInstance } from './QiniuSDK'; import SmsSdk, { TencentSmsInstance, AliSmsInstance } from './SmsSdk'; +import CTYunSDk, { CTYunInstance } from './CTYunSDK'; export * from './service/amap/Amap'; -export { AmapSDK, QiniuSDK, WechatSDK, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, }; +export { AmapSDK, QiniuSDK, WechatSDK, CTYunSDk, CTYunInstance, WechatMpInstance, WechatPublicInstance, WechatWebInstance, QiniuCloudInstance, SmsSdk, TencentSmsInstance, AliSmsInstance, }; export * from './types'; diff --git a/lib/index.js b/lib/index.js index 07e3540..0d95207 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.AliSmsInstance = exports.TencentSmsInstance = exports.SmsSdk = exports.QiniuCloudInstance = exports.WechatWebInstance = exports.WechatPublicInstance = exports.WechatMpInstance = exports.WechatSDK = exports.QiniuSDK = exports.AmapSDK = void 0; +exports.AliSmsInstance = exports.TencentSmsInstance = exports.SmsSdk = exports.QiniuCloudInstance = exports.WechatWebInstance = exports.WechatPublicInstance = exports.WechatMpInstance = exports.CTYunInstance = exports.CTYunSDk = exports.WechatSDK = exports.QiniuSDK = exports.AmapSDK = void 0; const tslib_1 = require("tslib"); const WechatSDK_1 = tslib_1.__importStar(require("./WechatSDK")); exports.WechatSDK = WechatSDK_1.default; @@ -16,5 +16,8 @@ const SmsSdk_1 = tslib_1.__importStar(require("./SmsSdk")); exports.SmsSdk = SmsSdk_1.default; Object.defineProperty(exports, "TencentSmsInstance", { enumerable: true, get: function () { return SmsSdk_1.TencentSmsInstance; } }); Object.defineProperty(exports, "AliSmsInstance", { enumerable: true, get: function () { return SmsSdk_1.AliSmsInstance; } }); +const CTYunSDK_1 = tslib_1.__importStar(require("./CTYunSDK")); +exports.CTYunSDk = CTYunSDK_1.default; +Object.defineProperty(exports, "CTYunInstance", { enumerable: true, get: function () { return CTYunSDK_1.CTYunInstance; } }); tslib_1.__exportStar(require("./service/amap/Amap"), exports); tslib_1.__exportStar(require("./types"), exports); diff --git a/lib/service/ctyun/CTYun.d.ts b/lib/service/ctyun/CTYun.d.ts new file mode 100644 index 0000000..86d7b56 --- /dev/null +++ b/lib/service/ctyun/CTYun.d.ts @@ -0,0 +1,13 @@ +import { Action, CTYunZone } from '../../types/CTYun'; +export declare class CTYunInstance { + private accessKey; + private secretKey; + constructor(accessKey: string, secretKey: string); + getUploadInfo(bucket: string, zone: CTYunZone, key?: string, actions?: Action[]): { + key: string | undefined; + uploadToken: void; + uploadHost: string; + bucket: string; + }; + getToken(zone: CTYunZone, bucket: string, actions?: Action[]): void; +} diff --git a/lib/service/ctyun/CTYun.js b/lib/service/ctyun/CTYun.js new file mode 100644 index 0000000..22ff958 --- /dev/null +++ b/lib/service/ctyun/CTYun.js @@ -0,0 +1,96 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.CTYunInstance = void 0; +const tslib_1 = require("tslib"); +const aws_sdk_1 = tslib_1.__importDefault(require("aws-sdk")); +const CTYun_ENDPOINT_LIST = { + hazz: { + ul: 'oos-hazz.ctyunapi.cn', + }, + lnsy: { + ul: 'oos-lnsy.ctyunapi.cn', + }, + sccd: { + ul: 'oos-sccd.ctyunapi.cn', + }, + xjwlmq: { + ul: 'oos-xjwlmq.ctyunapi.cn', + }, + gslz: { + ul: 'oos-gslz.ctyunapi.cn', + }, + sdqd: { + ul: 'oos-sdqd.ctyunapi.cn', + }, + gzgy: { + ul: 'oos-gzgy.ctyunapi.cn', + }, + hbwh: { + ul: 'oos-gslz.ctyunapi.cn', + }, + xzls: { + ul: 'oos-xzls.ctyunapi.cn', + }, + ahwh: { + ul: 'oos-ahwh.ctyunapi.cn', + }, + gdsz: { + ul: 'oos-gdsz.ctyunapi.cn', + }, + jssz: { + ul: 'oos-jssz.ctyunapi.cn', + }, + sh2: { + ul: 'oos-sh2.ctyunapi.cn', + }, +}; +class CTYunInstance { + accessKey; + secretKey; + constructor(accessKey, secretKey) { + this.accessKey = accessKey; + this.secretKey = secretKey; + } + getUploadInfo(bucket, zone, key, actions) { + try { + const uploadToken = this.getToken(zone, bucket, actions); + return { + key, + uploadToken, + uploadHost: `https://${CTYun_ENDPOINT_LIST[zone].ul}`, + bucket, + }; + } + catch (err) { + throw err; + } + } + getToken(zone, bucket, actions) { + const config = { + accessKeyId: this.accessKey, + secretAccessKey: this.secretKey, + endpoint: `http://${CTYun_ENDPOINT_LIST[zone].ul}`, + region: "ctyun", + }; + const stsClient = new aws_sdk_1.default.STS(config); + const actions2 = actions ? actions.map((ele) => `s3:${ele}`) : ['s3:*']; + const params = { + Policy: `{"Version":"2012-10-17","Statement":{"Effect":"Allow","A +ction":${actions2},"Resource":["arn:aws:s3:::${bucket}","arn:aws:s +3:::${bucket}/*"]}}`, + RoleArn: "arn:aws:iam:::role/oak", + RoleSessionName: "oak", + DurationSeconds: 900, // 过期时间 + }; + stsClient.assumeRole(params, (err, data) => { + if (err) { + throw err; + } + else { + console.log('success', data); + return data; + } + }); + } +} +exports.CTYunInstance = CTYunInstance; diff --git a/lib/types/ctyun.d.ts b/lib/types/ctyun.d.ts new file mode 100644 index 0000000..f131fab --- /dev/null +++ b/lib/types/ctyun.d.ts @@ -0,0 +1,6 @@ +/** + * 此处记录ctyun操作权限名称,更多操作权限参考下方链接 + * https://www.ctyun.cn/document/10306929/10136179 + */ +export type Action = '*' | 'PutObject' | 'GetObject' | 'DeleteObject' | 'ListBucket'; +export type CTYunZone = 'hazz' | 'lnsy' | 'sccd' | 'xjwlmq' | 'gslz' | 'sdqd' | 'gzgy' | 'hbwh' | 'xzls' | 'ahwh' | 'gdsz' | 'jssz' | 'sh2'; diff --git a/lib/types/ctyun.js b/lib/types/ctyun.js new file mode 100644 index 0000000..c8ad2e5 --- /dev/null +++ b/lib/types/ctyun.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/lib/types/index.d.ts b/lib/types/index.d.ts index 69902fc..0763376 100644 --- a/lib/types/index.d.ts +++ b/lib/types/index.d.ts @@ -1,2 +1,3 @@ export * from './Wechat'; export * from './Qiniu'; +export * from './CTYun'; diff --git a/lib/types/index.js b/lib/types/index.js index 7ae07bf..51d4322 100644 --- a/lib/types/index.js +++ b/lib/types/index.js @@ -3,3 +3,4 @@ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); tslib_1.__exportStar(require("./Wechat"), exports); tslib_1.__exportStar(require("./Qiniu"), exports); +tslib_1.__exportStar(require("./CTYun"), exports); diff --git a/package.json b/package.json index a5ddbca..16d76f9 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "dependencies": { "@alicloud/pop-core": "^1.7.12", + "aws-sdk": "^2.1499.0", "cheerio": "^1.0.0-rc.12", "isomorphic-fetch": "^3.0.0", "oak-domain": "file:../oak-domain", diff --git a/src/CTYunSDK.ts b/src/CTYunSDK.ts new file mode 100644 index 0000000..10b2a48 --- /dev/null +++ b/src/CTYunSDK.ts @@ -0,0 +1,29 @@ +import { CTYunInstance } from './service/ctyun/CTYun'; + +class CTYunSDK { + ctyunMap: Record; + + constructor() { + this.ctyunMap = {}; + } + + getInstance( + accessKey: string, + accessSecret: string, + ) { + if (this.ctyunMap[accessKey]) { + return this.ctyunMap[accessKey]; + } + const instance = new CTYunInstance(accessKey, accessSecret); + Object.assign(this.ctyunMap, { + [accessKey]: instance, + }); + return instance; + } +} + +const SDK = new CTYunSDK(); +export default SDK; + +export { CTYunInstance }; + diff --git a/src/index.ts b/src/index.ts index e7501e7..fc8ba81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,12 +2,15 @@ import WechatSDK, { WechatMpInstance, WechatPublicInstance, WechatWebInstance } import AmapSDK from './AmapSDK'; import QiniuSDK, { QiniuCloudInstance } from './QiniuSDK'; import SmsSdk, { TencentSmsInstance, AliSmsInstance } from './SmsSdk'; +import CTYunSDk, { CTYunInstance } from './CTYunSDK'; export * from './service/amap/Amap'; export { AmapSDK, QiniuSDK, WechatSDK, + CTYunSDk, + CTYunInstance, WechatMpInstance, WechatPublicInstance, WechatWebInstance, diff --git a/src/service/ctyun/CTYun.ts b/src/service/ctyun/CTYun.ts new file mode 100644 index 0000000..67cb82b --- /dev/null +++ b/src/service/ctyun/CTYun.ts @@ -0,0 +1,129 @@ +import AWS from 'aws-sdk'; +import crypto from 'crypto'; +import { Action, CTYunZone } from '../../types/CTYun'; + +const CTYun_ENDPOINT_LIST = { + hazz: { + ul: 'oos-hazz.ctyunapi.cn', + }, + lnsy: { + ul: 'oos-lnsy.ctyunapi.cn', + }, + sccd: { + ul: 'oos-sccd.ctyunapi.cn', + }, + xjwlmq: { + ul: 'oos-xjwlmq.ctyunapi.cn', + }, + gslz: { + ul: 'oos-gslz.ctyunapi.cn', + }, + sdqd: { + ul: 'oos-sdqd.ctyunapi.cn', + }, + gzgy: { + ul: 'oos-gzgy.ctyunapi.cn', + }, + hbwh: { + ul: 'oos-gslz.ctyunapi.cn', + }, + xzls: { + ul: 'oos-xzls.ctyunapi.cn', + }, + ahwh: { + ul: 'oos-ahwh.ctyunapi.cn', + }, + gdsz: { + ul: 'oos-gdsz.ctyunapi.cn', + }, + jssz: { + ul: 'oos-jssz.ctyunapi.cn', + }, + sh2: { + ul: 'oos-sh2.ctyunapi.cn', + }, +} + +export class CTYunInstance { + private accessKey: string; + private secretKey: string; + + constructor(accessKey: string, secretKey: string,) { + this.accessKey = accessKey; + this.secretKey = secretKey; + } + + getUploadInfo(bucket: string, zone: CTYunZone, key?: string, actions?: Action[]) { + try { + const uploadToken = this.getToken(zone, bucket, actions); + const signInfo = this.getSignInfo(bucket, actions); + return { + key, + uploadToken, + accessKey: this.accessKey, + policy: signInfo.encodePolicy, + signature: signInfo.signature, + uploadHost: `https://${CTYun_ENDPOINT_LIST[zone].ul}`, + bucket, + }; + } catch (err) { + throw err; + } + } + + getSignInfo(bucket: string, actions?: Action[]) { + const actions2 = actions ? actions.map((ele) => `s3:${ele}`) : ['s3:*']; + const policy = `{"Version":"2012-10-17","Statement":{"Effect":"Allow","A +ction":${actions2},"Resource":["arn:aws:s3:::${bucket}","arn:aws:s +3:::${bucket}/*"]}}`; + const encodePolicy = this.urlSafeBase64Encode(policy); + const signature = this.hmacSha1(encodePolicy, this.secretKey); + return { + encodePolicy, + signature + } + } + + getToken(zone: CTYunZone, bucket: string, actions?: Action[]) { + const config = { + accessKeyId: this.accessKey, + secretAccessKey: this.secretKey, + endpoint: `http://${CTYun_ENDPOINT_LIST[zone].ul}`, + region: "ctyun", + } + const stsClient = new AWS.STS(config); + const actions2 = actions ? actions.map((ele) => `s3:${ele}`) : ['s3:*']; + const params = { + Policy: `{"Version":"2012-10-17","Statement":{"Effect":"Allow","A +ction":${actions2},"Resource":["arn:aws:s3:::${bucket}","arn:aws:s +3:::${bucket}/*"]}}`, + RoleArn: "arn:aws:iam:::role/oak", + RoleSessionName: "oak", + DurationSeconds: 900, // 过期时间 + } + stsClient.assumeRole(params, (err, data) => { + if (err) { + throw err; + } + else { + console.log('success', data); + return data; + } + }) + } + + private base64ToUrlSafe(v: string) { + return v.replace(/\//g, '_').replace(/\+/g, '-'); + } + + private hmacSha1(encodedFlags: any, secretKey: string) { + const hmac = crypto.createHmac('sha1', secretKey); + hmac.update(encodedFlags); + return hmac.digest('base64'); + } + + private urlSafeBase64Encode(jsonFlags: string) { + const encoded = Buffer.from(jsonFlags).toString('base64'); + return this.base64ToUrlSafe(encoded); + } +} diff --git a/src/types/CTYun.ts b/src/types/CTYun.ts new file mode 100644 index 0000000..70e3114 --- /dev/null +++ b/src/types/CTYun.ts @@ -0,0 +1,8 @@ + +/** + * 此处记录ctyun操作权限名称,更多操作权限参考下方链接 + * https://www.ctyun.cn/document/10306929/10136179 + */ +export type Action = '*' | 'PutObject' | 'GetObject' | 'DeleteObject' | 'ListBucket'; + +export type CTYunZone = 'hazz' | 'lnsy' | 'sccd' | 'xjwlmq' | 'gslz' | 'sdqd' | 'gzgy' | 'hbwh' | 'xzls' | 'ahwh' | 'gdsz' | 'jssz' | 'sh2'; diff --git a/src/types/index.ts b/src/types/index.ts index fe6ace8..09d60f9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,2 +1,3 @@ export * from './Wechat'; -export * from './Qiniu'; \ No newline at end of file +export * from './Qiniu'; +export * from './CTYun'; \ No newline at end of file