Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
migrate cloud function to separate repo
Browse files Browse the repository at this point in the history
  • Loading branch information
samwachspress committed Sep 14, 2021
1 parent 03bcb6f commit c3bb8f4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gcloudignore
@@ -0,0 +1,12 @@
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
2 changes: 2 additions & 0 deletions README.md
@@ -0,0 +1,2 @@
# Generate Thumbnails
Google Cloud Function to monitor photo directory of site content bucket and generate thumbnails when new images are added
26 changes: 26 additions & 0 deletions main.py
@@ -0,0 +1,26 @@
import re
from wand.image import Image
from google.cloud import storage

client = storage.Client()

def generate_thumbnails(data, context):
thumbnail_regex = "^photos\/Celegans\/.*\.thumb.(jpg|jpeg)$"
image_regex = "^photos\/Celegans\/.*\.(jpg|jpeg)$"

# Only generate thumbnails for matching paths
is_image = re.search(image_regex, data['name'])
is_thumbnail = re.search(thumbnail_regex, data['name'])

if (is_image and not is_thumbnail):
# Download the image and resize it
bucket = client.get_bucket(data['bucket'])
thumbnail = Image(blob=bucket.get_blob(data['name']).download_as_string())
thumbnail.transform(resize='x200')

# Upload the thumbnail with modified name
path = data['name'].rsplit('.', 1)
blob_name = path[0] + ".thumb." + path[1]
thumbnail_blob = bucket.blob(blob_name)
thumbnail_blob.upload_from_string(thumbnail.make_blob())

2 changes: 2 additions & 0 deletions requirements.txt
@@ -0,0 +1,2 @@
google-cloud-storage
wand

0 comments on commit c3bb8f4

Please sign in to comment.