@@ -930,6 +930,59 @@ def callback(message):
930
930
# [END dlp_inspect_bigquery]
931
931
932
932
933
+ # [START dlp_inspect_image_all_infotypes]
934
+ def inspect_image_file_all_infotypes (
935
+ project ,
936
+ filename ,
937
+ include_quote = True ,
938
+ ):
939
+ """Uses the Data Loss Prevention API to analyze strings for protected data in image file.
940
+ Args:
941
+ project: The Google Cloud project id to use as a parent resource.
942
+ filename: The path to the file to inspect.
943
+ include_quote: Boolean for whether to display a quote of the detected
944
+ information in the results.
945
+
946
+ Returns:
947
+ None; the response from the API is printed to the terminal.
948
+ """
949
+
950
+ # Import the client library
951
+ import google .cloud .dlp
952
+
953
+ # Instantiate a client.
954
+ dlp = google .cloud .dlp_v2 .DlpServiceClient ()
955
+
956
+ # Construct the byte_item, containing the image file's byte data.
957
+ with open (filename , mode = "rb" ) as f :
958
+ byte_item = {"type_" : 'IMAGE' , "data" : f .read ()}
959
+
960
+ # Convert the project id into a full resource id.
961
+ parent = f"projects/{ project } "
962
+
963
+ # Call the API.
964
+ response = dlp .inspect_content (
965
+ request = {
966
+ "parent" : parent ,
967
+ "inspect_config" : {"include_quote" : include_quote },
968
+ "item" : {"byte_item" : byte_item },
969
+ }
970
+ )
971
+
972
+ # Print out the results.
973
+ print ("Findings: " , response .result .findings .count )
974
+ if response .result .findings :
975
+ for finding in response .result .findings :
976
+ print ("Quote: {}" .format (finding .quote ))
977
+ print ("Info type: {}" .format (finding .info_type .name ))
978
+ print ("Likelihood: {}" .format (finding .likelihood ))
979
+ else :
980
+ print ("No findings." )
981
+
982
+
983
+ # [END dlp_inspect_image_all_infotypes]
984
+
985
+
933
986
if __name__ == "__main__" :
934
987
default_project = os .environ .get ("GOOGLE_CLOUD_PROJECT" )
935
988
@@ -1356,6 +1409,26 @@ def callback(message):
1356
1409
default = 300 ,
1357
1410
)
1358
1411
1412
+ parser_image_file = subparsers .add_parser (
1413
+ "image_all_infotypes" ,
1414
+ help = "Inspect a local file with all info types."
1415
+ )
1416
+ parser_image_file .add_argument (
1417
+ "--project" ,
1418
+ help = "The Google Cloud project id to use as a parent resource." ,
1419
+ default = default_project ,
1420
+ )
1421
+ parser_image_file .add_argument (
1422
+ "filename" ,
1423
+ help = "The path to the file to inspect."
1424
+ )
1425
+ parser_image_file .add_argument (
1426
+ "--include_quote" ,
1427
+ help = "A Boolean for whether to display a quote of the detected"
1428
+ "information in the results." ,
1429
+ default = True ,
1430
+ )
1431
+
1359
1432
args = parser .parse_args ()
1360
1433
1361
1434
if args .content == "string" :
@@ -1436,3 +1509,10 @@ def callback(message):
1436
1509
max_findings = args .max_findings ,
1437
1510
timeout = args .timeout ,
1438
1511
)
1512
+
1513
+ elif args .content == "image_all_infotypes" :
1514
+ inspect_image_file_all_infotypes (
1515
+ args .project ,
1516
+ args .filename ,
1517
+ include_quote = args .include_quote ,
1518
+ )
0 commit comments