List objects in a bucket

List all objects in a bucket and return them as a list

Script s3 Verified

by admin ยท 8/16/2022

The script

Submitted by hugo989 Bun
Verified 6 days ago
1
import * as Minio from "minio@7";
2

3
type S3 = {
4
  endPoint: string;
5
  port: number;
6
  useSSL: boolean;
7
  pathStyle: boolean;
8
  bucket: string;
9
  accessKey: string;
10
  secretKey: string;
11
  region: string;
12
};
13
export async function main(
14
  s3: S3,
15
  prefix?: string,
16
  bucketName?: string,
17
  maxResults?: number,
18
  pageSize?: number,
19
) {
20
  const s3client = new Minio.Client(s3);
21
  const options = removeObjectEmptyFields({
22
    prefix,
23
    bucketName,
24
    maxResults,
25
    pageSize,
26
  });
27

28
  const stream = s3client.listObjects(
29
    options.bucketName || s3.bucket,
30
    options.prefix ?? "",
31
    true,
32
  );
33

34
  const result: any[] = [];
35
  await new Promise<void>((resolve, reject) => {
36
    stream.on("data", (obj: any) => {
37
      if (
38
        options.maxResults !== undefined &&
39
        result.length >= options.maxResults
40
      ) {
41
        stream.destroy();
42
        resolve();
43
        return;
44
      }
45
      result.push(obj);
46
    });
47
    stream.on("end", () => resolve());
48
    stream.on("close", () => resolve());
49
    stream.on("error", (err: any) => reject(err));
50
  });
51

52
  return result;
53
}
54

55
function removeObjectEmptyFields(
56
  object?: Record<string, any>,
57
  removeEmptyArraysAndObjects = true,
58
  createNewObject = true,
59
) {
60
  if (!object || typeof object !== "object") return {}
61
  const obj = createNewObject ? { ...object } : object
62
  const emptyValues = [undefined, null, ""]
63
  for (const key in obj) {
64
    const value = obj[key]
65
    if (emptyValues.includes(value)) {
66
      delete obj[key]
67
    } else if (typeof value === "object") {
68
      if (Object.keys(value).length) {
69
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
70
      }
71
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
72
        delete obj[key]
73
      }
74
    }
75
  }
76
  return obj
77
}
78

Other submissions
  • Submitted by alvin chia107 Deno
    Created 1251 days ago
    1
    // import * as wmill from 'https://deno.land/x/windmill/index.ts'
    2
    
    
    3
    export async function main(x: string) {
    4
      return x
    5
    }
  • Submitted by admin Deno
    Created 398 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { S3Client } from "https://deno.land/x/[email protected]/mod.ts";
    3
    
    
    4
    type S3 = {
    5
      endPoint: string;
    6
      port: number;
    7
      useSSL: boolean;
    8
      pathStyle: boolean;
    9
      bucket: string;
    10
      accessKey: string;
    11
      secretKey: string;
    12
      region: string;
    13
    };
    14
    export async function main(
    15
      s3: S3,
    16
      prefix?: string,
    17
      bucketName?: string,
    18
      maxResults?: number,
    19
      pageSize?: number,
    20
    ) {
    21
      const s3client = new S3Client(s3);
    22
      const options = removeObjectEmptyFields({
    23
        prefix,
    24
        bucketName,
    25
        maxResults,
    26
        pageSize,
    27
      });
    28
      const objects = s3client.listObjects(options);
    29
    
    
    30
      const result: any[] = [];
    31
      for await (const obj of objects) {
    32
        result.push(obj);
    33
      }
    34
    
    
    35
      return result;
    36
    }
    37