feat: 添加 listObjects 方法的 getMeta 选项以获取对象元数据

This commit is contained in:
2026-02-01 17:37:48 +08:00
parent d4016e5680
commit 151a74fad1
3 changed files with 13 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/oss",
"version": "0.0.18",
"version": "0.0.19",
"main": "dist/index.js",
"scripts": {
"build": "bun run bun.config.ts"

View File

@@ -258,12 +258,12 @@ export class OssBase implements OssBaseOperation {
*/
async listObjects<IS_FILE = false>(
objectName: string,
opts?: { recursive?: boolean; startAfter?: string; maxKeys?: number },
opts?: { recursive?: boolean; startAfter?: string; maxKeys?: number, getMeta?: boolean },
): Promise<IS_FILE extends true ? ListFileObject[] : ListObjectResult[]> {
const prefix = `${this.prefix}${objectName}`;
const results: ListObjectResult[] = [];
let continuationToken: string | undefined;
const getMeta = opts?.getMeta ?? false;
do {
const command = new ListObjectsV2Command({
Bucket: this.bucketName,
@@ -279,12 +279,19 @@ export class OssBase implements OssBaseOperation {
// 处理文件对象
if (response.Contents) {
for (const item of response.Contents) {
results.push({
const result: ListFileObject = {
name: item.Key || '',
size: item.Size || 0,
lastModified: item.LastModified || new Date(),
etag: item.ETag?.replace(/"/g, '') || '',
});
}
if (getMeta) {
const stat = await this.statObject(item.Key || '', false);
if (stat?.metaData) {
result.metaData = stat.metaData;
}
}
results.push(result);
}
}

View File

@@ -31,6 +31,7 @@ export type ListFileObject = {
size: number;
lastModified: Date;
etag: string;
metaData?: ItemBucketMetadata;
};
export type ListDirectoryObject = {