|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dlp.snippets; |
| 18 | + |
| 19 | +// [START dlp_inspect_image_file] |
| 20 | +import com.google.cloud.dlp.v2.DlpServiceClient; |
| 21 | +import com.google.privacy.dlp.v2.ByteContentItem; |
| 22 | +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; |
| 23 | +import com.google.privacy.dlp.v2.ContentItem; |
| 24 | +import com.google.privacy.dlp.v2.Finding; |
| 25 | +import com.google.privacy.dlp.v2.InfoType; |
| 26 | +import com.google.privacy.dlp.v2.InspectConfig; |
| 27 | +import com.google.privacy.dlp.v2.InspectContentRequest; |
| 28 | +import com.google.privacy.dlp.v2.InspectContentResponse; |
| 29 | +import com.google.privacy.dlp.v2.ProjectName; |
| 30 | +import com.google.protobuf.ByteString; |
| 31 | +import java.io.FileInputStream; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public class InspectImageFile { |
| 36 | + |
| 37 | + // Inspects the specified image file. |
| 38 | + public static void inspectImageFile(String projectId, String filePath) { |
| 39 | + // String projectId = "my-project-id"; |
| 40 | + // String filePath = "path/to/image.png"; |
| 41 | + // String fileType = "IMAGE" |
| 42 | + |
| 43 | + // Initialize client with try-with-resources for automatic cleanup of background resources |
| 44 | + try (DlpServiceClient dlp = DlpServiceClient.create()) { |
| 45 | + // Set project for request |
| 46 | + ProjectName project = ProjectName.of(projectId); |
| 47 | + |
| 48 | + // Set content for request |
| 49 | + ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); |
| 50 | + ByteContentItem byteItem = ByteContentItem.newBuilder() |
| 51 | + .setType(BytesType.IMAGE) |
| 52 | + .setData(fileBytes) |
| 53 | + .build(); |
| 54 | + ContentItem item = ContentItem.newBuilder() |
| 55 | + .setByteItem(byteItem) |
| 56 | + .build(); |
| 57 | + |
| 58 | + // Set required InfoTypes for inspection config |
| 59 | + List<InfoType> infoTypes = new ArrayList<>(); |
| 60 | + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types |
| 61 | + for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { |
| 62 | + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); |
| 63 | + } |
| 64 | + |
| 65 | + // Set the inspect configuration for request |
| 66 | + InspectConfig config = InspectConfig.newBuilder() |
| 67 | + .addAllInfoTypes(infoTypes) |
| 68 | + .setIncludeQuote(true) |
| 69 | + .build(); |
| 70 | + |
| 71 | + // Construct the request to be sent by the client |
| 72 | + InspectContentRequest request = InspectContentRequest.newBuilder() |
| 73 | + .setParent(project.toString()) |
| 74 | + .setItem(item) |
| 75 | + .setInspectConfig(config) |
| 76 | + .build(); |
| 77 | + |
| 78 | + // Use the client to send the API request |
| 79 | + InspectContentResponse response = dlp.inspectContent(request); |
| 80 | + |
| 81 | + // Parse the response and process results |
| 82 | + System.out.println("Findings: " + response.getResult().getFindingsCount()); |
| 83 | + for (Finding f : response.getResult().getFindingsList()) { |
| 84 | + System.out.println("\tQuote: " + f.getQuote()); |
| 85 | + System.out.println("\tInfo type: " + f.getInfoType().getName()); |
| 86 | + System.out.println("\tLikelihood: " + f.getLikelihood()); |
| 87 | + } |
| 88 | + } catch (Exception e) { |
| 89 | + System.out.println("Error during inspectFile: \n" + e.toString()); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +// [END dlp_inspect_image_file] |
0 commit comments