|
| 1 | +# Copyright 2018 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START dlp_inspect_string] |
| 16 | +import os |
| 17 | + |
| 18 | +# Import the client library. |
| 19 | +import google.cloud.dlp |
| 20 | + |
| 21 | + |
| 22 | +def inspect_file(): |
| 23 | + # Instantiate a client. |
| 24 | + dlp = google.cloud.dlp.DlpServiceClient() |
| 25 | + |
| 26 | + inspect_config = { |
| 27 | + # The infoTypes of information to match |
| 28 | + 'info_types': [ |
| 29 | + {'name': 'PHONE_NUMBER'}, |
| 30 | + {'name': 'EMAIL_ADDRESS'}, |
| 31 | + {'name': 'CREDIT_CARD_NUMBER'}, |
| 32 | + ], |
| 33 | + # The minimum likelihood required before returning a match |
| 34 | + 'min_likelihood': 'LIKELIHOOD_UNSPECIFIED', |
| 35 | + # Whether to include the matching string |
| 36 | + 'include_quote': True, |
| 37 | + 'limits': { |
| 38 | + # The maximum number of findings to report per request |
| 39 | + # (0 = server maximum) |
| 40 | + 'max_findings_per_request': 0, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + # Construct the item, containing the file's byte data. |
| 45 | + # Before running this code, replace the filename with your filepath |
| 46 | + filename = os.path.join( |
| 47 | + os.path.dirname(__file__), 'resources', 'test.txt') |
| 48 | + with open(filename, mode='rb') as f: |
| 49 | + item = {'byte_item': {'type': 'TEXT_UTF8', 'data': f.read()}} |
| 50 | + |
| 51 | + # Convert the project id into a full resource id. |
| 52 | + # Before running this code, replace 'YOUR_PROJECT_ID' with your project ID |
| 53 | + # or set the GOOGLE_CLOUD_PROJECT environment variable to your project ID. |
| 54 | + project_id = os.getenv('GOOGLE_CLOUD_PROJECT') or 'YOUR_PROJECT_ID' |
| 55 | + parent = dlp.project_path(project_id) |
| 56 | + |
| 57 | + # Call the API. |
| 58 | + response = dlp.inspect_content(parent, inspect_config, item) |
| 59 | + |
| 60 | + # Print out the results. |
| 61 | + if response.result.findings: |
| 62 | + for finding in response.result.findings: |
| 63 | + try: |
| 64 | + if finding.quote: |
| 65 | + print('Quote: {}'.format(finding.quote)) |
| 66 | + except AttributeError: |
| 67 | + pass |
| 68 | + print('Info type: {}'.format(finding.info_type.name)) |
| 69 | + print('Likelihood: {}'.format(finding.likelihood)) |
| 70 | + else: |
| 71 | + print('No findings.') |
| 72 | +# [END dlp_inspect_string] |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + inspect_file() |
0 commit comments