ImageMagick: use separate bucket for blurred images. (#2199) · code4ward/python-docs-samples@38ccb43 · GitHub
Skip to content

Commit 38ccb43

Browse 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

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

functions/imagemagick/README.md

Lines changed: 11 additions & 3 deletions

functions/imagemagick/main.py

Lines 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

Lines 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
Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)