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 769 days ago Viewed 13004 times
In Windmill, a trigger script is designed to pull data from an external source and return all the new items since the last run. It operates without resorting to external webhooks and is typically used with schedules and states to compare the current execution to the previous one.
0
Submitted by adam186 Deno
Verified 769 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