Files
certd/packages/plugins/plugin-cert/src/plugin/cert-plugin/uploads/impls/alioss.ts
T

37 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-01-03 00:12:15 +08:00
import { BaseHttpChallengeUploader } from "../api.js";
2025-01-02 00:28:13 +08:00
import { AliossAccess, AliyunAccess } from "@certd/plugin-lib";
2025-01-03 00:12:15 +08:00
import { AliossClient } from "@certd/plugin-lib";
2025-01-02 00:28:13 +08:00
export class AliossHttpChallengeUploader extends BaseHttpChallengeUploader<AliossAccess> {
async upload(filePath: string, fileContent: string) {
const aliyunAccess = await this.ctx.accessService.getById<AliyunAccess>(this.access.accessId);
const client = new AliossClient({
access: aliyunAccess,
bucket: this.access.bucket,
region: this.access.region,
});
await client.uploadFile(filePath, Buffer.from(fileContent));
this.logger.info(`校验文件上传成功: ${filePath}`);
}
async remove(filePath: string) {
// remove file from alioss
const client = await this.getAliossClient();
await client.removeFile(filePath);
this.logger.info(`文件删除成功: ${filePath}`);
}
private async getAliossClient() {
const aliyunAccess = await this.ctx.accessService.getById<AliyunAccess>(this.access.accessId);
const client = new AliossClient({
access: aliyunAccess,
bucket: this.access.bucket,
region: this.access.region,
});
await client.init();
return client;
}
}