This commit is contained in:
xiaojunnuo
2025-04-25 01:44:15 +08:00
parent 308d4600ef
commit aec51e514c
4 changed files with 78 additions and 23 deletions
@@ -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);
}
);
});
}
}