@@ -83,6 +83,64 @@ def deidentify_with_mask(
83
83
84
84
# [END dlp_deidentify_masking]
85
85
86
+ # [START dlp_deidentify_redact]
87
+ def deidentify_with_redact (
88
+ project ,
89
+ input_str ,
90
+ info_types ,
91
+ ):
92
+ """Uses the Data Loss Prevention API to deidentify sensitive data in a
93
+ string by redacting matched input values.
94
+ Args:
95
+ project: The Google Cloud project id to use as a parent resource.
96
+ input_str: The string to deidentify (will be treated as text).
97
+ info_types: A list of strings representing info types to look for.
98
+ Returns:
99
+ None; the response from the API is printed to the terminal.
100
+ """
101
+ import google .cloud .dlp
102
+
103
+ # Instantiate a client
104
+ dlp = google .cloud .dlp_v2 .DlpServiceClient ()
105
+
106
+ # Convert the project id into a full resource id.
107
+ parent = dlp .project_path (project )
108
+
109
+ # Construct inspect configuration dictionary
110
+ inspect_config = {
111
+ "info_types" : [{"name" : info_type } for info_type in info_types ]
112
+ }
113
+
114
+ # Construct deidentify configuration dictionary
115
+ deidentify_config = {
116
+ "info_type_transformations" : {
117
+ "transformations" : [
118
+ {
119
+ "primitive_transformation" : {
120
+ "redact_config" : {}
121
+ }
122
+ }
123
+ ]
124
+ }
125
+ }
126
+
127
+ # Construct item
128
+ item = {"value" : input_str }
129
+
130
+ # Call the API
131
+ response = dlp .deidentify_content (
132
+ parent ,
133
+ inspect_config = inspect_config ,
134
+ deidentify_config = deidentify_config ,
135
+ item = item ,
136
+ )
137
+
138
+ # Print out the results.
139
+ print (response .item .value )
140
+
141
+
142
+ # [END dlp_deidentify_redact]
143
+
86
144
# [START dlp_deidentify_replace]
87
145
def deidentify_with_replace (
88
146
project ,
0 commit comments