使用 Cloud Data Loss Prevention API 创建检查作业。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
C#
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
using System;
using System.Linq;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;
using static Google.Cloud.Dlp.V2.StorageConfig.Types;
public class JobsCreate
{
public static DlpJob CreateJob(string projectId, string gcsPath)
{
var dlp = DlpServiceClient.Create();
var storageConfig = new StorageConfig
{
CloudStorageOptions = new CloudStorageOptions
{
FileSet = new CloudStorageOptions.Types.FileSet()
{
Url = gcsPath
}
},
TimespanConfig = new TimespanConfig
{
EnableAutoPopulationOfTimespanConfig = true
}
};
var inspectConfig = new InspectConfig
{
InfoTypes = { new[] { "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER" }.Select(it => new InfoType() { Name = it }) },
IncludeQuote = true,
MinLikelihood = Likelihood.Unlikely,
Limits = new InspectConfig.Types.FindingLimits() { MaxFindingsPerItem = 100 }
};
var response = dlp.CreateDlpJob(new CreateDlpJobRequest
{
Parent = new LocationName(projectId, "global").ToString(),
InspectJob = new InspectJobConfig
{
InspectConfig = inspectConfig,
StorageConfig = storageConfig,
}
});
Console.WriteLine($"Job: {response.Name} status: {response.State}");
return response;
}
}
Java
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.Action;
import com.google.privacy.dlp.v2.CloudStorageOptions;
import com.google.privacy.dlp.v2.CreateDlpJobRequest;
import com.google.privacy.dlp.v2.DlpJob;
import com.google.privacy.dlp.v2.InfoType;
import com.google.privacy.dlp.v2.InspectConfig;
import com.google.privacy.dlp.v2.InspectJobConfig;
import com.google.privacy.dlp.v2.Likelihood;
import com.google.privacy.dlp.v2.LocationName;
import com.google.privacy.dlp.v2.StorageConfig;
import com.google.privacy.dlp.v2.StorageConfig.TimespanConfig;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class JobsCreate {
public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String gcsPath = "gs://" + "your-bucket-name" + "path/to/file.txt";
createJobs(projectId, gcsPath);
}
// Creates a DLP Job
public static void createJobs(String projectId, String gcsPath) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
// Set autoPopulateTimespan to true to scan only new content
boolean autoPopulateTimespan = true;
TimespanConfig timespanConfig =
TimespanConfig.newBuilder()
.setEnableAutoPopulationOfTimespanConfig(autoPopulateTimespan)
.build();
// Specify the GCS file to be inspected.
CloudStorageOptions cloudStorageOptions =
CloudStorageOptions.newBuilder()
.setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl(gcsPath))
.build();
StorageConfig storageConfig =
StorageConfig.newBuilder()
.setCloudStorageOptions(cloudStorageOptions)
.setTimespanConfig(timespanConfig)
.build();
// Specify the type of info the inspection will look for.
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
List<InfoType> infoTypes =
Stream.of("EMAIL_ADDRESS", "PERSON_NAME", "LOCATION", "PHONE_NUMBER")
.map(it -> InfoType.newBuilder().setName(it).build())
.collect(Collectors.toList());
// The minimum likelihood required before returning a match:
// See: https://cloud.google.com/dlp/docs/likelihood
Likelihood minLikelihood = Likelihood.UNLIKELY;
// The maximum number of findings to report (0 = server maximum)
InspectConfig.FindingLimits findingLimits =
InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(100).build();
InspectConfig inspectConfig =
InspectConfig.newBuilder()
.addAllInfoTypes(infoTypes)
.setIncludeQuote(true)
.setMinLikelihood(minLikelihood)
.setLimits(findingLimits)
.build();
// Specify the action that is triggered when the job completes.
Action.PublishSummaryToCscc publishSummaryToCscc =
Action.PublishSummaryToCscc.getDefaultInstance();
Action action = Action.newBuilder().setPublishSummaryToCscc(publishSummaryToCscc).build();
// Configure the inspection job we want the service to perform.
InspectJobConfig inspectJobConfig =
InspectJobConfig.newBuilder()
.setInspectConfig(inspectConfig)
.setStorageConfig(storageConfig)
.addActions(action)
.build();
// Construct the job creation request to be sent by the client.
CreateDlpJobRequest createDlpJobRequest =
CreateDlpJobRequest.newBuilder()
.setParent(LocationName.of(projectId, "global").toString())
.setInspectJob(inspectJobConfig)
.build();
// Send the job creation request and process the response.
DlpJob createdDlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);
System.out.println("Job created successfully: " + createdDlpJob.getName());
}
}
}
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。