Skip to content

Commit 2b7892e

Browse files
committed
Add java docs sample for DLP to convert saved findings in GCS to more usable proto format.
1 parent 5d5f26f commit 2b7892e

File tree

4 files changed

+182
-1
lines changed

4 files changed

+182
-1
lines changed

dlp/snippets/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<groupId>com.google.cloud</groupId>
3333
<scope>import</scope>
3434
<type>pom</type>
35-
<version>26.32.0</version>
35+
<version>26.64.0</version>
3636
</dependency>
3737
</dependencies>
3838
</dependencyManagement>
@@ -48,6 +48,10 @@
4848
<groupId>com.google.cloud</groupId>
4949
<artifactId>google-cloud-pubsub</artifactId>
5050
</dependency>
51+
<dependency>
52+
<groupId>com.google.protobuf</groupId>
53+
<artifactId>protobuf-java</artifactId>
54+
</dependency>
5155
<dependency>
5256
<groupId>junit</groupId>
5357
<artifactId>junit</artifactId>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 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_process_inspect_findings_saved_to_gcs]
20+
21+
import com.google.privacy.dlp.v2.Finding;
22+
import com.google.privacy.dlp.v2.SaveToGcsFindingsOutput;
23+
import com.google.protobuf.ByteString;
24+
import com.google.protobuf.TextFormat;
25+
import java.io.FileInputStream;
26+
import java.io.IOException;
27+
import java.io.InputStreamReader;
28+
import java.io.Reader;
29+
import java.nio.charset.StandardCharsets;
30+
31+
public class ProcessInspectFindingsSavedToGcs {
32+
33+
public static void main(String[] args) throws Exception {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String inputPath = "src/test/resources/save_to_gcs_findings.txt";
36+
processFindingsGcsFile(inputPath);
37+
}
38+
39+
// Processes a file containing findings from a DLP inspect job.
40+
public static void processFindingsGcsFile(String inputPath)
41+
throws IOException {
42+
SaveToGcsFindingsOutput.Builder builder = SaveToGcsFindingsOutput.newBuilder();
43+
try (Reader reader =
44+
new InputStreamReader(new FileInputStream(inputPath), StandardCharsets.UTF_8)) {
45+
TextFormat.merge(reader, builder);
46+
}
47+
SaveToGcsFindingsOutput output = builder.build();
48+
49+
// Parse the converted proto and process results
50+
System.out.println("Findings: " + output.getFindingsCount());
51+
for (Finding f : output.getFindingsList()) {
52+
System.out.println("\tInfo type: " + f.getInfoType().getName());
53+
System.out.println("\tLikelihood: " + f.getLikelihood());
54+
}
55+
}
56+
}
57+
// [END dlp_process_inspect_findings_saved_to_gcs]

dlp/snippets/src/test/java/dlp/snippets/InspectTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,4 +624,14 @@ public void testInspectWithStoredInfotype() throws Exception {
624624
assertThat(output).contains("InfoType: STORED_TYPE");
625625
}
626626
}
627+
628+
@Test
629+
public void testProcessInspectFindingsSavedToGcs() throws Exception {
630+
ProcessInspectFindingsSavedToGcs.processFindingsGcsFile(
631+
"src/test/resources/save_to_gcs_findings.txt");
632+
String output = bout.toString();
633+
assertThat(output).contains("Findings: 2");
634+
assertThat(output).contains("Info type: PERSON_NAME");
635+
assertThat(output).contains("Likelihood: LIKELY");
636+
}
627637
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
findings {
16+
info_type {
17+
name: "PERSON_NAME"
18+
sensitivity_score {
19+
score: SENSITIVITY_MODERATE
20+
}
21+
}
22+
likelihood: LIKELY
23+
location {
24+
byte_range {
25+
start: 1208
26+
end: 1216
27+
}
28+
content_locations {
29+
container_name: "gs://fake_test_bucket/file.txt"
30+
document_location {
31+
}
32+
container_timestamp {
33+
seconds: 1728939753
34+
nanos: 301000000
35+
}
36+
container_version: "1728939753176395"
37+
}
38+
container {
39+
type: "Google Cloud Storage"
40+
project_id: "fake-project-id"
41+
full_path: "gs://fake_test_bucket/file.txt"
42+
root_path: "fake_test_bucket"
43+
relative_path: "file.txt"
44+
update_time {
45+
seconds: 1728939753
46+
nanos: 301000000
47+
}
48+
version: "1728939753176395"
49+
}
50+
}
51+
create_time {
52+
seconds: 1741889947
53+
nanos: 947000000
54+
}
55+
resource_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
56+
job_create_time {
57+
seconds: 1741889652
58+
nanos: 348000000
59+
}
60+
job_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
61+
finding_id: "2025-03-13T18:21:18.454889Z3148393127282654372"
62+
}
63+
findings {
64+
info_type {
65+
name: "PERSON_NAME"
66+
sensitivity_score {
67+
score: SENSITIVITY_MODERATE
68+
}
69+
}
70+
likelihood: POSSIBLE
71+
location {
72+
byte_range {
73+
start: 19872
74+
end: 19879
75+
}
76+
content_locations {
77+
container_name: "gs://fake_test_bucket/file.txt"
78+
document_location {
79+
}
80+
container_timestamp {
81+
seconds: 1728939753
82+
nanos: 301000000
83+
}
84+
container_version: "1728939753176395"
85+
}
86+
container {
87+
type: "Google Cloud Storage"
88+
project_id: "fake-project-id"
89+
full_path: "gs://fake_test_bucket/file.txt"
90+
root_path: "fake_test_bucket"
91+
relative_path: "file.txt"
92+
update_time {
93+
seconds: 1728939753
94+
nanos: 301000000
95+
}
96+
version: "1728939753176395"
97+
}
98+
}
99+
create_time {
100+
seconds: 1741889947
101+
nanos: 948000000
102+
}
103+
resource_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
104+
job_create_time {
105+
seconds: 1741889652
106+
nanos: 348000000
107+
}
108+
job_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
109+
finding_id: "2025-03-13T18:21:18.506689Z2134257296577089402"
110+
}

0 commit comments

Comments
 (0)