|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2023 Google Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * For instructions on how to run the samples: |
| 21 | + * |
| 22 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Google\Cloud\Samples\Dlp; |
| 26 | + |
| 27 | +# [START dlp_create_job] |
| 28 | +use Google\Cloud\Dlp\V2\CloudStorageOptions; |
| 29 | +use Google\Cloud\Dlp\V2\CloudStorageOptions\FileSet; |
| 30 | +use Google\Cloud\Dlp\V2\DlpServiceClient; |
| 31 | +use Google\Cloud\Dlp\V2\InfoType; |
| 32 | +use Google\Cloud\Dlp\V2\InspectConfig; |
| 33 | +use Google\Cloud\Dlp\V2\InspectConfig\FindingLimits; |
| 34 | +use Google\Cloud\Dlp\V2\StorageConfig; |
| 35 | +use Google\Cloud\Dlp\V2\Likelihood; |
| 36 | +use Google\Cloud\Dlp\V2\Action; |
| 37 | +use Google\Cloud\Dlp\V2\Action\PublishSummaryToCscc; |
| 38 | +use Google\Cloud\Dlp\V2\InspectJobConfig; |
| 39 | +use Google\Cloud\Dlp\V2\StorageConfig\TimespanConfig; |
| 40 | + |
| 41 | +/** |
| 42 | + * Creates an inspection job with the Cloud Data Loss Prevention API. |
| 43 | + * |
| 44 | + * @param string $callingProjectId The project ID to run the API call under. |
| 45 | + * @param string $gcsPath GCS file to be inspected. Example : gs://GOOGLE_STORAGE_BUCKET_NAME/dlp_sample.csv |
| 46 | + */ |
| 47 | +function create_job( |
| 48 | + string $callingProjectId, |
| 49 | + string $gcsPath |
| 50 | +): void { |
| 51 | + // Instantiate a client. |
| 52 | + $dlp = new DlpServiceClient(); |
| 53 | + |
| 54 | + // Set autoPopulateTimespan to true to scan only new content. |
| 55 | + $timespanConfig = (new TimespanConfig()) |
| 56 | + ->setEnableAutoPopulationOfTimespanConfig(true); |
| 57 | + |
| 58 | + // Specify the GCS file to be inspected. |
| 59 | + $cloudStorageOptions = (new CloudStorageOptions()) |
| 60 | + ->setFileSet((new FileSet()) |
| 61 | + ->setUrl($gcsPath)); |
| 62 | + $storageConfig = (new StorageConfig()) |
| 63 | + ->setCloudStorageOptions(($cloudStorageOptions)) |
| 64 | + ->setTimespanConfig($timespanConfig); |
| 65 | + |
| 66 | + // ----- Construct inspection config ----- |
| 67 | + $emailAddressInfoType = (new InfoType()) |
| 68 | + ->setName('EMAIL_ADDRESS'); |
| 69 | + $personNameInfoType = (new InfoType()) |
| 70 | + ->setName('PERSON_NAME'); |
| 71 | + $locationInfoType = (new InfoType()) |
| 72 | + ->setName('LOCATION'); |
| 73 | + $phoneNumberInfoType = (new InfoType()) |
| 74 | + ->setName('PHONE_NUMBER'); |
| 75 | + $infoTypes = [$emailAddressInfoType, $personNameInfoType, $locationInfoType, $phoneNumberInfoType]; |
| 76 | + |
| 77 | + // Whether to include the matching string in the response. |
| 78 | + $includeQuote = true; |
| 79 | + // The minimum likelihood required before returning a match. |
| 80 | + $minLikelihood = likelihood::LIKELIHOOD_UNSPECIFIED; |
| 81 | + |
| 82 | + // The maximum number of findings to report (0 = server maximum). |
| 83 | + $limits = (new FindingLimits()) |
| 84 | + ->setMaxFindingsPerRequest(100); |
| 85 | + |
| 86 | + // Create the Inspect configuration object. |
| 87 | + $inspectConfig = (new InspectConfig()) |
| 88 | + ->setMinLikelihood($minLikelihood) |
| 89 | + ->setLimits($limits) |
| 90 | + ->setInfoTypes($infoTypes) |
| 91 | + ->setIncludeQuote($includeQuote); |
| 92 | + |
| 93 | + // Specify the action that is triggered when the job completes. |
| 94 | + $action = (new Action()) |
| 95 | + ->setPublishSummaryToCscc(new PublishSummaryToCscc()); |
| 96 | + |
| 97 | + // Configure the inspection job we want the service to perform. |
| 98 | + $inspectJobConfig = (new InspectJobConfig()) |
| 99 | + ->setInspectConfig($inspectConfig) |
| 100 | + ->setStorageConfig($storageConfig) |
| 101 | + ->setActions([$action]); |
| 102 | + |
| 103 | + // Send the job creation request and process the response. |
| 104 | + $parent = "projects/$callingProjectId/locations/global"; |
| 105 | + $job = $dlp->createDlpJob($parent, [ |
| 106 | + 'inspectJob' => $inspectJobConfig |
| 107 | + ]); |
| 108 | + |
| 109 | + // Print results. |
| 110 | + printf($job->getName()); |
| 111 | +} |
| 112 | +# [END dlp_create_job] |
| 113 | +// The following 2 lines are only needed to run the samples. |
| 114 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 115 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments