This simple command-line application demonstrates how to invoke Google DLP API from PHP.
- Enable APIs - Enable the DLP API and create a new project or select an existing project.
- Download The Credentials - Click "Go to credentials" after enabling the APIs. Click
"New Credentials"
and select "Service Account Key". Create a new service account, use the JSON key type, and
select "Create". Once downloaded, set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON key that was downloaded. - Clone the repo and cd into this directory
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
$ cd php-docs-samples/dlp
- Install dependencies via Composer.
Run
php composer.phar install
(if composer is installed locally) orcomposer install
(if composer is installed globally). - Execute the snippets in the src/ directory by running
php src/SNIPPET_NAME.php
. The usage will print for each if no arguments are provided:$ php src/inspect_string.php Usage: php src/inspect_string.php PROJECT_ID STRING $ php src/inspect_string.php your-project-id 'bob@example.com' Findings: Quote: bob@example.com Info type: EMAIL_ADDRESS Likelihood: LIKELY
See the DLP Documentation for more information.
- Ensure that
GOOGLE_APPLICATION_CREDENTIALS
points to authorized service account credentials file. - Create a Google Cloud Project and set the
GOOGLE_PROJECT_ID
environment variable.export GOOGLE_PROJECT_ID=YOUR_PROJECT_ID
- Create a Google Cloud Storage bucket and upload test.txt.
- Set the
GOOGLE_STORAGE_BUCKET
environment variable. - Set the
GCS_PATH
environment variable to point to the path for the bucket file.
export GOOGLE_STORAGE_BUCKET=YOUR_BUCKET export GCS_PATH=gs://GOOGLE_STORAGE_BUCKET/test.txt
- Set the
- Set the
DLP_DEID_WRAPPED_KEY
environment variable to an AES-256 key encrypted ('wrapped') with a Cloud Key Management Service (KMS) key. - Set the
DLP_DEID_KEY_NAME
environment variable to the path-name of the Cloud KMS key you wrappedDLP_DEID_WRAPPED_KEY
with.export DLP_DEID_WRAPPED_KEY=YOUR_ENCRYPTED_AES_256_KEY export DLP_DEID_KEY_NAME=projects/GOOGLE_PROJECT_ID/locations/YOUR_LOCATION/keyRings/YOUR_KEYRING_NAME/cryptoKeys/YOUR_KEY_NAME
- Create a De-identify templates
- Create default de-identify template for unstructured file.
- Create a de-identify template for structured files.
- Create image redaction template for images.
export DLP_DEIDENTIFY_TEMPLATE=YOUR_DEFAULT_DEIDENTIFY_TEMPLATE export DLP_STRUCTURED_DEIDENTIFY_TEMPLATE=YOUR_STRUCTURED_DEIDENTIFY_TEMPLATE export DLP_IMAGE_REDACT_DEIDENTIFY_TEMPLATE=YOUR_IMAGE_REDACT_TEMPLATE
- Copy and paste the data below into a CSV file and create a BigQuery table from the file:
Set the
Name,TelephoneNumber,Mystery,Age,Gender James,(567) 890-1234,8291 3627 8250 1234,19,Male Gandalf,(223) 456-7890,4231 5555 6781 9876,27,Male Dumbledore,(313) 337-1337,6291 8765 1095 7629,27,Male Joe,(452) 223-1234,3782 2288 1166 3030,35,Male Marie,(452) 223-1234,8291 3627 8250 1234,35,Female Carrie,(567) 890-1234,2253 5218 4251 4526,35,Female
DLP_DATASET_ID
andDLP_TABLE_ID
environment values.export DLP_DATASET_ID=YOUR_BIGQUERY_DATASET_ID export DLP_TABLE_ID=YOUR_TABLE_ID
- Create a Google Cloud Datastore kind and add an entity with properties:
Provide namespace and kind values.
Email : john@doe.com Person Name : John Phone Number : 343-343-3435 Email : gary@doe.com Person Name : Gary Phone Number : 343-443-3136
- Set the environment variables
DLP_NAMESPACE_ID
andDLP_DATASTORE_KIND
with the values provided in above step.
export DLP_NAMESPACE_ID=YOUR_NAMESPACE_ID export DLP_DATASTORE_KIND=YOUR_DATASTORE_KIND
- Set the environment variables
If you see an error like this:
PHP Fatal error: Uncaught Error: Call to undefined function Google\Protobuf\Internal\bccomp() in /usr/local/google/home/crwilson/github/GoogleCloudPlatform/php-docs-samples/dlp/vendor/google/protobuf/src/Google/Protobuf/Internal/Message.php:986
You may need to install the bcmath PHP extension. e.g. (may depend on your php version)
$ sudo apt-get install php8.1-bcmath
- See CONTRIBUTING.md
- See LICENSE