Skip to content

Commit e78bf49

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

File tree

4 files changed

+165
-1
lines changed

4 files changed

+165
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
28+
public class ProcessInspectFindingsSavedToGcs {
29+
30+
public static void main(String[] args) throws Exception {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String inputPath = "src/test/resources/save_to_gcs_findings.txt";
33+
processFindingsGcsFile(inputPath);
34+
}
35+
36+
// Processes a file containing findings from a DLP inspect job.
37+
public static void processFindingsGcsFile(String inputPath)
38+
throws IOException {
39+
SaveToGcsFindingsOutput.Builder builder = SaveToGcsFindingsOutput.newBuilder();
40+
try (Reader reader =
41+
new InputStreamReader(new FileInputStream(inputPath), StandardCharsets.UTF_8)) {
42+
TextFormat.merge(reader, builder);
43+
}
44+
SaveToGcsFindingsOutput output = builder.build();
45+
46+
// Parse the converted proto and process results
47+
System.out.println("Findings: " + output.getFindingsCount());
48+
for (Finding f : output.getFindingsList()) {
49+
System.out.println("\tInfo type: " + f.getInfoType().getName());
50+
System.out.println("\tLikelihood: " + f.getLikelihood());
51+
}
52+
}
53+
}
54+
// [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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
findings {
2+
info_type {
3+
name: "PERSON_NAME"
4+
sensitivity_score {
5+
score: SENSITIVITY_MODERATE
6+
}
7+
}
8+
likelihood: LIKELY
9+
location {
10+
byte_range {
11+
start: 1208
12+
end: 1216
13+
}
14+
content_locations {
15+
container_name: "gs://fake_test_bucket/file.txt"
16+
document_location {
17+
}
18+
container_timestamp {
19+
seconds: 1728939753
20+
nanos: 301000000
21+
}
22+
container_version: "1728939753176395"
23+
}
24+
container {
25+
type: "Google Cloud Storage"
26+
project_id: "fake-project-id"
27+
full_path: "gs://fake_test_bucket/file.txt"
28+
root_path: "fake_test_bucket"
29+
relative_path: "file.txt"
30+
update_time {
31+
seconds: 1728939753
32+
nanos: 301000000
33+
}
34+
version: "1728939753176395"
35+
}
36+
}
37+
create_time {
38+
seconds: 1741889947
39+
nanos: 947000000
40+
}
41+
resource_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
42+
job_create_time {
43+
seconds: 1741889652
44+
nanos: 348000000
45+
}
46+
job_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
47+
finding_id: "2025-03-13T18:21:18.454889Z3148393127282654372"
48+
}
49+
findings {
50+
info_type {
51+
name: "PERSON_NAME"
52+
sensitivity_score {
53+
score: SENSITIVITY_MODERATE
54+
}
55+
}
56+
likelihood: POSSIBLE
57+
location {
58+
byte_range {
59+
start: 19872
60+
end: 19879
61+
}
62+
content_locations {
63+
container_name: "gs://fake_test_bucket/file.txt"
64+
document_location {
65+
}
66+
container_timestamp {
67+
seconds: 1728939753
68+
nanos: 301000000
69+
}
70+
container_version: "1728939753176395"
71+
}
72+
container {
73+
type: "Google Cloud Storage"
74+
project_id: "fake-project-id"
75+
full_path: "gs://fake_test_bucket/file.txt"
76+
root_path: "fake_test_bucket"
77+
relative_path: "file.txt"
78+
update_time {
79+
seconds: 1728939753
80+
nanos: 301000000
81+
}
82+
version: "1728939753176395"
83+
}
84+
}
85+
create_time {
86+
seconds: 1741889947
87+
nanos: 948000000
88+
}
89+
resource_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
90+
job_create_time {
91+
seconds: 1741889652
92+
nanos: 348000000
93+
}
94+
job_name: "projects/fake-project-id/locations/global/dlpJobs/i-test-gcs-save"
95+
finding_id: "2025-03-13T18:21:18.506689Z2134257296577089402"
96+
}

0 commit comments

Comments
 (0)