All Sensitive Data Protection code samples
This page contains code samples for Sensitive Data Protection. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.
Inspect data with a custom regex
Regex example: Matching medical record numbers. The following sample uses a regular expression custom infoType detector that instructs Cloud DLP to match a medical record number (MRN) in the input text "Patient's MRN 444-5-22222," and then assigns each match a likelihood of POSSIBLE.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Go
- Node JS
- Python
- PHP
- C#
- Java
- JavaScript
De-identify table data with infoTypes
Transform findings found in columns. You can transform findings that either make up part of a cell's content or all of it. In this example, all instances of PERSON_NAME are anonymized.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Node JS
- C#
- Python
- PHP
- Go
- Java
- JavaScript
Inspect a string for sensitive data, omitting overlapping matches on domain and email
Omit matches on domain names that are part of email addresses in a DOMAIN_NAME detector scan.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Node JS
- Go
- C#
- JavaScript
- Python
- Java
- PHP
De-identify tabular data through bucketing
This sample replaces the values within each bucket with predefined replacement values.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Java
- Go
- PHP
- Node JS
- C#
- JavaScript
- Python
Inspect a string for sensitive data by using multiple rules
Illustrates applying both exclusion and hotword rules. This snippet's rule set includes both hotword rules and dictionary and regex exclusion rules. Notice that the four rules are specified in an array within the rules element.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- JavaScript
- C#
- Python
- PHP
- Go
- Java
- Node JS
List information types for a category
Demonstrates listing information types for a category.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- C#
- JavaScript
- Node JS
- Python
- Go
- PHP
- Java
Client library quickstart
Demonstrates inspecting a string with the Cloud DLP API.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Java
- Python
- Node JS
- C#
- PHP
- Ruby
- Go
- JavaScript
Inspect a local file
Demonstrates finding sensitive data in a local text or image file.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Java
- Node JS
- Go
- Python
- C#
- Ruby
- JavaScript
Inspect a string from sensitive data by using a custom hotword
Increase the likelihood of a PERSON_NAME match if there is the hotword "patient" nearby. Illustrates using the InspectConfig property for the purpose of scanning a medical database for patient names. You can use Cloud DLP's built-in PERSON_NAME infoType detector, but that causes Cloud DLP to match on all names of people, not just names of patients. To fix this, you can include a hotword rule that looks for the word "patient" within a certain character proximity from the first character of potential matches. You can then assign findings that match this pattern a likelihood of "very likely," since they correspond to your special criteria. Setting the minimum likelihood to VERY_LIKELY within InspectConfig ensures that only matches to this configuration are returned in findings.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- C#
- JavaScript
- Java
- PHP
- Go
- Python
- Node JS
Get an inspection job
Get DLP inspection job.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Python
- JavaScript
- Node JS
- Java
- C#
- Go
- PHP
Inspect a string for sensitive data, omitting overlapping matches on person and email
Omit matches on a PERSON_NAME detector if also matched by an EMAIL_ADDRESS detector.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- C#
- PHP
- Python
- Go
- Java
- Node JS
- JavaScript
Inspect BigQuery for sensitive data with sampling
The following examples demonstrate using the Cloud Data Loss Prevention API to scan a 1000-row subset of a BigQuery table. The scan starts from a random row.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Python
- JavaScript
- Node JS
- Go
- PHP
- C#
- Java
Compute l-diversity
Compute l-diversity with Cloud DLP. L-diversity, which is an extension of k-anonymity, measures the diversity of sensitive values for each column in which they occur. A dataset has l-diversity if, for every set of rows with identical quasi-identifiers, there are at least l distinct values for each sensitive attribute.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- PHP
- Java
- Go
- Node JS
- Python
- JavaScript
- C#
Scan content using a large custom dictionary detector
This sample scans the given text using the specified stored infoType detector.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- PHP
- Go
- Python
- Node JS
- JavaScript
- C#
- Java
Computing k-map estimates
You can estimate k-map values using Cloud DLP, which uses a statistical model to estimate a re-identification dataset.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Go
- PHP
- C#
- Python
- Java
- Node JS
- JavaScript
List triggers
List all job triggers for the current project.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Node JS
- PHP
- Go
- JavaScript
- C#
- Java
- Python
Inspect BigQuery for sensitive data
Demonstrates finding sensitive data that is stored in BigQuery.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- C#
- JavaScript
- Java
- Go
- Python
- Node JS
- PHP
Redact only certain sensitive data from an image using infoTypes
Redact only certain sensitive data from an image.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Go
- Python
- JavaScript
- Java
- Node JS
- PHP
- C#
Inspect a Cloud Storage file
Demonstrates finding sensitive data in a file that is located in Cloud Storage.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Go
- Node JS
- JavaScript
- Java
- PHP
- C#
- Python
Inspect data for phone numbers
Demonstrates a simple scan request to the Cloud DLP API. Notice that the PHONE_NUMBER detector is specified in inspectConfig, which instructs Cloud DLP to scan the given string for a phone number.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- PHP
- Python
- Node JS
- C#
- Go
- JavaScript
- Java
Inspect a string
Demonstrates finding sensitive data in a string.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- C#
- Python
- Java
- PHP
- JavaScript
- Go
- Node JS
- Ruby
De-identify data: Redacting with matched input values
Uses the Data Loss Prevention API to de-identify sensitive data in a string by redacting matched input values.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Java
- Go
- JavaScript
- C#
- Node JS
- PHP
- Python
Delete an inspection template
Delete an inspection template from Cloud DLP.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Node JS
- JavaScript
- C#
- Python
- Go
- Java
- PHP
De-identify table data with format-preserving encryption
Demonstrates encrypting sensitive data in a table while maintaining format.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Python
- Node JS
- C#
- JavaScript
- Go
- PHP
- Java
Redact all detected text in an image
Redact all detected text in an image.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- PHP
- C#
- Java
- Go
- Python
- Node JS
- JavaScript
Compute numerical statistics
You can determine minimum, maximum, and quantile values for an individual BigQuery column. To calculate these values, you configure a DlpJob, setting the NumericalStatsConfig privacy metric to the name of the column to scan. When you run the job, Cloud DLP computes statistics for the given column, returning its results in the NumericalStatsResult object.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Node JS
- JavaScript
- Python
- PHP
- Java
- Go
- C#
Re-identify table data with FPE
Re-identify table data with format-preserving encryption.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- JavaScript
- Node JS
- PHP
- Python
- C#
- Go
- Java
Inspect a string for sensitive data, using exclusion dictionary
Omit a specific email address from an EMAIL_ADDRESS detector scan with an exclusion dictionary.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Node JS
- Go
- JavaScript
- PHP
- Java
- C#
- Python
Create an inspection job
Creates an inspection job with the Cloud Data Loss Prevention API.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Java
- C#
- Python
- Node JS
- PHP
- JavaScript
- Go
Re-identify content encrypted by deterministic encryption
Re-identify content that was previously de-identified through deterministic encryption.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- C#
- JavaScript
- Go
- PHP
- Java
- Node JS
- Python
Delete a trigger
Delete a DLP trigger.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Java
- PHP
- C#
- Go
- Python
- Node JS
- JavaScript
Redact data from an image with color-coded infoTypes
Redacting infoTypes from an image with color coding.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Python
- Java
- Node JS
- Go
- C#
- PHP
- JavaScript
Computing k-anonymity
K-anonymity is a property of a dataset that indicates the re-identifiability of its records. A dataset is k-anonymous if quasi-identifiers for each person in the dataset are identical to at least k – 1 other people also in the dataset. This sample demonstrates how to use Cloud DLP to compute a k-anonymity value.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Go
- C#
- PHP
- Python
- Node JS
- JavaScript
- Java
Delete a job
Delete a DLP job.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Go
- Node JS
- PHP
- Python
- JavaScript
- Java
- C#
Inspect Datastore
Demonstrates finding sensitive data stored in Datastore.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- JavaScript
- C#
- Go
- PHP
- Python
- Node JS
- Java
Create an exception list for de-identification
Create an exception list for a regular custom dictionary detector.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Node JS
- Java
- JavaScript
- Python
- PHP
- C#
- Go
Transform findings using a cryptographic hash transformation
This sample transforms tabular data through a cryptographic hash transformation.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Python
- Java
- Go
- C#
- PHP
- Node JS
- JavaScript
Inspect a string for sensitive data, omitting custom matches
Omit scan matches from a PERSON_NAME detector scan that overlap with a custom detector.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Java
- Python
- PHP
- C#
- Go
- Node JS
- JavaScript
De-identify sensitive data with a simple word list
Matches against a custom simple word list to de-identify sensitive data.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- JavaScript
- C#
- Go
- Python
- Java
- Node JS
- PHP
Inspect a string for sensitive data, excluding a custom substring
Illustrates how to use an InspectConfig to instruct Cloud DLP to avoid matching on the name "Jimmy" in a scan that uses the specified custom regular expression detector.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- JavaScript
- Java
- Go
- Python
- PHP
- C#
- Node JS
De-identify content through deterministic encryption
Use the Data Loss Prevention API to de-identify sensitive data in a string using deterministic encryption, which is a reversible cryptographic method. The encryption is performed with a wrapped key.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Java
- Node JS
- PHP
- Go
- Python
- JavaScript
- C#
Inspect an image file for sensitive data
Uses Cloud DLP to inspect an image for sensitive data.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- JavaScript
- Go
- C#
- Python
- Node JS
- PHP
- Java
De-identify data using table bucketing
Transform a column without inspection. To transform a column in which the content is already known, you can skip inspection and specify a transformation directly.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- C#
- PHP
- Go
- Java
- JavaScript
- Node JS
- Python
Update a job trigger
This sample updates the infoTypes and minimum likelihood of a job trigger.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Java
- PHP
- JavaScript
- Node JS
- Go
- Python
- C#
Augment a built-in infotype detector
This sample demonstrates how to add terms to match an existing infoType detector.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Go
- Node JS
- JavaScript
- C#
- PHP
- Java
- Python
Create an inspection template
Use templates to create and persist configuration information for use with Cloud DLP. Templates are useful for decoupling configuration information—such as what you inspect for and how you de-identify it—from the implementation of your requests. Templates provide a way to re-use configuration and enable consistency across users and datasets. In addition, whenever you update a template, it's updated for any job trigger that uses it.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- PHP
- Java
- JavaScript
- Python
- Node JS
- Go
- C#
Inspect a string with an exclusion dictionary substring
Omit scan matches that include the substring "TEST".
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- PHP
- JavaScript
- Python
- Go
- C#
- Node JS
- Java
De-identify table data: Suppress a row based on the content of a column
Suppress a row based on the content of a column. You can remove a row entirely based on the content that appears in any column. This example suppresses the record for "Charles Dickens," as this patient is over 89 years old.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Python
- Java
- Go
- PHP
- JavaScript
- C#
- Node JS
Inspect an image for sensitive data with listed infoTypes
If you want to inspect an image for only certain sensitive data types, specify their corresponding built-in infoTypes.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- C#
- JavaScript
- Python
- Go
- Java
- Node JS
- PHP
Update a stored infoType
This sample demonstrates how to update the source term list of a stored infoType and rebuild the dictionary.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Java
- PHP
- Python
- Node JS
- Go
- C#
- JavaScript
Perform risk analysis
Use the Data Loss Prevention API to compute risk metrics of a column of categorical data in a BigQuery table.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- JavaScript
- Node JS
- Go
- Python
- C#
- PHP
- Java
De-identify free text with FPE by using a surrogate
Uses the Data Loss Prevention API to de-identify sensitive data in a string using format-preserving encryption (FPE). The encryption is performed with an unwrapped key.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- PHP
- Java
- C#
- Python
- Node JS
- JavaScript
- Go
Re-identify content encrypted by FPE
Demonstrates re-identifying de-identified content.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Node JS
- JavaScript
- Python
- Go
- Java
- C#
- PHP
Inspect a string, excluding REGEX matches
Omit email addresses ending with a specific domain from an EMAIL_ADDRESS detector scan.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Go
- Java
- Node JS
- C#
- JavaScript
- PHP
- Python
Set the match likelihood of a table column
Set the match likelihood of an entire column of data. This approach is helpful, for example, if you want to exclude a column of data from inspection results.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- C#
- Python
- Java
- JavaScript
- Node JS
- PHP
- Go
Redact sensitive data from an image using default infoTypes
Redact the default infoTypes from this image.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Python
- JavaScript
- Node JS
- Go
- Java
- PHP
- C#
List jobs
List all Cloud DLP jobs for the current project.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- PHP
- JavaScript
- Java
- C#
- Go
- Node JS
- Python
Create de-identified copies of Cloud Storage files
This sample demonstrates how to inspect a Cloud Storage resource and create de-identified copies of the files.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Python
- Node JS
- Go
- Java
- C#
- PHP
- JavaScript
Redact an image
Demonstrates redacting sensitive data from an image.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Java
- Python
- PHP
- Node JS
- JavaScript
- C#
- Go
Inspect data with a hotword rule
This sample uses a custom regex with a hotword rule to increase the likelihood of match.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Go
- C#
- Node JS
- Java
- JavaScript
- Python
- PHP
Character masking
Demonstrates masking characters.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- JavaScript
- Go
- Python
- Java
- C#
- PHP
- Node JS
List templates
List inspection or de-identification templates.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- C#
- Java
- Python
- Node JS
- JavaScript
- Go
- PHP
Inspect image for sensitive data with infoTypes
To inspect an image for sensitive data, you submit a base64-encoded image to the Cloud DLP API's content.inspect method. Unless you specify information types (infoTypes) to search for, Cloud DLP searches for the most common infoTypes.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- JavaScript
- Go
- C#
- PHP
- Java
- Python
- Node JS
Re-identify free text with FPE using a surrogate
Uses the Cloud Data Loss Prevention API to re-identify sensitive data in a string that was encrypted by format-preserving encryption (FPE) with a surrogate type. The encryption is performed with an unwrapped key.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- PHP
- Go
- C#
- JavaScript
- Node JS
- Java
- Python
Inspect a table for sensitive content
Check a table of data for sensitive content.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Java
- Python
- C#
- Node JS
- PHP
- Go
- JavaScript
De-identify table data using conditional logic and replace with infoTypes
Transform findings only when specific conditions are met on another field.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- JavaScript
- PHP
- Python
- Java
- C#
- Go
- Node JS
Inspect storage with sampling
The following examples demonstrate using the Cloud DLP API to scan a 90% subset of a Cloud Storage bucket for person names. The scan starts from a random location in the dataset and only includes text files under 200 bytes.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Python
- JavaScript
- Go
- Node JS
- PHP
- C#
- Java
De-identify table data using masking and conditional logic
Transform a column based on the value of another column.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Go
- PHP
- Python
- C#
- Java
- JavaScript
- Node JS
Re-identify text data with FPE
Re-identify text data with format-preserving encryption.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Node JS
- Python
- Go
- C#
- Java
- JavaScript
- PHP
De-identify sensitive data: Replacing matched input values
Uses the Data Loss Prevention API to de-identify sensitive data in a string by replacing matched input values with a value that you specify.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Python
- Java
- JavaScript
- C#
- PHP
- Go
- Node JS
Date shifting of a CSV file
Demonstrates date shifting of a CSV file.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- C#
- PHP
- JavaScript
- Go
- Node JS
- Python
- Java
Create a job trigger
Creates a scheduled Cloud Data Loss Prevention API job trigger.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Java
- Go
- PHP
- Node JS
- Python
- C#
- JavaScript
Create a stored infoType
This sample creates a stored infoType.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Go
- JavaScript
- C#
- Node JS
- Java
- PHP
- Python
De-identify sensitive data by replacing with infoType
Uses the Data Loss Prevention API to de-identify sensitive data in a string by replacing it with the infoType.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Go
- JavaScript
- PHP
- Node JS
- Java
- C#
- Python
Format-preserving encryption (FPE)
Demonstrates encrypting sensitive characters while maintaining format.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Python
- Node JS
- Go
- JavaScript
- PHP
- Java
- C#
Create a hybrid job trigger and inspect example data
This sample creates a hybrid job trigger and sends example data to it for inspection.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Java
- Node JS
- Go
- C#
- JavaScript
- PHP
- Python