|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | +import com.google.cloud.dlp.v2.DlpServiceClient; |
| 20 | +import com.google.privacy.dlp.v2.ByteContentItem; |
| 21 | +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; |
| 22 | +import com.google.privacy.dlp.v2.InfoType; |
| 23 | +import com.google.privacy.dlp.v2.InspectConfig; |
| 24 | +import com.google.privacy.dlp.v2.Likelihood; |
| 25 | +import com.google.privacy.dlp.v2.ProjectName; |
| 26 | +import com.google.privacy.dlp.v2.RedactImageRequest; |
| 27 | +import com.google.privacy.dlp.v2.RedactImageResponse; |
| 28 | +import com.google.protobuf.ByteString; |
| 29 | +import java.io.FileInputStream; |
| 30 | +import java.io.FileOutputStream; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +class RedactImageFile { |
| 35 | + |
| 36 | + static void redactImageFile(String projectId, String filePath) { |
| 37 | + // String projectId = "my-project-id"; |
| 38 | + // String filePath = "path/to/image.png"; |
| 39 | + |
| 40 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 41 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 42 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 43 | + try (DlpServiceClient dlp = DlpServiceClient.create()) { |
| 44 | + // Specify the project used for request. |
| 45 | + ProjectName project = ProjectName.of(projectId); |
| 46 | + |
| 47 | + // Specify the content to be inspected. |
| 48 | + ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); |
| 49 | + ByteContentItem byteItem = |
| 50 | + ByteContentItem.newBuilder().setType(BytesType.IMAGE).setData(fileBytes).build(); |
| 51 | + |
| 52 | + // Specify the type of info and likelihood necessary to redact. |
| 53 | + List<InfoType> infoTypes = new ArrayList<>(); |
| 54 | + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types |
| 55 | + for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { |
| 56 | + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); |
| 57 | + } |
| 58 | + InspectConfig config = |
| 59 | + InspectConfig.newBuilder() |
| 60 | + .addAllInfoTypes(infoTypes) |
| 61 | + .setMinLikelihood(Likelihood.LIKELY) |
| 62 | + .build(); |
| 63 | + |
| 64 | + // Construct the Redact request to be sent by the client. |
| 65 | + RedactImageRequest request = |
| 66 | + RedactImageRequest.newBuilder() |
| 67 | + .setParent(project.toString()) |
| 68 | + .setByteItem(byteItem) |
| 69 | + .setInspectConfig(config) |
| 70 | + .build(); |
| 71 | + |
| 72 | + // Use the client to send the API request. |
| 73 | + RedactImageResponse response = dlp.redactImage(request); |
| 74 | + |
| 75 | + // Parse the response and process results. |
| 76 | + String outputPath = "redacted.png"; |
| 77 | + FileOutputStream redacted = new FileOutputStream(outputPath); |
| 78 | + redacted.write(response.getRedactedImage().toByteArray()); |
| 79 | + redacted.close(); |
| 80 | + System.out.println("Redacted image written to " + outputPath); |
| 81 | + |
| 82 | + } catch (Exception e) { |
| 83 | + System.out.println("Error during inspectFile: \n" + e.toString()); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments