@@ -277,6 +277,52 @@ def redact_image_listed_info_types(
277
277
# [END dlp_redact_image_listed_infotypes]
278
278
279
279
280
+ # [START dlp_redact_image_all_infotypes]
281
+ def redact_image_all_info_types (
282
+ project ,
283
+ filename ,
284
+ output_filename ,
285
+ ):
286
+ """Uses the Data Loss Prevention API to redact protected data in an image.
287
+ Args:
288
+ project: The Google Cloud project id to use as a parent resource.
289
+ filename: The path to the file to inspect.
290
+ output_filename: The path to which the redacted image will be written.
291
+ A full list of info type categories can be fetched from the API.
292
+ Returns:
293
+ None; the response from the API is printed to the terminal.
294
+ """
295
+
296
+ # Import the client library
297
+ import google .cloud .dlp
298
+
299
+ # Instantiate a client.
300
+ dlp = google .cloud .dlp_v2 .DlpServiceClient ()
301
+
302
+ # Construct the byte_item, containing the file's byte data.
303
+ with open (filename , mode = "rb" ) as f :
304
+ byte_item = {"type_" : google .cloud .dlp_v2 .FileType .IMAGE , "data" : f .read ()}
305
+
306
+ # Convert the project id into a full resource id.
307
+ parent = f"projects/{ project } "
308
+
309
+ # Call the API.
310
+ response = dlp .redact_image (
311
+ request = {
312
+ "parent" : parent ,
313
+ "byte_item" : byte_item ,
314
+ }
315
+ )
316
+
317
+ # Write out the results.
318
+ with open (output_filename , mode = "wb" ) as f :
319
+ f .write (response .redacted_image )
320
+ print (f"Wrote { len (response .redacted_image )} to { output_filename } " )
321
+
322
+
323
+ # [END dlp_redact_image_all_infotypes]
324
+
325
+
280
326
if __name__ == "__main__" :
281
327
default_project = os .environ .get ("GOOGLE_CLOUD_PROJECT" )
282
328
@@ -364,6 +410,12 @@ def redact_image_listed_info_types(
364
410
default = None ,
365
411
)
366
412
413
+ all_info_types_parser = subparsers .add_parser (
414
+ "all_info_types" ,
415
+ help = "Redact all infoTypes from an image." ,
416
+ parents = [common_args_parser ],
417
+ )
418
+
367
419
args = parser .parse_args ()
368
420
369
421
if args .content == "info_types" :
@@ -390,3 +442,9 @@ def redact_image_listed_info_types(
390
442
min_likelihood = args .min_likelihood ,
391
443
mime_type = args .mime_type ,
392
444
)
445
+ elif args .content == "all_info_types" :
446
+ redact_image_all_info_types (
447
+ args .project ,
448
+ args .filename ,
449
+ args .output_filename ,
450
+ )
0 commit comments