Skip to content

Commit 77ed524

Browse files
committed
Updated Redact sample to new format.
1 parent b0aee81 commit 77ed524

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
import static junit.framework.TestCase.assertNotNull;
20+
import static org.junit.Assert.assertThat;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import org.hamcrest.CoreMatchers;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
@RunWith(JUnit4.class)
33+
public class RedactTests {
34+
35+
private ByteArrayOutputStream bout;
36+
37+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
38+
39+
private static void requireEnvVar(String varName) {
40+
assertNotNull(
41+
System.getenv(varName),
42+
"Environment variable '%s' is required to perform these tests.".format(varName)
43+
);
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
}
51+
52+
@Before
53+
public void beforeTest() {
54+
bout = new ByteArrayOutputStream();
55+
System.setOut(new PrintStream(bout));
56+
}
57+
58+
@After
59+
public void tearDown() {
60+
System.setOut(null);
61+
bout.reset();
62+
}
63+
64+
@Test
65+
public void testInspectString() {
66+
InspectString.inspectString(PROJECT_ID, "I'm Gary and my email is [email protected]");
67+
68+
String output = bout.toString();
69+
assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
70+
}
71+
72+
@Test
73+
public void textInspectTestFile() {
74+
InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");
75+
76+
String output = bout.toString();
77+
assertThat(output, CoreMatchers.containsString("Info type: PHONE_NUMBER"));
78+
assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
79+
}
80+
81+
82+
@Test
83+
public void testInspectImageFile() {
84+
InspectImageFile.inspectImageFile(PROJECT_ID, "src/test/resources/test.png");
85+
86+
String output = bout.toString();
87+
assertThat(output, CoreMatchers.containsString("Info type: PHONE_NUMBER"));
88+
assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
89+
}
90+
91+
}

0 commit comments

Comments
 (0)