-
Notifications
You must be signed in to change notification settings - Fork 2k
Add DLP redact image sample #438
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
Changes from 3 commits
ed85afb
6202c3a
d6ac9f4
221a1ed
6cda6a7
371ab1b
e6a6fb7
9cd25e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| **/*.result.png |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,61 @@ function redactString (string, replaceString, minLikelihood, infoTypes) { | |
| // [END redact_string] | ||
| } | ||
|
|
||
| function redactImage (filepath, minLikelihood, infoTypes, outputPath) { | ||
| // [START redact_image] | ||
| // Imports required Node.js libraries | ||
| const mime = require('mime'); | ||
| const fs = require('fs'); | ||
|
|
||
| // Imports the Google Cloud Data Loss Prevention library | ||
| const DLP = require('@google-cloud/dlp'); | ||
|
|
||
| // Instantiates a client | ||
| const dlp = DLP(); | ||
|
|
||
| // The path to a local file to inspect. Can be a JPG or PNG image file. | ||
| // const fileName = 'path/to/image.png'; | ||
|
|
||
| // The minimum likelihood required before redacting a match | ||
| // const minLikelihood = LIKELIHOOD_UNSPECIFIED; | ||
|
|
||
| // The infoTypes of information to redact | ||
| // const infoTypes = ['EMAIL_ADDRESS', 'PHONE_NUMBER']; | ||
|
|
||
| // The local path to save the resulting image to. | ||
| // const outputPath = 'result.png'; | ||
|
|
||
| const fileItems = [{ | ||
| type: mime.lookup(filepath) || 'application/octet-stream', | ||
| data: Buffer.from(fs.readFileSync(filepath)).toString('base64') | ||
| }]; | ||
|
|
||
| const imageRedactionConfigs = infoTypes.map((infoType) => { | ||
| return { infoType: infoType }; | ||
| }); | ||
|
|
||
| const request = { | ||
| inspectConfig: { | ||
| minLikelihood: minLikelihood | ||
| }, | ||
| imageRedactionConfigs: imageRedactionConfigs, | ||
| items: fileItems | ||
| }; | ||
|
|
||
| dlp.redactContent(request) | ||
| .then((response) => { | ||
| const image = response[0].items[0].data; | ||
| fs.writeFileSync(outputPath, image); | ||
| }) | ||
| .then(() => { | ||
| console.log(`Saved image redaction results to path: ${outputPath}`); | ||
|
Member
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. This
Contributor
Author
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. True - unless we decide to promisify
Member
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. Alright, keep the sync function and move this up. |
||
| }) | ||
| .catch((err) => { | ||
| console.log(`Error in redactImage: ${err.message || err}`); | ||
| }); | ||
| // [END redact_image] | ||
| } | ||
|
|
||
| const cli = require(`yargs`) | ||
| .demand(1) | ||
| .command( | ||
|
|
@@ -72,6 +127,12 @@ const cli = require(`yargs`) | |
| {}, | ||
| (opts) => redactString(opts.string, opts.replaceString, opts.minLikelihood, opts.infoTypes) | ||
| ) | ||
| .command( | ||
| `image <filepath> <outputPath>`, | ||
| `Redact sensitive data from an image using the Data Loss Prevention API.`, | ||
| {}, | ||
| (opts) => redactImage(opts.filepath, opts.minLikelihood, opts.infoTypes, opts.outputPath) | ||
| ) | ||
| .option('m', { | ||
| alias: 'minLikelihood', | ||
| default: 'LIKELIHOOD_UNSPECIFIED', | ||
|
|
@@ -96,6 +157,7 @@ const cli = require(`yargs`) | |
| }) | ||
| }) | ||
| .example(`node $0 string "My name is Gary" "REDACTED" -t US_MALE_NAME`) | ||
| .example(`node $0 image resources/test.png redaction_result.png -t US_MALE_NAME`) | ||
| .wrap(120) | ||
| .recommendCommands() | ||
| .epilogue(`For more information, see https://cloud.google.com/dlp/docs. Optional flags are explained at https://cloud.google.com/dlp/docs/reference/rest/v2beta1/content/inspect#InspectConfig`); | ||
|
|
||
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.
Best practice would not be to use a blocking call to write the file, though it makes the sample simpler. I'll leave it up to you.
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.
Agreed all around - I was going with the simpler-sample approach here.
We could use this module, if we don't mind adding additional dependencies.
Otherwise, I'd personally vote for using the
syncfunctions.