添加 服务端GetOSSToken接口
This commit is contained in:
@@ -11,10 +11,12 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/ali-oss": "^6.16.11",
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/jsonwebtoken": "^9.0.6",
|
||||
"@types/node": "^20.12.12",
|
||||
"ali-oss": "^6.21.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.4.5"
|
||||
|
||||
59
Server/src/APIs/Console/GetOSSToken.ts
Normal file
59
Server/src/APIs/Console/GetOSSToken.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import { STS } from 'ali-oss'
|
||||
import Auth from "../../Plugs/Middleware/Auth";
|
||||
import config from "../../config";
|
||||
|
||||
// 获取OSS Token
|
||||
class GetOSSToken extends API {
|
||||
constructor() {
|
||||
super('GET', '/console/ossToken', Auth);
|
||||
}
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
// 进行OSS_Upload_STS_Token获取
|
||||
let sts = new STS({
|
||||
accessKeyId: config.oss.accessKeyId,
|
||||
accessKeySecret: config.oss.accessKeySecret
|
||||
});
|
||||
let policy = {
|
||||
"Version": "1",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": [
|
||||
// "oss:GetObject",
|
||||
// "oss:PutObject",
|
||||
// "oss:ListObject"
|
||||
"oss:*"
|
||||
],
|
||||
"Resource": [
|
||||
// `acs:oss:*:*:tone-personal/${config.oss.dir}/*`.toString()
|
||||
"*"
|
||||
// `acs:oss:*:tone-personal/*`.toString()
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
try {
|
||||
let sts_res = await sts.assumeRole(config.oss.roleArn, policy, config.oss.stsExpirationSec);
|
||||
let sts_token: any = {
|
||||
AccessKeyId: sts_res.credentials.AccessKeyId,
|
||||
AccessKeySecret: sts_res.credentials.AccessKeySecret,
|
||||
SecurityToken: sts_res.credentials.SecurityToken,
|
||||
OSSRegion: config.oss.region,
|
||||
Bucket: config.oss.bucket,
|
||||
ExpirationSec: config.oss.stsExpirationSec,
|
||||
}
|
||||
this.logger.info('STS AssumeRol 成功');
|
||||
res.json({ ...ServerStdResponse.OK, data: sts_token });
|
||||
return;
|
||||
} catch (error: any) {
|
||||
this.logger.error('STS AssumeRole 获取时发生错误', error.message);
|
||||
res.json(ServerStdResponse.SERVER_ERROR);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default GetOSSToken;
|
||||
@@ -23,6 +23,7 @@ import SaveResource from '../APIs/Console/SaveResource'
|
||||
import DelResource from '../APIs/Console/DelResource'
|
||||
import SaveBlog from '../APIs/Console/SaveBlog'
|
||||
import DelBlog from '../APIs/Console/DelBlog'
|
||||
import GetOSSToken from "../APIs/Console/GetOSSToken";
|
||||
|
||||
class Server {
|
||||
private logger = new Logger('Server');
|
||||
@@ -52,6 +53,7 @@ class Server {
|
||||
this.apiLoader.add(GetBlogs)
|
||||
this.apiLoader.add(SaveBlog);
|
||||
this.apiLoader.add(DelBlog);
|
||||
this.apiLoader.add(GetOSSToken);
|
||||
|
||||
this.apiLoader.start(config.apiPort);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,17 @@ const config = {
|
||||
secret: '17e50223f4a545ec9e36ebf08e2f71bb',
|
||||
expiresIn: '1d',
|
||||
},
|
||||
apiPort: 8080,
|
||||
oss: {
|
||||
accessKeyId: '',
|
||||
accessKeySecret: '',
|
||||
roleArn: '',
|
||||
bucket: '',
|
||||
region: '',
|
||||
callbackUrl: '',
|
||||
dir: '',
|
||||
stsExpirationSec: 3600
|
||||
},
|
||||
apiPort: 23500,
|
||||
} as const;
|
||||
|
||||
export default config;
|
||||
Reference in New Issue
Block a user