mirror of https://github.com/certd/certd
chore:
parent
308d4600ef
commit
aec51e514c
|
@ -9,7 +9,6 @@ export type OssClientRemoveByOpts = {
|
|||
};
|
||||
|
||||
export type OssFileItem = {
|
||||
name: string;
|
||||
path: string;
|
||||
size: number;
|
||||
//毫秒时间戳
|
||||
|
|
|
@ -27,7 +27,7 @@ export class OssClientFactory {
|
|||
throw new Error(`暂不支持此文件上传方式: ${type}`);
|
||||
}
|
||||
}
|
||||
async createOssClientByType(type: string, opts: { rootDir: string; access: any; ctx: OssClientContext }) {
|
||||
async createOssClientByType(type: string, opts: { rootDir?: string; access: any; ctx: OssClientContext }) {
|
||||
const cls = await this.getClassByType(type);
|
||||
if (cls) {
|
||||
// @ts-ignore
|
||||
|
|
|
@ -1,37 +1,49 @@
|
|||
import dayjs from "dayjs";
|
||||
import { TencentAccess, TencentCosAccess, TencentCosClient } from "../../tencent/index.js";
|
||||
import { BaseOssClient, OssClientRemoveByOpts, OssFileItem } from "../api.js";
|
||||
|
||||
export default class TencentOssClientImpl extends BaseOssClient<TencentCosAccess> {
|
||||
download(fileName: string, savePath: string): Promise<void> {
|
||||
throw new Error("Method not implemented.");
|
||||
client: TencentCosClient;
|
||||
|
||||
join(...strs: string[]) {
|
||||
const str = super.join(...strs);
|
||||
if (str.startsWith("/")) {
|
||||
return str.substring(1);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
removeBy(removeByOpts: OssClientRemoveByOpts): Promise<void> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
listDir(dir: string): Promise<OssFileItem[]> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
async upload(filePath: string, fileContent: Buffer) {
|
||||
async init() {
|
||||
const access = await this.ctx.accessService.getById<TencentAccess>(this.access.accessId);
|
||||
const client = new TencentCosClient({
|
||||
this.client = new TencentCosClient({
|
||||
access: access,
|
||||
logger: this.logger,
|
||||
region: this.access.region,
|
||||
bucket: this.access.bucket,
|
||||
});
|
||||
const key = this.rootDir + filePath;
|
||||
await client.uploadFile(key, fileContent);
|
||||
}
|
||||
async download(filePath: string, savePath: string): Promise<void> {
|
||||
const key = this.join(this.rootDir, filePath);
|
||||
await this.client.downloadFile(key, savePath);
|
||||
}
|
||||
|
||||
async listDir(dir: string): Promise<OssFileItem[]> {
|
||||
const dirKey = this.join(this.rootDir, dir) + "/";
|
||||
const res: any[] = await this.client.listDir(dirKey);
|
||||
return res.map(item => {
|
||||
return {
|
||||
path: item.Key,
|
||||
size: item.Size,
|
||||
lastModified: dayjs(item.LastModified).valueOf(),
|
||||
};
|
||||
});
|
||||
}
|
||||
async upload(filePath: string, fileContent: Buffer) {
|
||||
const key = this.join(this.rootDir, filePath);
|
||||
await this.client.uploadFile(key, fileContent);
|
||||
}
|
||||
|
||||
async remove(filePath: string) {
|
||||
const access = await this.ctx.accessService.getById<TencentAccess>(this.access.accessId);
|
||||
const client = new TencentCosClient({
|
||||
access: access,
|
||||
logger: this.logger,
|
||||
region: this.access.region,
|
||||
bucket: this.access.bucket,
|
||||
});
|
||||
const key = this.rootDir + filePath;
|
||||
await client.removeFile(key);
|
||||
const key = this.join(this.rootDir, filePath);
|
||||
await this.client.removeFile(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { TencentAccess } from "../access.js";
|
||||
import { ILogger } from "@certd/basic";
|
||||
import fs from "fs";
|
||||
|
||||
export class TencentCosClient {
|
||||
access: TencentAccess;
|
||||
|
@ -66,4 +67,47 @@ export class TencentCosClient {
|
|||
);
|
||||
});
|
||||
}
|
||||
|
||||
async downloadFile(key: string, savePath: string) {
|
||||
const cos = await this.getCosClient();
|
||||
const writeStream = fs.createWriteStream(savePath);
|
||||
return new Promise((resolve, reject) => {
|
||||
cos.getObject(
|
||||
{
|
||||
Bucket: this.bucket,
|
||||
Region: this.region,
|
||||
Key: key,
|
||||
Output: writeStream,
|
||||
},
|
||||
function (err, data) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve(data);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async listDir(dirKey: string) {
|
||||
const cos = await this.getCosClient();
|
||||
return new Promise((resolve, reject) => {
|
||||
cos.getBucket(
|
||||
{
|
||||
Bucket: this.bucket,
|
||||
Region: this.region,
|
||||
Prefix: dirKey,
|
||||
MaxKeys: 1000,
|
||||
},
|
||||
function (err, data) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve(data.Contents);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue