Edits history of script submission #22500 for ' Get recently updated objects (s3)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { getState, setState } from "windmill-client@1";
    import * as Minio from "minio@7";
    
    /**
     * Tested on 10 000 objects, which got processed in 2.3-2.6 seconds.
     */
    type S3 = {
      endPoint: string;
      port: number;
      useSSL: boolean;
      pathStyle: boolean;
      bucket: string;
      accessKey: string;
      secretKey: string;
      region: string;
    };
    export async function main(s3: S3, bucket?: string) {
      const client = new Minio.Client(s3);
      const stream = client.listObjects(bucket || s3.bucket, "", true);
    
      const lastCheck = (await getState()) || 0;
      await setState(Date.now());
      const newlyUpdated: any[] = [];
    
      await new Promise<void>((resolve, reject) => {
        stream.on("data", (obj: any) => {
          const lastMod = new Date(obj.lastModified).getTime();
          if (lastMod > lastCheck) {
            newlyUpdated.push(obj);
          }
        });
        stream.on("end", () => resolve());
        stream.on("close", () => resolve());
        stream.on("error", (err: any) => reject(err));
      });
    
      return newlyUpdated;
    }
    

    Submitted by hugo989 6 days ago