解析以 Protobuf 文本形式存储的发现结果

此代码示例展示了如何从以 Protobuf 文本格式保存的检查结果中提取详细信息。如果您将检查结果保存到了 Cloud Storage,请执行此任务。此示例将导出的 SaveToGcsFindingsOutput 对象转换为人类可读的格式。然后,您可以使用输出结果来分析各个检查结果。

Java

如需了解如何安装和使用 Sensitive Data Protection 客户端库,请参阅 Sensitive Data Protection 客户端库

如需向 Sensitive Data Protection 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.SaveToGcsFindingsOutput;
import com.google.protobuf.ByteString;
import com.google.protobuf.TextFormat;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

public class ProcessInspectFindingsSavedToGcs {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String inputPath = "src/test/resources/save_to_gcs_findings.txt";
    processFindingsGcsFile(inputPath);
  }

  // Processes a file containing findings from a DLP inspect job.
  public static void processFindingsGcsFile(String inputPath)
      throws IOException {
    SaveToGcsFindingsOutput.Builder builder = SaveToGcsFindingsOutput.newBuilder();
    try (Reader reader =
        new InputStreamReader(new FileInputStream(inputPath), StandardCharsets.UTF_8)) {
      TextFormat.merge(reader, builder);
    }
    SaveToGcsFindingsOutput output = builder.build();

    // Parse the converted proto and process results
    System.out.println("Findings: " + output.getFindingsCount());
    for (Finding f : output.getFindingsList()) {
      System.out.println("\tInfo type: " + f.getInfoType().getName());
      System.out.println("\tLikelihood: " + f.getLikelihood());
    }
  }
}