1
Get recently updated objects Trigger
One script reply has been approved by the moderators Verified

Trigger script to check for recently updated objects.

Created by adam186 732 days ago Viewed 9572 times
0
Submitted by adam186 Deno
Verified 732 days ago
1
import {
2
  getState,
3
  setState,
4
} from "https://deno.land/x/windmill@v1.85.0/mod.ts";
5
import { S3Client } from "https://deno.land/x/s3_lite_client@0.3.0/mod.ts";
6

7
/**
8
 * Tested on 10 000 objects, which got processed in 2.3-2.6 seconds.
9
 */
10
type S3 = {
11
  endPoint: string;
12
  port: number;
13
  useSSL: boolean;
14
  pathStyle: boolean;
15
  bucket: string;
16
  accessKey: string;
17
  secretKey: string;
18
  region: string;
19
};
20
export async function main(s3: S3, bucket?: string) {
21
  const client = new S3Client(s3);
22
  const generator = client.listObjects({
23
    bucketName: bucket || undefined,
24
  });
25

26
  const lastCheck = (await getState()) || 0;
27
  await setState(Date.now());
28
  const newlyUpdated = [];
29

30
  while (true) {
31
    const obj = await generator.next();
32
    if (obj.done) break;
33

34
    const lastMod = new Date(obj.value.lastModified).getTime();
35
    if (lastMod > lastCheck) {
36
      newlyUpdated.push(obj.value);
37
    }
38
  }
39

40
  return newlyUpdated;
41
}
42