Inspect data using a hybrid job (v2)

Send data to an active hybrid inspection job to detect sensitive information.

Code sample

Node.js

To learn how to install and use the client library for Sensitive Data Protection, see Sensitive Data Protection client libraries.

To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'use strict';

const {DlpServiceClient} = require('@google-cloud/dlp');
const {status} = require('@grpc/grpc-js');

const client = new DlpServiceClient();

/**
 * Use the hybridInspectDlpJob method
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The Google Cloud location or region for the DLP job (for example, 'global' or 'us-central1').
 * @param {string} dlpJobId - The server-generated or caller-specified ID of the running hybrid DLP job.
 */
async function hybridInspectDlpJob(projectId, locationId, dlpJobId) {
  try {
    const name = `projects/${projectId}/locations/${locationId}/dlpJobs/${dlpJobId}`;
    const hybridItem = {
      item: {
        value: 'My email is user@example.com and phone is 555-0123',
      },
    };

    const request = {
      name,
      hybridItem,
    };

    await client.hybridInspectDlpJob(request);
    console.log(`Hybrid inspection request processed for job: ${name}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`DLP job not found: ${err.message}`);
      return;
    }
    if (err.code === status.FAILED_PRECONDITION) {
      console.error(`Job not in active state: ${err.message}`);
      return;
    }
    console.error(`Google API error [code ${err.code}]: ${err.message}`);
  }
}

module.exports = { hybridInspectDlpJob };

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.