|
| 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 | +import sys |
| 15 | + |
| 16 | +def inspect_image_file( |
| 17 | + project_id='YOUR_PROJECT_ID', |
| 18 | + file_path='path/to/image.png'): |
| 19 | + # [START dlp_inspect_image_file] |
| 20 | + # Import the Google Cloud Data Loss Prevention library |
| 21 | + import google.cloud.dlp |
| 22 | + |
| 23 | + # TODO(developer): Uncomment these variables before running the sample. |
| 24 | + # project_id = 'YOUR_PROJECT_ID' |
| 25 | + # file_path = 'path/to/image.png' |
| 26 | + |
| 27 | + # Instantiate a client |
| 28 | + dlp = google.cloud.dlp.DlpServiceClient() |
| 29 | + |
| 30 | + # Get the bytes of the file |
| 31 | + with open(file_path, mode='rb') as fh: |
| 32 | + file_bytes = fh.read() |
| 33 | + |
| 34 | + # Construct request |
| 35 | + parent = dlp.project_path(project_id) |
| 36 | + item = {'byte_item': {'type': 'IMAGE', 'data': file_bytes}} |
| 37 | + inspect_config = { |
| 38 | + # The infoTypes of information to match |
| 39 | + 'info_types': [ |
| 40 | + {'name': 'PHONE_NUMBER'}, |
| 41 | + {'name': 'EMAIL_ADDRESS'}, |
| 42 | + {'name': 'CREDIT_CARD_NUMBER'}, |
| 43 | + ], |
| 44 | + # Whether to include the matching string |
| 45 | + 'include_quote': True, |
| 46 | + } |
| 47 | + |
| 48 | + # Run request |
| 49 | + response = dlp.inspect_content(parent, inspect_config, item) |
| 50 | + |
| 51 | + # Print the results |
| 52 | + if response.result.findings: |
| 53 | + for finding in response.result.findings: |
| 54 | + print('Quote: {}'.format(finding.quote)) |
| 55 | + print('Info type: {}'.format(finding.info_type.name)) |
| 56 | + print('Likelihood: {}'.format(finding.likelihood)) |
| 57 | + else: |
| 58 | + print('No findings.') |
| 59 | + # [END dlp_inspect_image_file] |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + inspect_image_file(project_id=sys.argv[1], file_path=sys.argv[2]) |
0 commit comments