MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

dlp/inspectImageFile.js:21–95  ·  view source on GitHub ↗
(projectId, imagePath)

Source from the content-addressed store, hash-verified

19// description: Inspect a image for sensitive data like Phone Number, Email.
20// usage: node inspectImageFile.js.js my-project imagePath infoTypes
21function main(projectId, imagePath) {
22 // [START dlp_inspect_image_file]
23 // Imports the Google Cloud Data Loss Prevention library
24 const DLP = require('@google-cloud/dlp');
25 const mime = require('mime');
26 const fs = require('fs');
27
28 // Instantiates a client
29 const dlp = new DLP.DlpServiceClient();
30
31 // The project ID to run the API call under
32 // const imagePath = './test.jpeg';
33
34 // InfoTypes
35 const infoTypes = [
36 {name: 'PHONE_NUMBER'},
37 {name: 'EMAIL_ADDRESS'},
38 {name: 'CREDIT_CARD_NUMBER'},
39 ];
40
41 async function inspectImageFile() {
42 let fileBytes = null;
43 let fileTypeConstant = null;
44 try {
45 // Load Image
46 fileTypeConstant =
47 ['image/jpeg', 'image/bmp', 'image/png', 'image/svg'].indexOf(
48 mime.getType(imagePath)
49 ) + 1;
50 fileBytes = Buffer.from(fs.readFileSync(imagePath)).toString('base64');
51 } catch (error) {
52 console.log(error);
53 return;
54 }
55
56 // Specify item to inspect
57 const item = {
58 byteItem: {
59 type: fileTypeConstant,
60 data: fileBytes,
61 },
62 };
63
64 // Specify inspect configuration to match information with mentioned infotypes.
65 const inspectConfig = {
66 infoTypes: infoTypes,
67 includeQuote: true,
68 };
69
70 // Combine configurations into a request for the service.
71 const request = {
72 parent: `projects/${projectId}/locations/global`,
73 inspectConfig: inspectConfig,
74 item: item,
75 };
76
77 // Use the client to send the request.
78 const [response] = await dlp.inspectContent(request);

Callers 1

Calls 1

inspectImageFileFunction · 0.85

Tested by

no test coverage detected