Sending bucket notifications to a functionNovember 21, 2022
A bucket can send a notification to a function running on Stedi every time it receives a file. The function will get a message as its first argument with information about the file. It’ll look something like this.
{
"Records": [{
"eventName":"ObjectCreated:Put",
"s3": {
"bucket": {
"name": "demo-notification-input"
},
"object": {
"key": "test.txt"
"size": 1493
}
}
}]
}
Of course, you need a bucket and a function to make this work. You also need to set up the notification. The notification settings are part of the bucket, not of the function.
Prerequisites
You need to create:
You need to install:
- Node.js for running the function on your local machine.
git clone https://github.com/Stedi-Demos/bucket-notifications.git
cd bucket-notifications
npm ci
Overview
File | Description |
---|---|
handler.js | Contains the code for the function that receives the bucket notification. |
local.js | Allows you to test the function on your local machine before deploying it to Stedi. |
setup.js | Creates the resources you need for this demo. It creates an input and an output bucket and it creates two settings files. |
deploy.js | Deploys the function to Stedi. |
notification.js | Enables the notification on the input bucket. |
clean.js | Removes the buckets and the function created by this demo. |
settings.js | Contains settings used by the helper scripts of this demo: deploy.js , notification.js , and clean.js . |
.env | Contains settings used by the function. These settings are deployed with the function. |
Walkthrough
Function code
The function writes its output to a different bucket than the one that sends notifications, otherwise it would end up in an infinite loop.
Testing locally
node scripts/setup.js
local.txt
. You should upload that file to the input bucket, or the function won’t be able to read it. You can do this with the Stedi CLI, which was installed along with the other dependencies for this demo. There’s a catch, though: you need to know the name of the input bucket. Since bucket names need to be globally unique, the setup script generated a name especially for you. Fortunately, it wrote the name to the settings file. Uploading the file looks something like this, but remember that you need to change the bucket name.npx stedi buckets put-object -b file://test/local.txt -k local.txt -n demo-notification-input
Now you can run the test.
node scripts/local.js
.env
contains only those settings that are used by the function, including the name of the output bucket. settings.js
contains the settings that are used by the scripts in the scripts
directory. This also includes the name of the output bucket, but if we store it in two different places, they may go out of sync, so settings.js
grabs it from .env
.Deploying the function
node scripts/deploy.js
Setting up the notification
To connect the bucket and the function, you have to add the notification configuration to the bucket. That configuration looks something like this.
notifications: {
functions: [{
functionName: functionName
}]
}
If that looks like a lot of ceremony for what’s essentially just a function name, you should know that we plan to add more notification options in the future.
node scripts/notification.js
Testing the deployed function
If you upload a file to the input bucket, you’ll trigger the function. Remember to change the bucket name.
npx stedi buckets put-object -b file://test/deployed.txt -k deployed.txt -n demo-notification-input
Cleaning up
To delete the buckets and the function:
node scripts/clean.js