Files
certd/packages/plugins/plugin-lib/src/aliyun/lib/oss-client.ts
T

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-01-02 00:28:13 +08:00
import { AliyunAccess } from "../access";
export class AliossClient {
access: AliyunAccess;
region: string;
bucket: string;
client: any;
constructor(opts: { access: AliyunAccess; bucket: string; region: string }) {
this.access = opts.access;
this.bucket = opts.bucket;
this.region = opts.region;
}
async init() {
2025-01-03 01:17:20 +08:00
if (this.client) {
return;
}
2025-01-02 00:28:13 +08:00
// @ts-ignore
const OSS = await import("ali-oss");
2025-01-03 01:17:20 +08:00
const ossClient = new OSS.default({
2025-01-02 00:28:13 +08:00
accessKeyId: this.access.accessKeyId,
accessKeySecret: this.access.accessKeySecret,
// yourRegion填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou。
region: this.region,
//@ts-ignore
authorizationV4: true,
// yourBucketName填写Bucket名称。
bucket: this.bucket,
});
2025-01-03 01:17:20 +08:00
// oss
this.client = ossClient;
2025-01-02 00:28:13 +08:00
}
2025-01-03 01:17:20 +08:00
async doRequest(bucket: string, xml: string, params: any) {
await this.init();
params = this.client._bucketRequestParams("POST", bucket, {
2025-01-02 00:28:13 +08:00
...params,
});
params.content = xml;
params.mime = "xml";
params.successStatuses = [200];
2025-01-03 01:17:20 +08:00
const res = await this.client.request(params);
2025-01-02 00:28:13 +08:00
this.checkRet(res);
return res;
}
checkRet(ret: any) {
if (ret.code != null) {
throw new Error("执行失败:" + ret.Message);
}
}
async uploadFile(filePath: string, content: Buffer) {
2025-01-03 01:17:20 +08:00
await this.init();
return await this.client.put(filePath, content);
2025-01-02 00:28:13 +08:00
}
async removeFile(filePath: string) {
2025-01-03 01:17:20 +08:00
await this.init();
2025-01-02 00:28:13 +08:00
return await this.client.delete(filePath);
}
}