|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2023 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * For instructions on how to run the samples: |
| 20 | + * |
| 21 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Dlp; |
| 25 | + |
| 26 | +# [START dlp_deidentify_cloud_storage] |
| 27 | +use Google\Cloud\Dlp\V2\CloudStorageOptions; |
| 28 | +use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet; |
| 29 | +use Google\Cloud\Dlp\V2\DlpServiceClient; |
| 30 | +use Google\Cloud\Dlp\V2\InfoType; |
| 31 | +use Google\Cloud\Dlp\V2\InspectConfig; |
| 32 | +use Google\Cloud\Dlp\V2\StorageConfig; |
| 33 | +use Google\Cloud\Dlp\V2\Action; |
| 34 | +use Google\Cloud\Dlp\V2\Action\Deidentify; |
| 35 | +use Google\Cloud\Dlp\V2\BigQueryTable; |
| 36 | +use Google\Cloud\Dlp\V2\FileType; |
| 37 | +use Google\Cloud\Dlp\V2\InspectJobConfig; |
| 38 | +use Google\Cloud\Dlp\V2\TransformationConfig; |
| 39 | +use Google\Cloud\Dlp\V2\TransformationDetailsStorageConfig; |
| 40 | +use Google\Cloud\Dlp\V2\Client\BaseClient\DlpServiceBaseClient; |
| 41 | +use Google\Cloud\Dlp\V2\DlpJob\JobState; |
| 42 | + |
| 43 | +/** |
| 44 | + * De-identify sensitive data stored in Cloud Storage using the API. |
| 45 | + * Create an inspection job that has a de-identification action. |
| 46 | + * |
| 47 | + * @param string $callingProjectId The project ID to run the API call under. |
| 48 | + * @param string $inputgcsPath The Cloud Storage directory that you want to de-identify. |
| 49 | + * @param string $outgcsPath The Cloud Storage directory where you want to store the |
| 50 | + * de-identified files. |
| 51 | + * @param string $deidentifyTemplateName The full resource name of the default de-identify template — for |
| 52 | + * unstructured and structured files — if you created one. This value |
| 53 | + * must be in the format |
| 54 | + * `projects/projectName/(locations/locationId)/deidentifyTemplates/templateName`. |
| 55 | + * @param string $structuredDeidentifyTemplateName The full resource name of the de-identify template for structured |
| 56 | + * files if you created one. This value must be in the format |
| 57 | + * `projects/projectName/(locations/locationId)/deidentifyTemplates/templateName`. |
| 58 | + * @param string $imageRedactTemplateName The full resource name of the image redaction template for images if |
| 59 | + * you created one. This value must be in the format |
| 60 | + * `projects/projectName/(locations/locationId)/deidentifyTemplates/templateName`. |
| 61 | + * @param string $datasetId The ID of the BigQuery dataset where you want to store |
| 62 | + * the transformation details. If you don't provide a table ID, the |
| 63 | + * system automatically creates one. |
| 64 | + * @param string $tableId The ID of the BigQuery table where you want to store the |
| 65 | + * transformation details. |
| 66 | + */ |
| 67 | +function deidentify_cloud_storage( |
| 68 | + // TODO(developer): Replace sample parameters before running the code. |
| 69 | + string $callingProjectId, |
| 70 | + string $inputgcsPath = 'gs://YOUR_GOOGLE_STORAGE_BUCKET', |
| 71 | + string $outgcsPath = 'gs://YOUR_GOOGLE_STORAGE_BUCKET', |
| 72 | + string $deidentifyTemplateName = 'YOUR_DEIDENTIFY_TEMPLATE_NAME', |
| 73 | + string $structuredDeidentifyTemplateName = 'YOUR_STRUCTURED_DEIDENTIFY_TEMPLATE_NAME', |
| 74 | + string $imageRedactTemplateName = 'YOUR_IMAGE_REDACT_DEIDENTIFY_TEMPLATE_NAME', |
| 75 | + string $datasetId = 'YOUR_DATASET_ID', |
| 76 | + string $tableId = 'YOUR_TABLE_ID' |
| 77 | +): void { |
| 78 | + // Instantiate a client. |
| 79 | + $dlp = new DlpServiceClient(); |
| 80 | + |
| 81 | + $parent = "projects/$callingProjectId/locations/global"; |
| 82 | + |
| 83 | + // Specify the GCS Path to be de-identify. |
| 84 | + $cloudStorageOptions = (new CloudStorageOptions()) |
| 85 | + ->setFileSet((new FileSet()) |
| 86 | + ->setUrl($inputgcsPath)); |
| 87 | + $storageConfig = (new StorageConfig()) |
| 88 | + ->setCloudStorageOptions(($cloudStorageOptions)); |
| 89 | + |
| 90 | + // Specify the type of info the inspection will look for. |
| 91 | + $inspectConfig = (new InspectConfig()) |
| 92 | + ->setInfoTypes([ |
| 93 | + (new InfoType())->setName('PERSON_NAME'), |
| 94 | + (new InfoType())->setName('EMAIL_ADDRESS') |
| 95 | + ]); |
| 96 | + |
| 97 | + // Specify the big query table to store the transformation details. |
| 98 | + $transformationDetailsStorageConfig = (new TransformationDetailsStorageConfig()) |
| 99 | + ->setTable((new BigQueryTable()) |
| 100 | + ->setProjectId($callingProjectId) |
| 101 | + ->setDatasetId($datasetId) |
| 102 | + ->setTableId($tableId)); |
| 103 | + |
| 104 | + // Specify the de-identify template used for the transformation. |
| 105 | + $transformationConfig = (new TransformationConfig()) |
| 106 | + ->setDeidentifyTemplate( |
| 107 | + DlpServiceBaseClient::projectDeidentifyTemplateName($callingProjectId, $deidentifyTemplateName) |
| 108 | + ) |
| 109 | + ->setStructuredDeidentifyTemplate( |
| 110 | + DlpServiceBaseClient::projectDeidentifyTemplateName($callingProjectId, $structuredDeidentifyTemplateName) |
| 111 | + ) |
| 112 | + ->setImageRedactTemplate( |
| 113 | + DlpServiceBaseClient::projectDeidentifyTemplateName($callingProjectId, $imageRedactTemplateName) |
| 114 | + ); |
| 115 | + |
| 116 | + $deidentify = (new Deidentify()) |
| 117 | + ->setCloudStorageOutput($outgcsPath) |
| 118 | + ->setTransformationConfig($transformationConfig) |
| 119 | + ->setTransformationDetailsStorageConfig($transformationDetailsStorageConfig) |
| 120 | + ->setFileTypesToTransform([FileType::TEXT_FILE, FileType::IMAGE, FileType::CSV]); |
| 121 | + |
| 122 | + $action = (new Action()) |
| 123 | + ->setDeidentify($deidentify); |
| 124 | + |
| 125 | + // Configure the inspection job we want the service to perform. |
| 126 | + $inspectJobConfig = (new InspectJobConfig()) |
| 127 | + ->setInspectConfig($inspectConfig) |
| 128 | + ->setStorageConfig($storageConfig) |
| 129 | + ->setActions([$action]); |
| 130 | + |
| 131 | + // Send the job creation request and process the response. |
| 132 | + $job = $dlp->createDlpJob($parent, [ |
| 133 | + 'inspectJob' => $inspectJobConfig |
| 134 | + ]); |
| 135 | + |
| 136 | + $numOfAttempts = 10; |
| 137 | + do { |
| 138 | + printf('Waiting for job to complete' . PHP_EOL); |
| 139 | + sleep(30); |
| 140 | + $job = $dlp->getDlpJob($job->getName()); |
| 141 | + if ($job->getState() == JobState::DONE) { |
| 142 | + break; |
| 143 | + } |
| 144 | + $numOfAttempts--; |
| 145 | + } while ($numOfAttempts > 0); |
| 146 | + |
| 147 | + // Print finding counts. |
| 148 | + printf('Job %s status: %s' . PHP_EOL, $job->getName(), JobState::name($job->getState())); |
| 149 | + switch ($job->getState()) { |
| 150 | + case JobState::DONE: |
| 151 | + $infoTypeStats = $job->getInspectDetails()->getResult()->getInfoTypeStats(); |
| 152 | + if (count($infoTypeStats) === 0) { |
| 153 | + printf('No findings.' . PHP_EOL); |
| 154 | + } else { |
| 155 | + foreach ($infoTypeStats as $infoTypeStat) { |
| 156 | + printf( |
| 157 | + ' Found %s instance(s) of infoType %s' . PHP_EOL, |
| 158 | + $infoTypeStat->getCount(), |
| 159 | + $infoTypeStat->getInfoType()->getName() |
| 160 | + ); |
| 161 | + } |
| 162 | + } |
| 163 | + break; |
| 164 | + case JobState::FAILED: |
| 165 | + printf('Job %s had errors:' . PHP_EOL, $job->getName()); |
| 166 | + $errors = $job->getErrors(); |
| 167 | + foreach ($errors as $error) { |
| 168 | + var_dump($error->getDetails()); |
| 169 | + } |
| 170 | + break; |
| 171 | + case JobState::PENDING: |
| 172 | + printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL); |
| 173 | + break; |
| 174 | + default: |
| 175 | + printf('Unexpected job state. Most likely, the job is either running or has not yet started.'); |
| 176 | + } |
| 177 | +} |
| 178 | +# [END dlp_deidentify_cloud_storage] |
| 179 | +// The following 2 lines are only needed to run the samples. |
| 180 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 181 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments