This commit is contained in:
梁朝伟 2023-11-21 15:47:19 +08:00
parent f8f7908844
commit 25315226eb
7 changed files with 166 additions and 86 deletions

View File

@ -5,9 +5,17 @@ export declare class CTYunInstance {
constructor(accessKey: string, secretKey: string);
getUploadInfo(bucket: string, zone: CTYunZone, key?: string, actions?: Action[]): {
key: string | undefined;
uploadToken: void;
accessKey: string;
policy: string;
signature: string;
uploadHost: string;
bucket: string;
};
getToken(zone: CTYunZone, bucket: string, actions?: Action[]): void;
getSignInfo(bucket: string, actions?: Action[]): {
encodePolicy: string;
signature: string;
};
private base64ToUrlSafe;
private hmacSha1;
private urlSafeBase64Encode;
}

View File

@ -1,4 +1,5 @@
import AWS from 'aws-sdk';
// import AWS from 'aws-sdk';
import crypto from 'crypto';
const CTYun_ENDPOINT_LIST = {
hazz: {
ul: 'oos-hazz.ctyunapi.cn',
@ -49,10 +50,14 @@ export class CTYunInstance {
}
getUploadInfo(bucket, zone, key, actions) {
try {
const uploadToken = this.getToken(zone, bucket, actions);
// const uploadToken = this.getToken(zone, bucket, actions);
const signInfo = this.getSignInfo(bucket, actions);
return {
key,
uploadToken,
// uploadToken,
accessKey: this.accessKey,
policy: signInfo.encodePolicy,
signature: signInfo.signature,
uploadHost: `https://${CTYun_ENDPOINT_LIST[zone].ul}`,
bucket,
};
@ -61,31 +66,55 @@ export class CTYunInstance {
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);
getSignInfo(bucket, actions) {
const actions2 = actions ? actions.map((ele) => `s3:${ele}`) : ['s3:*'];
const params = {
Policy: `{"Version":"2012-10-17","Statement":{"Effect":"Allow","A
const 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, // 过期时间
3:::${bucket}/*"]}}`;
const encodePolicy = this.urlSafeBase64Encode(policy);
const signature = this.hmacSha1(encodePolicy, this.secretKey);
return {
encodePolicy,
signature
};
stsClient.assumeRole(params, (err, data) => {
if (err) {
throw err;
}
else {
console.log('success', data);
return data;
}
});
}
// 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;
// }
// })
// }
base64ToUrlSafe(v) {
return v.replace(/\//g, '_').replace(/\+/g, '-');
}
hmacSha1(encodedFlags, secretKey) {
const hmac = crypto.createHmac('sha1', secretKey);
hmac.update(encodedFlags);
return hmac.digest('base64');
}
urlSafeBase64Encode(jsonFlags) {
const encoded = Buffer.from(jsonFlags).toString('base64');
return this.base64ToUrlSafe(encoded);
}
}

View File

@ -336,6 +336,9 @@ export class QiniuCloudInstance {
*/
async access(host, path, headers, query, method, body, mockData) {
const query2 = query && getQueryString(query);
/**
* web/server环境测试通过小程序没测by Xc
*/
const url = new URL(`https://${host}${path}`);
if (process.env.NODE_ENV === 'development' && mockData) {
console.warn(`mocking access qiniu api: url: ${url.toString()}, body: ${JSON.stringify(body)}, method: ${method}`, mockData);

View File

@ -5,9 +5,17 @@ export declare class CTYunInstance {
constructor(accessKey: string, secretKey: string);
getUploadInfo(bucket: string, zone: CTYunZone, key?: string, actions?: Action[]): {
key: string | undefined;
uploadToken: void;
accessKey: string;
policy: string;
signature: string;
uploadHost: string;
bucket: string;
};
getToken(zone: CTYunZone, bucket: string, actions?: Action[]): void;
getSignInfo(bucket: string, actions?: Action[]): {
encodePolicy: string;
signature: string;
};
private base64ToUrlSafe;
private hmacSha1;
private urlSafeBase64Encode;
}

View File

@ -2,7 +2,8 @@
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"));
// import AWS from 'aws-sdk';
const crypto_1 = tslib_1.__importDefault(require("crypto"));
const CTYun_ENDPOINT_LIST = {
hazz: {
ul: 'oos-hazz.ctyunapi.cn',
@ -53,10 +54,14 @@ class CTYunInstance {
}
getUploadInfo(bucket, zone, key, actions) {
try {
const uploadToken = this.getToken(zone, bucket, actions);
// const uploadToken = this.getToken(zone, bucket, actions);
const signInfo = this.getSignInfo(bucket, actions);
return {
key,
uploadToken,
// uploadToken,
accessKey: this.accessKey,
policy: signInfo.encodePolicy,
signature: signInfo.signature,
uploadHost: `https://${CTYun_ENDPOINT_LIST[zone].ul}`,
bucket,
};
@ -65,32 +70,56 @@ class CTYunInstance {
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);
getSignInfo(bucket, actions) {
const actions2 = actions ? actions.map((ele) => `s3:${ele}`) : ['s3:*'];
const params = {
Policy: `{"Version":"2012-10-17","Statement":{"Effect":"Allow","A
const 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, // 过期时间
3:::${bucket}/*"]}}`;
const encodePolicy = this.urlSafeBase64Encode(policy);
const signature = this.hmacSha1(encodePolicy, this.secretKey);
return {
encodePolicy,
signature
};
stsClient.assumeRole(params, (err, data) => {
if (err) {
throw err;
}
else {
console.log('success', data);
return data;
}
});
}
// 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;
// }
// })
// }
base64ToUrlSafe(v) {
return v.replace(/\//g, '_').replace(/\+/g, '-');
}
hmacSha1(encodedFlags, secretKey) {
const hmac = crypto_1.default.createHmac('sha1', secretKey);
hmac.update(encodedFlags);
return hmac.digest('base64');
}
urlSafeBase64Encode(jsonFlags) {
const encoded = Buffer.from(jsonFlags).toString('base64');
return this.base64ToUrlSafe(encoded);
}
}
exports.CTYunInstance = CTYunInstance;

View File

@ -340,6 +340,9 @@ class QiniuCloudInstance {
*/
async access(host, path, headers, query, method, body, mockData) {
const query2 = query && getQueryString(query);
/**
* web/server环境测试通过小程序没测by Xc
*/
const url = new URL(`https://${host}${path}`);
if (process.env.NODE_ENV === 'development' && mockData) {
console.warn(`mocking access qiniu api: url: ${url.toString()}, body: ${JSON.stringify(body)}, method: ${method}`, mockData);

View File

@ -1,4 +1,4 @@
import AWS from 'aws-sdk';
// import AWS from 'aws-sdk';
import crypto from 'crypto';
import { Action, CTYunZone } from '../../types/CTYun';
@ -55,11 +55,11 @@ export class CTYunInstance {
getUploadInfo(bucket: string, zone: CTYunZone, key?: string, actions?: Action[]) {
try {
const uploadToken = this.getToken(zone, bucket, actions);
// const uploadToken = this.getToken(zone, bucket, actions);
const signInfo = this.getSignInfo(bucket, actions);
return {
key,
uploadToken,
// uploadToken,
accessKey: this.accessKey,
policy: signInfo.encodePolicy,
signature: signInfo.signature,
@ -84,33 +84,33 @@ ction":${actions2},"Resource":["arn:aws:s3:::${bucket}","arn:aws:s
}
}
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;
}
})
}
// 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, '-');