This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
forked from AndersenLab/CeNDR
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate cloud function to separate repo
- Loading branch information
1 parent
03bcb6f
commit c3bb8f4
Showing
4 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
google-cloud-storage | ||
wand |