支持天翼yun

This commit is contained in:
梁朝伟 2023-11-21 15:35:37 +08:00
parent 3698635fe2
commit 9135fc097b
26 changed files with 475 additions and 5 deletions

9
es/CTYunSDK.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
import { CTYunInstance } from './service/ctyun/CTYun';
declare class CTYunSDK {
ctyunMap: Record<string, CTYunInstance>;
constructor();
getInstance(accessKey: string, accessSecret: string): CTYunInstance;
}
declare const SDK: CTYunSDK;
export default SDK;
export { CTYunInstance };

20
es/CTYunSDK.js Normal file
View File

@ -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 };

3
es/index.d.ts vendored
View File

@ -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';

View File

@ -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';

13
es/service/ctyun/CTYun.d.ts vendored Normal file
View File

@ -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;
}

91
es/service/ctyun/CTYun.js Normal file
View File

@ -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;
}
});
}
}

6
es/types/ctyun.d.ts vendored Normal file
View File

@ -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';

1
es/types/ctyun.js Normal file
View File

@ -0,0 +1 @@
export {};

1
es/types/index.d.ts vendored
View File

@ -1,2 +1,3 @@
export * from './Wechat';
export * from './Qiniu';
export * from './CTYun';

View File

@ -1,2 +1,3 @@
export * from './Wechat';
export * from './Qiniu';
export * from './CTYun';

9
lib/CTYunSDK.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
import { CTYunInstance } from './service/ctyun/CTYun';
declare class CTYunSDK {
ctyunMap: Record<string, CTYunInstance>;
constructor();
getInstance(accessKey: string, accessSecret: string): CTYunInstance;
}
declare const SDK: CTYunSDK;
export default SDK;
export { CTYunInstance };

23
lib/CTYunSDK.js Normal file
View File

@ -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;

3
lib/index.d.ts vendored
View File

@ -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';

View File

@ -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);

13
lib/service/ctyun/CTYun.d.ts vendored Normal file
View File

@ -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;
}

View File

@ -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;

6
lib/types/ctyun.d.ts vendored Normal file
View File

@ -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';

2
lib/types/ctyun.js Normal file
View File

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@ -1,2 +1,3 @@
export * from './Wechat';
export * from './Qiniu';
export * from './CTYun';

View File

@ -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);

View File

@ -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",

29
src/CTYunSDK.ts Normal file
View File

@ -0,0 +1,29 @@
import { CTYunInstance } from './service/ctyun/CTYun';
class CTYunSDK {
ctyunMap: Record<string, CTYunInstance>;
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 };

View File

@ -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,

129
src/service/ctyun/CTYun.ts Normal file
View File

@ -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);
}
}

8
src/types/CTYun.ts Normal file
View File

@ -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';

View File

@ -1,2 +1,3 @@
export * from './Wechat';
export * from './Qiniu';
export * from './Qiniu';
export * from './CTYun';