Files
certd/packages/ui/certd-server/src/plugins/plugin-lib/oss/impls/tencentcos.ts
T

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-04-25 01:44:15 +08:00
import dayjs from "dayjs";
2025-04-25 01:26:04 +08:00
import { TencentAccess, TencentCosAccess, TencentCosClient } from "../../tencent/index.js";
2025-04-25 02:11:23 +08:00
import { BaseOssClient, OssFileItem } from "../api.js";
2025-01-02 00:28:13 +08:00
2025-04-25 01:26:04 +08:00
export default class TencentOssClientImpl extends BaseOssClient<TencentCosAccess> {
2025-04-25 01:44:15 +08:00
client: TencentCosClient;
join(...strs: string[]) {
const str = super.join(...strs);
if (str.startsWith("/")) {
return str.substring(1);
}
return str;
2025-04-25 01:26:04 +08:00
}
2025-04-25 01:44:15 +08:00
async init() {
const access = await this.ctx.accessService.getById<TencentAccess>(this.access.accessId);
2025-04-25 01:44:15 +08:00
this.client = new TencentCosClient({
access: access,
logger: this.logger,
region: this.access.region,
bucket: this.access.bucket,
});
2025-04-25 01:44:15 +08:00
}
async download(filePath: string, savePath: string): Promise<void> {
const key = this.join(this.rootDir, filePath);
await this.client.downloadFile(key, savePath);
2025-01-02 00:28:13 +08:00
}
2025-04-25 01:44:15 +08:00
async listDir(dir: string): Promise<OssFileItem[]> {
const dirKey = this.join(this.rootDir, dir) + "/";
2025-04-25 02:11:08 +08:00
// @ts-ignore
2025-04-25 01:44:15 +08:00
const res: any[] = await this.client.listDir(dirKey);
return res.map(item => {
return {
path: item.Key,
size: item.Size,
lastModified: dayjs(item.LastModified).valueOf(),
};
});
2025-04-25 01:44:15 +08:00
}
2025-04-27 01:31:46 +08:00
async upload(filePath: string, fileContent: Buffer | string) {
2025-04-25 01:44:15 +08:00
const key = this.join(this.rootDir, filePath);
await this.client.uploadFile(key, fileContent);
2025-04-27 01:31:46 +08:00
this.logger.info(`文件上传成功: ${filePath}`);
2025-04-25 01:44:15 +08:00
}
2025-04-27 01:31:46 +08:00
async remove(filePath: string, opts?: { joinRootDir?: boolean }) {
if (opts?.joinRootDir !== false) {
filePath = this.join(this.rootDir, filePath);
}
await this.client.removeFile(filePath);
this.logger.info(`文件删除成功: ${filePath}`);
}
2025-01-02 00:28:13 +08:00
}