删除 DLP 作业。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
C#
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
using System;
using Google.Cloud.Dlp.V2;
public class JobsDelete
{
public static void DeleteJob(string jobName)
{
var dlp = DlpServiceClient.Create();
dlp.DeleteDlpJob(new DeleteDlpJobRequest
{
Name = jobName
});
Console.WriteLine($"Successfully deleted job {jobName}.");
}
}
Go
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
import (
"context"
"fmt"
"io"
dlp "cloud.google.com/go/dlp/apiv2"
dlppb "google.golang.org/genproto/googleapis/privacy/dlp/v2"
)
// deleteJob deletes the job with the given name.
func deleteJob(w io.Writer, jobName string) error {
// jobName := "job-example"
ctx := context.Background()
client, err := dlp.NewClient(ctx)
if err != nil {
return fmt.Errorf("dlp.NewClient: %v", err)
}
defer client.Close()
req := &dlppb.DeleteDlpJobRequest{
Name: jobName,
}
if err = client.DeleteDlpJob(ctx, req); err != nil {
return fmt.Errorf("DeleteDlpJob: %v", err)
}
fmt.Fprintf(w, "Successfully deleted job")
return nil
}
Java
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.DeleteDlpJobRequest;
import com.google.privacy.dlp.v2.DlpJobName;
import java.io.IOException;
public class JobsDelete {
public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String jobId = "your-job-id";
deleteJobs(projectId, jobId);
}
// Deletes a DLP Job with the given jobId
public static void deleteJobs(String projectId, String jobId) 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()) {
// Construct the complete job name from the projectId and jobId
DlpJobName jobName = DlpJobName.of(projectId, jobId);
// Construct the job deletion request to be sent by the client.
DeleteDlpJobRequest deleteDlpJobRequest =
DeleteDlpJobRequest.newBuilder().setName(jobName.toString()).build();
// Send the job deletion request
dlpServiceClient.deleteDlpJob(deleteDlpJobRequest);
System.out.println("Job deleted successfully.");
}
}
}
Node.js
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
// Imports the Google Cloud Data Loss Prevention library
const DLP = require('@google-cloud/dlp');
// Instantiates a client
const dlp = new DLP.DlpServiceClient();
// The project ID to run the API call under
// const projectId = 'my-project';
// The name of the job whose results should be deleted
// Parent project ID is automatically extracted from this parameter
// const jobName = 'projects/my-project/dlpJobs/X-#####'
function deleteJob() {
// Construct job deletion request
const request = {
name: jobName,
};
// Run job deletion request
dlp
.deleteDlpJob(request)
.then(() => {
console.log(`Successfully deleted job ${jobName}.`);
})
.catch(err => {
console.log(`Error in deleteJob: ${err.message || err}`);
});
}
deleteJob();
PHP
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
/**
* Delete results of a Data Loss Prevention API job
*/
use Google\Cloud\Dlp\V2\DlpServiceClient;
/** Uncomment and populate these variables in your code */
// $jobId = 'The name of the job whose results should be deleted';
// Instantiate a client.
$dlp = new DlpServiceClient();
// Run job-deletion request
// The Parent project ID is automatically extracted from this parameter
$dlp->deleteDlpJob($jobId);
// Print status
printf('Successfully deleted job %s' . PHP_EOL, $jobId);
Python
如需了解如何安装和使用 Cloud DLP 客户端库,请参阅 Cloud DLP 客户端库。
def delete_dlp_job(project, job_name):
"""Uses the Data Loss Prevention API to delete a long-running DLP job.
Args:
project: The project id to use as a parent resource.
job_name: The name of the DlpJob resource to be deleted.
Returns:
None; the response from the API is printed to the terminal.
"""
# Import the client library.
import google.cloud.dlp
# Instantiate a client.
dlp = google.cloud.dlp_v2.DlpServiceClient()
# Convert the project id and job name into a full resource id.
name = f"projects/{project}/dlpJobs/{job_name}"
# Call the API to delete job.
dlp.delete_dlp_job(request={"name": name})
print("Successfully deleted %s" % job_name)
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。