DEV Community

Rak
Rak

Posted on

Creating Bucket (S3) Notifications on AWS with Python

In this tutorial, we’ll explore how to set up bucket notifications in Python, enabling your applications to react to changes in file storage buckets. This is particularly useful in scenarios where you need to monitor a bucket for new file uploads or deletions.

If you haven't used the Nitric SDK before, then start with this tutorial.

1. Create a Bucket Instance

Import the necessary packages and instantiate a new bucket object. In this tutorial, we'll name our bucket 'assets'.

from nitric.resources import bucket
from nitric.application import Nitric

assets = bucket("assets")

accessible_assets = bucket("assets").allow("reading")
Enter fullscreen mode Exit fullscreen mode

2. Set Up Notifications for File Write Events

To monitor for file write events, specifically for files starting with a certain prefix, use the On method on the bucket object. Let’s trigger notifications for files starting with '/users/images'.

from nitric.resources import bucket
from nitric.application import Nitric

assets = bucket("assets")

accessible_assets = bucket("assets").allow("reading")

# The request will contain the name of the file `key` and the type of event `type`
@assets.on("delete", "*")
async def delete_anything(ctx):
  print(f"a file named {ctx.req.key} was deleted")

@assets.on("write", "/images/cat")
async def create_cat_image(ctx):
  print(f"A cat image was written")

# If `on` is called with a permissioned bucket, a file will also be provided with the request
@accessible_assets.on("write", "/images/dog")
async def access_dog_file(ctx):
  dog_image = await ctx.req.file.read()

  print(dog_image)

Nitric.run()
Enter fullscreen mode Exit fullscreen mode

3. Set Up Notifications for File Delete Events

Similarly, to monitor for file deletion events for any file, set up a notification like so:

from nitric.resources import bucket
from nitric.application import Nitric

# Create a reference to an 'assets' bucket with permissions to delete
assets = bucket('assets').allow('deleting')

logo = assets.file('images/logo.png')

await logo.delete()

Nitric.run()
Enter fullscreen mode Exit fullscreen mode

Avoid writing or deleting to the bucket from within a notification as this can trigger the notification again, potentially causing an infinite loop which can be costly.

Let's run this thing!

nitric start
Enter fullscreen mode Exit fullscreen mode

Conclusion

Now you have set up bucket notifications in Python using the Nitric SDK.

This setup will help you monitor file write and delete events, allowing your application to react to changes in your bucket accordingly.

Top comments (0)