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
- Sensitive Data Protection
- Google Cloud
- JavaScript
- Java
- Go
- PHP
- Python
- C#
- Node JS
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
- Java
- C#
- Python
- Go
- PHP
- 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.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- PHP
- Python
- C#
- Node JS
- Java
- Go
- JavaScript
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
- JavaScript
- PHP
- Java
- Python
- Node JS
- C#
- Go
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.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Java
- Go
- PHP
- C#
- Node JS
- Python
- JavaScript
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
- Go
- PHP
- Python
- Java
Client library quickstart
Demonstrates inspecting a string with the Cloud DLP API.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Node JS
- C#
- PHP
- Ruby
- JavaScript
- Java
- Python
- Go
Inspect a local file
Demonstrates finding sensitive data in a local text or image file.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- C#
- JavaScript
- Python
- Go
- Ruby
- Node JS
- Java
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
- Java
- Go
- Node JS
- PHP
- JavaScript
- Python
- C#
Get an inspection job
Get DLP inspection job.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- PHP
- Python
- Node JS
- C#
- JavaScript
- Go
- Java
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.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- PHP
- Go
- C#
- Node JS
- Java
- Python
- 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.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- C#
- Go
- Node JS
- Python
- PHP
- Java
- JavaScript
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.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- PHP
- Node JS
- Go
- Java
- Python
- C#
- JavaScript
Scan content using a large custom dictionary detector
This sample scans the given text using the specified stored infoType detector.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Java
- JavaScript
- Node JS
- C#
- Python
- Go
- PHP
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
- Google Cloud
- Cloud Data Loss Prevention
- Node JS
- Java
- Go
- PHP
- Python
- JavaScript
- C#
List triggers
List all job triggers for the current project.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Java
- Python
- Go
- C#
- JavaScript
- Node JS
- PHP
Inspect BigQuery for sensitive data
Demonstrates finding sensitive data that is stored in BigQuery.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- JavaScript
- Java
- C#
- Python
- PHP
- Node JS
- Go
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
- Java
- C#
- Go
- Node JS
- JavaScript
- Python
- PHP
Inspect a Cloud Storage file
Demonstrates finding sensitive data in a file that is located in Cloud Storage.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- PHP
- C#
- JavaScript
- Go
- Python
- Java
- Node JS
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.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- PHP
- JavaScript
- Go
- Node JS
- Java
- C#
- Python
Inspect a string
Demonstrates finding sensitive data in a string.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Python
- C#
- PHP
- Go
- Java
- Node JS
- Ruby
- JavaScript
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
- Python
- Node JS
- C#
- Java
- Go
- PHP
- JavaScript
Delete an inspection template
Delete an inspection template from Cloud DLP.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- C#
- Python
- Node JS
- Java
- PHP
- Go
- JavaScript
De-identify table data with format-preserving encryption
Demonstrates encrypting sensitive data in a table while maintaining format.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- C#
- PHP
- JavaScript
- Go
- Node JS
- Python
- Java
Redact all detected text in an image
Redact all detected text in an image.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Python
- PHP
- Node JS
- JavaScript
- C#
- Java
- Go
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.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Python
- Java
- C#
- JavaScript
- Node JS
- Go
- PHP
Re-identify table data with FPE
Re-identify table data with format-preserving encryption.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Go
- Java
- Node JS
- JavaScript
- C#
- PHP
- Python
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
- Google Cloud
- Sensitive Data Protection
- Node JS
- JavaScript
- Go
- C#
- PHP
- Python
- Java
Create an inspection job
Creates an inspection job with the Cloud Data Loss Prevention API.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Python
- Java
- C#
- JavaScript
- Go
- PHP
- Node JS
Re-identify content encrypted by deterministic encryption
Re-identify content that was previously de-identified through deterministic encryption.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- PHP
- Python
- JavaScript
- Java
- Go
- C#
- Node JS
Delete a trigger
Delete a DLP trigger.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- PHP
- Go
- Java
- JavaScript
- C#
- Python
- Node JS
Redact data from an image with color-coded infoTypes
Redacting infoTypes from an image with color coding.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Java
- JavaScript
- PHP
- Go
- C#
- Python
- Node JS
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.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- PHP
- Java
- Node JS
- C#
- Go
- JavaScript
- Python
Delete a job
Delete a DLP job.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Node JS
- C#
- JavaScript
- Go
- Python
- PHP
- Java
Inspect Datastore
Demonstrates finding sensitive data stored in Datastore.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Python
- C#
- Node JS
- JavaScript
- Java
- PHP
- Go
Create an exception list for de-identification
Create an exception list for a regular custom dictionary detector.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Node JS
- Go
- PHP
- Python
- Java
- JavaScript
- C#
Transform findings using a cryptographic hash transformation
This sample transforms tabular data through a cryptographic hash transformation.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Java
- JavaScript
- C#
- Go
- PHP
- Python
- Node JS
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
- PHP
- Node JS
- Go
- C#
- Java
- JavaScript
- Python
De-identify sensitive data with a simple word list
Matches against a custom simple word list to de-identify sensitive data.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Java
- Go
- Python
- C#
- Node JS
- JavaScript
- 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
- C#
- Python
- PHP
- Java
- Go
- JavaScript
- 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.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Java
- Node JS
- JavaScript
- C#
- Go
- PHP
- Python
Inspect an image file for sensitive data
Uses Cloud DLP to inspect an image for sensitive data.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Node JS
- Java
- PHP
- Python
- C#
- JavaScript
- Go
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
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Java
- C#
- Node JS
- Python
- Go
- JavaScript
- PHP
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
- Go
- PHP
- Java
- Python
- C#
- Node JS
- JavaScript
Augment a built-in infotype detector
This sample demonstrates how to add terms to match an existing infoType detector.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Node JS
- JavaScript
- Java
- C#
- Python
- Go
- PHP
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.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Java
- JavaScript
- Go
- Node JS
- PHP
- C#
- Python
Inspect a string with an exclusion dictionary substring
Omit scan matches that include the substring "TEST".
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- C#
- Python
- Go
- Node JS
- JavaScript
- PHP
- 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.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Go
- JavaScript
- PHP
- Python
- C#
- Node JS
- Java
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.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Java
- PHP
- Go
- JavaScript
- Python
- Node JS
- C#
Update a stored infoType
This sample demonstrates how to update the source term list of a stored infoType and rebuild the dictionary.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- PHP
- Node JS
- Java
- Go
- C#
- Python
- JavaScript
Perform risk analysis
Use the Data Loss Prevention API to compute risk metrics of a column of categorical data in a BigQuery table.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- Java
- C#
- PHP
- Python
- Go
- JavaScript
- Node JS
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.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Java
- Go
- Python
- Node JS
- JavaScript
- PHP
- C#
Re-identify content encrypted by FPE
Demonstrates re-identifying de-identified content.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Python
- JavaScript
- Node JS
- C#
- Go
- PHP
- Java
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
- Node JS
- C#
- JavaScript
- Python
- PHP
- Go
- Java
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.
- Google Cloud
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Python
- Node JS
- Go
- PHP
- JavaScript
- C#
- Java
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
- Java
- Node JS
- PHP
- JavaScript
- Go
- C#
- Python
List jobs
List all Cloud DLP jobs for the current project.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- C#
- Go
- Node JS
- Java
- Python
- PHP
- JavaScript
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
- Go
- PHP
- C#
- Node JS
- JavaScript
- Java
Redact an image
Demonstrates redacting sensitive data from an image.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Go
- JavaScript
- C#
- Java
- Node JS
- PHP
- Python
Inspect data with a hotword rule
This sample uses a custom regex with a hotword rule to increase the likelihood of match.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Go
- Node JS
- JavaScript
- C#
- Java
- Python
- PHP
Character masking
Demonstrates masking characters.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Python
- Java
- JavaScript
- Node JS
- C#
- Go
- PHP
List templates
List inspection or de-identification templates.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Node JS
- JavaScript
- Go
- C#
- Python
- Java
- 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.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Python
- C#
- PHP
- JavaScript
- Java
- Go
- 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
- Sensitive Data Protection
- Google Cloud
- Go
- PHP
- JavaScript
- Java
- C#
- Python
- Node JS
Inspect a table for sensitive content
Check a table of data for sensitive content.
- Sensitive Data Protection
- Google Cloud
- Cloud Data Loss Prevention
- PHP
- JavaScript
- Go
- Node JS
- Python
- Java
- C#
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
- Java
- Python
- C#
- Go
- PHP
- Node JS
- JavaScript
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.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Java
- Python
- PHP
- Go
- C#
- JavaScript
- Node JS
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
- Python
- Node JS
- JavaScript
- C#
- PHP
- Java
Re-identify text data with FPE
Re-identify text data with format-preserving encryption.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- Go
- Java
- PHP
- Python
- JavaScript
- C#
- Node JS
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.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- PHP
- C#
- JavaScript
- Java
- Node JS
- Go
- Python
Date shifting of a CSV file
Demonstrates date shifting of a CSV file.
- Sensitive Data Protection
- Cloud Data Loss Prevention
- Google Cloud
- C#
- Python
- PHP
- Java
- JavaScript
- Go
- Node JS
Create a job trigger
Creates a scheduled Cloud Data Loss Prevention API job trigger.
- Google Cloud
- Sensitive Data Protection
- Cloud Data Loss Prevention
- JavaScript
- PHP
- Node JS
- Python
- Go
- C#
- Java
Create a stored infoType
This sample creates a stored infoType.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Python
- Java
- Go
- Node JS
- C#
- PHP
- JavaScript
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.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Go
- Java
- PHP
- Node JS
- C#
- JavaScript
- Python
Format-preserving encryption (FPE)
Demonstrates encrypting sensitive characters while maintaining format.
- Cloud Data Loss Prevention
- Sensitive Data Protection
- Google Cloud
- Go
- Python
- Java
- PHP
- C#
- JavaScript
- Node JS
Create a hybrid job trigger and inspect example data
This sample creates a hybrid job trigger and sends example data to it for inspection.
- Cloud Data Loss Prevention
- Google Cloud
- Sensitive Data Protection
- Python
- C#
- Java
- JavaScript
- PHP
- Go
- Node JS