Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 38ccb43

Browse filesBrowse files
author
Ace Nassri
authored
ImageMagick: use separate bucket for blurred images. (GoogleCloudPlatform#2199)
* ImageMagick: use separate bucket for blurred images. * Address Charlie's comments * Fix lint
1 parent 29ef01e commit 38ccb43
Copy full SHA for 38ccb43

File tree

Expand file treeCollapse file tree

4 files changed

+28
-11
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+28
-11
lines changed

‎functions/imagemagick/README.md

Copy file name to clipboardExpand all lines: functions/imagemagick/README.md
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,23 @@ Functions for your project.
2121

2222
1. Create a Cloud Storage Bucket:
2323

24-
gsutil mb gs://YOUR_BUCKET_NAME
24+
gsutil mb gs://YOUR_INPUT_BUCKET_NAME
2525

2626
This storage bucket is used to upload images for the function to check.
2727

28+
1. Create a second Cloud Storage Bucket:
29+
30+
gsutil mb gs://YOUR_OUTPUT_BUCKET_NAME
31+
32+
This second storage bucket is used to store blurred images. (Un-blurred images will not be saved to this bucket.)
33+
34+
This is necessary because saving the blurred image to the input bucket would cause your function to be invoked a second time with the blurred image itself.
35+
2836
1. Deploy the `blur_offensive_images` function with a Storage trigger:
2937

30-
gcloud functions deploy blur_offensive_images --trigger-bucket=YOUR_BUCKET_NAME --runtime python37
38+
gcloud functions deploy blur_offensive_images --trigger-bucket=YOUR_INPUT_BUCKET_NAME --set-env-vars BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME --runtime python37
3139

32-
* Replace `YOUR_BUCKET_NAME` with the name of the Cloud Storage Bucket you created earlier.
40+
* Replace `YOUR_INPUT_BUCKET_NAME` and `YOUR_OUTPUT_BUCKET_NAME` with the names of the respective Cloud Storage Buckets you created earlier.
3341

3442
1. Upload an offensive image to the Storage bucket, such as this image of
3543
a flesh-eating zombie: https://cdn.pixabay.com/photo/2015/09/21/14/24/zombie-949916_1280.jpg

‎functions/imagemagick/main.py

Copy file name to clipboardExpand all lines: functions/imagemagick/main.py
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ def __blur_image(current_blob):
7373

7474
print(f'Image {file_name} was blurred.')
7575

76-
# Send Blurred image back to the bucket (with a 'blurred-' prefix).
77-
# The prefix is necessary to avoid re-invoking the function upon upload.
78-
new_file_name = f'blurred-{file_name}'
79-
new_blob = current_blob.bucket.blob(new_file_name)
76+
# Upload result to a second bucket, to avoid re-triggering the function.
77+
# You could instead re-upload it to the same bucket + tell your function
78+
# to ignore files marked as blurred (e.g. those with a "blurred" prefix)
79+
blur_bucket_name = os.getenv('BLURRED_BUCKET_NAME')
80+
blur_bucket = storage_client.bucket(blur_bucket_name)
81+
new_blob = blur_bucket.blob(file_name)
8082
new_blob.upload_from_filename(temp_local_filename)
81-
print(f'Blurred image was uploaded to {new_file_name}.')
83+
print(f'Blurred image uploaded to: gs://{blur_bucket_name}/{file_name}')
8284

8385
# Delete the temporary file.
8486
os.remove(temp_local_filename)

‎functions/imagemagick/main_test.py

Copy file name to clipboardExpand all lines: functions/imagemagick/main_test.py
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,23 @@ def test_process_safe_image(
7979

8080
@patch('main.os')
8181
@patch('main.Image')
82-
def test_blur_image(image_mock, os_mock, capsys):
82+
@patch('main.storage_client')
83+
def test_blur_image(storage_client, image_mock, os_mock, capsys):
8384
filename = str(uuid.uuid4())
85+
blur_bucket = 'blurred-bucket-' + str(uuid.uuid4())
8486

8587
os_mock.remove = MagicMock()
8688
os_mock.path = MagicMock()
8789
os_mock.path.basename = MagicMock(side_effect=(lambda x: x))
8890

91+
os_mock.getenv = MagicMock(return_value=blur_bucket)
92+
8993
image_mock.return_value = image_mock
9094
image_mock.__enter__.return_value = image_mock
9195

9296
blob = UserDict()
9397
blob.name = filename
9498
blob.bucket = UserDict()
95-
blob.bucket.blob = MagicMock(return_value=blob)
9699
blob.download_to_filename = MagicMock()
97100
blob.upload_from_filename = MagicMock()
98101

@@ -102,6 +105,6 @@ def test_blur_image(image_mock, os_mock, capsys):
102105

103106
assert f'Image {filename} was downloaded to' in out
104107
assert f'Image {filename} was blurred.' in out
105-
assert f'Blurred image was uploaded to blurred-{filename}.' in out
108+
assert f'Blurred image uploaded to: gs://{blur_bucket}/{filename}' in out
106109
assert os_mock.remove.called
107110
assert image_mock.resize.called
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mock==3.0.5
2+
six==1.12.0
3+
uuid==1.30
4+
pytest==4.6.2

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.