|
22 | 22 | import os
|
23 | 23 |
|
24 | 24 |
|
| 25 | +# [START dlp_inspect_string_basic] |
| 26 | +def inspect_string_basic( |
| 27 | + project, |
| 28 | + content_string, |
| 29 | + info_types=["PHONE_NUMBER"], |
| 30 | +): |
| 31 | + """Uses the Data Loss Prevention API to analyze strings for protected data. |
| 32 | + Args: |
| 33 | + project: The Google Cloud project id to use as a parent resource. |
| 34 | + content_string: The string to inspect. |
| 35 | + info_types: A list of strings representing info types to look for. |
| 36 | + A full list of info type categories can be fetched from the API. |
| 37 | + Returns: |
| 38 | + None; the response from the API is printed to the terminal. |
| 39 | + """ |
| 40 | + |
| 41 | + # Import the client library. |
| 42 | + import google.cloud.dlp |
| 43 | + |
| 44 | + # Instantiate a client. |
| 45 | + dlp = google.cloud.dlp_v2.DlpServiceClient() |
| 46 | + |
| 47 | + # Prepare info_types by converting the list of strings into a list of |
| 48 | + # dictionaries (protos are also accepted). |
| 49 | + info_types = [{"name": info_type} for info_type in info_types] |
| 50 | + |
| 51 | + # Construct the configuration dictionary. |
| 52 | + inspect_config = { |
| 53 | + "info_types": info_types, |
| 54 | + "include_quote": True, |
| 55 | + } |
| 56 | + |
| 57 | + # Construct the `item`. |
| 58 | + item = {"value": content_string} |
| 59 | + |
| 60 | + # Convert the project id into a full resource id. |
| 61 | + parent = dlp.project_path(project) |
| 62 | + |
| 63 | + # Call the API. |
| 64 | + response = dlp.inspect_content(parent, inspect_config, item) |
| 65 | + |
| 66 | + # Print out the results. |
| 67 | + if response.result.findings: |
| 68 | + for finding in response.result.findings: |
| 69 | + print("Quote: {}".format(finding.quote)) |
| 70 | + print("Info type: {}".format(finding.info_type.name)) |
| 71 | + print("Likelihood: {}".format(finding.likelihood)) |
| 72 | + else: |
| 73 | + print("No findings.") |
| 74 | + |
| 75 | + |
| 76 | +# [END dlp_inspect_string_basic] |
| 77 | + |
| 78 | + |
25 | 79 | # [START dlp_inspect_string]
|
26 | 80 | def inspect_string(
|
27 | 81 | project,
|
|
0 commit comments