-
Notifications
You must be signed in to change notification settings - Fork 2k
Add Cloud Functions ImageMagick sample. #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,55 @@ | ||
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/> | ||
|
||
# Google Cloud Functions ImageMagick sample | ||
|
||
This sample shows you how to blur an image using ImageMagick in a | ||
Storage-triggered Cloud Function. | ||
|
||
View the [source code][code]. | ||
|
||
[code]: index.js | ||
|
||
## Deploy and Test | ||
|
||
1. Follow the [Cloud Functions quickstart guide][quickstart] to setup Cloud | ||
Functions for your project. | ||
|
||
1. Clone this repository: | ||
|
||
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git | ||
cd nodejs-docs-samples/functions/imagemagick | ||
|
||
1. Create a Cloud Storage bucket for storing images (if you already have one you | ||
want to use, you can skip this step): | ||
|
||
gsutil mb gs://YOUR_BUCKET_NAME | ||
|
||
* Replace `YOUR_BUCKET_NAME` with the name of your image Bucket. | ||
|
||
1. Create a Cloud Storage Bucket to stage our deployment: | ||
|
||
gsutil mb gs://YOUR_STAGE_BUCKET_NAME | ||
|
||
* Replace `YOUR_STAGE_BUCKET_NAME` with the name of your Cloud Storage Bucket. | ||
|
||
1. Deploy the `blurOffensiveImages` function with a Storage trigger: | ||
|
||
gcloud alpha functions deploy blurOffensiveImages --trigger-bucket=YOUR_BUCKET_NAME --stage-bucket=YOUR_STAGE_BUCKET_NAME | ||
|
||
* Replace `YOUR_BUCKET_NAME` with the name of your image Cloud Storage Bucket. | ||
* Replace `YOUR_STAGE_BUCKET_NAME` with the name of your Cloud Storage Bucket. | ||
|
||
1. Upload an offensive image to your image Storage bucket, such as this image of | ||
a flesh-eating zombie: https://cdn.pixabay.com/photo/2015/09/21/14/24/zombie-949916_1280.jpg | ||
|
||
1. Check the logs for the `blurOffensiveImages` function: | ||
|
||
gcloud beta functions get-logs blurOffensiveImages | ||
|
||
You should see something like this in your console: | ||
|
||
D ... User function triggered, starting execution | ||
I ... `The image zombie.jpg has been detected as inappropriate.` | ||
D ... Execution took 1 ms, user function completed successfully | ||
|
||
[quickstart]: https://cloud.google.com/functions/quickstart |
This file contains hidden or 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,105 @@ | ||
/** | ||
* Copyright 2017, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const exec = require('child_process').exec; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const storage = require('@google-cloud/storage')(); | ||
const vision = require('@google-cloud/vision')(); | ||
|
||
// Blurs uploaded images that are flagged as Adult or Violence. | ||
exports.blurOffensiveImages = (event) => { | ||
const object = event.data; | ||
|
||
// Exit if this is a deletion or a deploy event. | ||
if (object.resourceState === 'not_exists') { | ||
console.log('This is a deletion event.'); | ||
return; | ||
} else if (!object.name) { | ||
console.log('This is a deploy event.'); | ||
return; | ||
} | ||
|
||
const file = storage.bucket(object.bucket).file(object.name); | ||
|
||
console.log(`Analyzing ${file.name}.`); | ||
|
||
return vision.detectSafeSearch(file) | ||
.catch((err) => { | ||
console.error(`Failed to analyze ${file.name}.`, err); | ||
return Promise.reject(err); | ||
}) | ||
.then(([safeSearch]) => { | ||
if (safeSearch.adult || safeSearch.violence) { | ||
console.log(`The image ${file.name} has been detected as inappropriate.`); | ||
return blurImage(file, safeSearch); | ||
} else { | ||
console.log(`The image ${file.name} has been detected as OK.`); | ||
} | ||
}); | ||
}; | ||
|
||
// Blurs the given file using ImageMagick. | ||
function blurImage (file) { | ||
const tempLocalFilename = `/tmp/${path.parse(file.name).base}`; | ||
|
||
// Download file from bucket. | ||
return file.download({ destination: tempLocalFilename }) | ||
.catch((err) => { | ||
console.error('Failed to download file.', err); | ||
return Promise.reject(err); | ||
}) | ||
.then(() => { | ||
console.log(`Image ${file.name} has been downloaded to ${tempLocalFilename}.`); | ||
|
||
// Blur the image using ImageMagick. | ||
return new Promise((resolve, reject) => { | ||
exec(`convert ${tempLocalFilename} -channel RGBA -blur 0x24 ${tempLocalFilename}`, { stdio: 'ignore' }, (err, stdout) => { | ||
if (err) { | ||
console.error('Failed to blur image.', err); | ||
reject(err); | ||
} else { | ||
resolve(stdout); | ||
} | ||
}); | ||
}); | ||
}) | ||
.then(() => { | ||
console.log(`Image ${file.name} has been blurred.`); | ||
|
||
// Upload the Blurred image back into the bucket. | ||
return file.bucket.upload(tempLocalFilename, { destination: file.name }) | ||
.catch((err) => { | ||
console.error('Failed to upload blurred image.', err); | ||
return Promise.reject(err); | ||
}); | ||
}) | ||
.then(() => { | ||
console.log(`Blurred image has been uploaded to ${file.name}.`); | ||
|
||
// Delete the temporary file. | ||
return new Promise((resolve, reject) => { | ||
fs.unlink(tempLocalFilename, (err) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}); | ||
} |
This file contains hidden or 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,33 @@ | ||
{ | ||
"name": "nodejs-docs-samples-functions-imagemagick", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache-2.0", | ||
"author": "Google Inc.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
}, | ||
"cloud": { | ||
"requiresKeyFile": true, | ||
"requiresProjectId": true | ||
}, | ||
"engines": { | ||
"node": ">=4.3.2" | ||
}, | ||
"scripts": { | ||
"lint": "samples lint", | ||
"pretest": "npm run lint", | ||
"test": "ava -T 20s --verbose test/*.test.js" | ||
}, | ||
"dependencies": { | ||
"@google-cloud/storage": "1.1.0", | ||
"@google-cloud/vision": "0.11.2" | ||
}, | ||
"devDependencies": { | ||
"@google-cloud/nodejs-repo-tools": "1.3.1", | ||
"ava": "0.19.1", | ||
"proxyquire": "1.7.11", | ||
"sinon": "2.1.0" | ||
} | ||
} |
This file contains hidden or 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,105 @@ | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought: do we want to add any system tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The functions samples are all unit tests for now. |
||
* Copyright 2017, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const proxyquire = require(`proxyquire`).noCallThru(); | ||
const sinon = require(`sinon`); | ||
const test = require(`ava`); | ||
const tools = require(`@google-cloud/nodejs-repo-tools`); | ||
|
||
const bucketName = `my-bucket`; | ||
const filename = `image.jpg`; | ||
|
||
function getSample () { | ||
const file = { | ||
getMetadata: sinon.stub().returns(Promise.resolve([{}])), | ||
setMetadata: sinon.stub().returns(Promise.resolve()), | ||
download: sinon.stub().returns(Promise.resolve()), | ||
bucket: bucketName, | ||
name: filename | ||
}; | ||
const bucket = { | ||
file: sinon.stub().returns(file), | ||
upload: sinon.stub().returns(Promise.resolve()) | ||
}; | ||
file.bucket = bucket; | ||
const storageMock = { | ||
bucket: sinon.stub().returns(bucket) | ||
}; | ||
const visionMock = { | ||
detectSafeSearch: sinon.stub().returns(Promise.resolve([{ violence: true }])) | ||
}; | ||
const StorageMock = sinon.stub().returns(storageMock); | ||
const VisionMock = sinon.stub().returns(visionMock); | ||
const childProcessMock = { | ||
exec: sinon.stub().yields() | ||
}; | ||
const fsMock = { | ||
unlink: sinon.stub().yields() | ||
}; | ||
|
||
return { | ||
program: proxyquire(`../`, { | ||
'@google-cloud/vision': VisionMock, | ||
'@google-cloud/storage': StorageMock, | ||
'child_process': childProcessMock, | ||
'fs': fsMock | ||
}), | ||
mocks: { | ||
fs: fsMock, | ||
childProcess: childProcessMock, | ||
storage: storageMock, | ||
bucket, | ||
file, | ||
vision: visionMock | ||
} | ||
}; | ||
} | ||
|
||
test.beforeEach(tools.stubConsole); | ||
test.afterEach.always(tools.restoreConsole); | ||
|
||
test.serial(`blurOffensiveImages does nothing on delete`, async (t) => { | ||
await getSample().program.blurOffensiveImages({ data: { resourceState: `not_exists` } }); | ||
t.is(console.log.callCount, 1); | ||
t.deepEqual(console.log.getCall(0).args, ['This is a deletion event.']); | ||
}); | ||
|
||
test.serial(`blurOffensiveImages does nothing on deploy`, async (t) => { | ||
await getSample().program.blurOffensiveImages({ data: {} }); | ||
t.is(console.log.callCount, 1); | ||
t.deepEqual(console.log.getCall(0).args, ['This is a deploy event.']); | ||
}); | ||
|
||
test.serial(`blurOffensiveImages blurs images`, async (t) => { | ||
const sample = getSample(); | ||
await sample.program.blurOffensiveImages({ data: { bucket: bucketName, name: filename } }); | ||
t.is(console.log.callCount, 5); | ||
t.deepEqual(console.log.getCall(0).args, [`Analyzing ${sample.mocks.file.name}.`]); | ||
t.deepEqual(console.log.getCall(1).args, [`The image ${sample.mocks.file.name} has been detected as inappropriate.`]); | ||
t.deepEqual(console.log.getCall(2).args, [`Image ${sample.mocks.file.name} has been downloaded to /tmp/${sample.mocks.file.name}.`]); | ||
t.deepEqual(console.log.getCall(3).args, [`Image ${sample.mocks.file.name} has been blurred.`]); | ||
t.deepEqual(console.log.getCall(4).args, [`Blurred image has been uploaded to ${sample.mocks.file.name}.`]); | ||
}); | ||
|
||
test.serial(`blurOffensiveImages ignores safe images`, async (t) => { | ||
const sample = getSample(); | ||
sample.mocks.vision.detectSafeSearch = sinon.stub().returns(Promise.resolve([{}])); | ||
await sample.program.blurOffensiveImages({ data: { bucket: bucketName, name: filename } }); | ||
t.is(console.log.callCount, 2); | ||
t.deepEqual(console.log.getCall(0).args, [`Analyzing ${sample.mocks.file.name}.`]); | ||
t.deepEqual(console.log.getCall(1).args, [`The image ${sample.mocks.file.name} has been detected as OK.`]); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NSFW warning? 😛