पाठ 9 / 32

Scheduled Jobs and Storage Events

यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।

Run functions on a schedule or when files change, decode storage event keys correctly, and avoid recursive trigger loops.

लेखक: · 15 साल से वेब डेवलपर

Cron jobs and reacting to files

Two triggers cover a lot of real backend work. Scheduled triggers run a function on a cron or rate expression: nightly reports, cleaning expired sessions, polling a partner API. Amazon EventBridge Scheduler and Cloudflare Cron Triggers are examples. Schedules are often evaluated in UTC unless you configure a time zone, so "9 AM in India" needs care. Storage events fire when an object is created or deleted in a bucket: a student uploads a PDF and a function extracts its text; a photo arrives and a function makes a thumbnail. These deliveries are asynchronous and at-least-once, so the same upload can invoke your function twice. The classic disaster is a function that writes its output back into the same bucket and prefix that triggers it, creating an endless loop of invocations and a surprising bill. Write outputs to a different bucket or prefix, and filter the trigger by prefix or suffix.

Reading an S3 upload event

Object keys in S3 event notifications are URL-encoded, with + standing for a space. Decode them before using the key, or files with spaces and non-ASCII names will fail.

// S3 "ObjectCreated" handler: object keys arrive URL-encoded, with '+' for spaces.
export const handler = async (event) => {
  for (const record of event.Records) {
    const bucket = record.s3.bucket.name;
    const key = decodeURIComponent(record.s3.object.key.replace(/\+/g, ' '));
    console.log(`new upload: s3://${bucket}/${key}`);
    // e.g. create a thumbnail and write it to a DIFFERENT bucket or prefix
  }
};

await handler({ Records: [
  { s3: { bucket: { name: 'campus-uploads' }, object: { key: 'id+cards/Priya+S%C3%A9n.jpg' } } },
] });

Output:

new upload: s3://campus-uploads/id cards/Priya Sén.jpg

Cron expressions and time zones

India Standard Time is UTC+05:30, so a job meant for 09:00 IST on a UTC-only scheduler must be written for 03:30 UTC. Prefer schedulers that accept an explicit time zone, and write the intended local time in a comment next to the expression.

त्वरित जाँच: A function triggered by uploads to `photos/` writes thumbnails to `photos/thumbs/` in the same bucket, with no filter on the trigger. What is the risk?

  • Thumbnails will be written in the wrong image format
  • S3 will reject writes from a function it triggered
  • Each thumbnail triggers the function again, creating a recursive loop
  • The function will be throttled to one invocation per minute
Answer

Each thumbnail triggers the function again, creating a recursive loop — The output lands under the prefix that triggers the function. Use a separate bucket or prefix, or filter the trigger, to break the loop.