Skip to main content

Google Cloud function to generate thumbnail for images in Google Storage.

Project description

picture-svgrepo-com.svg https://madewithlove.now.sh/vn?heart=true&colorA=%23ffcd00&colorB=%23da251d https://badge.fury.io/py/thunagen.svg

Google Cloud function to generate thumbnail for images in Google Storage.

Convention

The thumbnails are placed in a folder “thumbnails” at the same place as original file.

The thumbnail size is appended to filename, right before the extention part. For example:

bucket/
└── folder/
    ├── photo.jpg
    └── thumbnails/
        ├── photo_128x128.jpg
        └── photo_512x512.jpg
    ├── photo-missing-extension
    └── thumbnails/
        ├── photo-missing-extension_128x128
        └── photo-missing-extension_512x512

The function expects these environment variables to be set:

  • THUMB_SIZES: Size of thumbnails to be generated. Example: 512x512,128x128.

  • MONITORED_PATHS: Folders (and theirs children) where the function will process the uploaded images. Muliple paths are separated by “:”, like user-docs:user-profiles. If you want to monitor all over the bucket, set it as /.

  • NOTIFY_THUMBNAIL_GENERATED (optional): Tell Thunagen to notify after thumbnails are created.

The variables can be passed via .env file in the working directory.

Get notified when thumbnails are generated

Other applications may want to be informed when the thumbnails are created. We support this by leveraging Google Cloud Pub/Sub service.

After finishing generating thumbnail, if the NOTIFY_THUMBNAIL_GENERATED environment variable is set (with non-empty value), the function will publish a message to Pub/Sub. The message is sent to topic thumbnail-generated/{bucket_name}/{image_path}, with the content being JSON string of thumbnail info (size and path). Example:

  • Topic: thumbnail-generated%2Fbucket%2Ffolder%2Fphoto.jpg (URL-encoded of “thumbnail-generated/bucket/folder/photo.jpg”)

  • Message:

    {
        "128x128": "folder/thumbnails/photo_128x128.jpg",
        "512x512": "folder/thumbnails/photo_512x512.jpg"
    }

Other applications can subscribe to that topic to get notified. Google doesnot allow slash (“/”) in topic name, so subscribed applications have to take care of URL-encode, decode the topic.

Why Thunagen

I’m aware that there is already a Firebase extension for the same purpose. But that extension, when doing its job, need to create a temporary file and in many cases, falling into race condition when the temporary file is deleted by another execution of the same cloud function. Thunagen, on the other hand, generates the file and uploads (back to Storage) on-the-fly (in memory), so it doesn’t get into that issue.

Installation

Thunagen is distributed via PyPI. You can install it with pip:

pip install thunagen

Include to your project

Thunagen is provided without a main.py file, for you to incorporate more easily to your project, where you may have your own way to configure deployment environment (different bucket for “staging” and “production”, for example).

To include Thunagen, from your main.py, do:

from thunagen.functions import generate_gs_thumbnail

Credit

Thunagen is brought to you by Nguyễn Hồng Quân, from SunshineTech (Việt Nam).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thunagen-0.3.8.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

thunagen-0.3.8-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file thunagen-0.3.8.tar.gz.

File metadata

  • Download URL: thunagen-0.3.8.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.7.5 Linux/5.3.0-40-generic

File hashes

Hashes for thunagen-0.3.8.tar.gz
Algorithm Hash digest
SHA256 2b73c525c0c36360eab124b8170fe5810ececad789d077c5886bf33b506c806a
MD5 9808c888e92a759ac2d8fc52a0e32200
BLAKE2b-256 d2f8463a0f79394ddccfff3c92f99929981eb31b6c254cdd0a41b566059ca535

See more details on using hashes here.

File details

Details for the file thunagen-0.3.8-py3-none-any.whl.

File metadata

  • Download URL: thunagen-0.3.8-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0 CPython/3.7.5 Linux/5.3.0-40-generic

File hashes

Hashes for thunagen-0.3.8-py3-none-any.whl
Algorithm Hash digest
SHA256 0587e3fad7e319d7690e15a5adcd7df2d57279048fe11e3a911ff97d2269cb4c
MD5 46c51c3fccba7839a885c187ea2a29b0
BLAKE2b-256 72c32d55d2944df7aac45f300dbe56d25375473f9c2f135d6eeee5184c6ceaf9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page