画像にある infoType を色分けして秘匿化する
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
機密データの保護用のクライアント ライブラリをインストールして使用する方法については、機密データの保護のクライアント ライブラリをご覧ください。
機密データの保護のために認証するには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;
using Google.Protobuf;
using System;
using System.IO;
public class RedactImageWithColorCodedInfoTypes
{
public static RedactImageResponse RedactImage(
string projectId,
string originalImagePath,
string redactedImagePath)
{
// Instantiate the dlp client.
var dlp = DlpServiceClient.Create();
// Construct the content item by providing the image and its type.
var byteContentItem = new ByteContentItem
{
Type = ByteContentItem.Types.BytesType.ImagePng,
Data = ByteString.FromStream(new FileStream(originalImagePath, FileMode.Open))
};
// Define types of info and associate each one with a different color to redact config.
var ssnRedactionConfig = new RedactImageRequest.Types.ImageRedactionConfig
{
InfoType = new InfoType { Name = "US_SOCIAL_SECURITY_NUMBER" },
RedactionColor = new Color
{
Red = 0.3f,
Green = 0.1f,
Blue = 0.6f
}
};
var emailRedactionConfig = new RedactImageRequest.Types.ImageRedactionConfig
{
InfoType = new InfoType { Name = "EMAIL_ADDRESS" },
RedactionColor = new Color
{
Red = 0.5f,
Green = 0.5f,
Blue = 1f
}
};
var phoneRedactionConfig = new RedactImageRequest.Types.ImageRedactionConfig
{
InfoType = new InfoType { Name = "PHONE_NUMBER" },
RedactionColor = new Color
{
Red = 1f,
Green = 0f,
Blue = 0.6f
}
};
// Construct the Redact request to be sent by the client. Do not specify the type of info to redact.
var request = new RedactImageRequest
{
ParentAsLocationName = new LocationName(projectId, "global"),
ImageRedactionConfigs = { ssnRedactionConfig, emailRedactionConfig, phoneRedactionConfig },
ByteItem = byteContentItem
};
// Call the API.
RedactImageResponse response = dlp.RedactImage(request);
// Writes redacted image into file
response.RedactedImage.WriteTo(new FileStream(redactedImagePath, FileMode.Create, FileAccess.Write));
Console.WriteLine($"Redacted image written to: {redactedImagePath}");
return response;
}
}
Go
機密データの保護用のクライアント ライブラリをインストールして使用する方法については、機密データの保護のクライアント ライブラリをご覧ください。
機密データの保護のために認証するには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。
import (
"context"
"fmt"
"io"
"io/ioutil"
dlp "cloud.google.com/go/dlp/apiv2"
"cloud.google.com/go/dlp/apiv2/dlppb"
)
// redactImageFileColoredInfoTypes redacts data from an image with color-coded infoTypes.
func redactImageFileColoredInfoTypes(w io.Writer, projectID, inputPath, outputPath string) error {
// projectId := "my-project-id"
// inputPath := "testdata/image.jpg"
// outputPath := "testdata/test-output-image-file-colored-infoType.jpeg"
ctx := context.Background()
// Initialize a client once and reuse it to send multiple requests. Clients
// are safe to use across goroutines. When the client is no longer needed,
// call the Close method to cleanup its resources.
client, err := dlp.NewClient(ctx)
if err != nil {
return err
}
// Closing the client safely cleans up background resources.
defer client.Close()
// read the image file
fileBytes, err := ioutil.ReadFile(inputPath)
if err != nil {
return err
}
// Specify the content to be redacted.
byteItem := &dlppb.ByteContentItem{
Type: dlppb.ByteContentItem_IMAGE_JPEG,
Data: fileBytes,
}
// Define types of info to redact associate each one with a different color.
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
ssnRedactionConfig := &dlppb.RedactImageRequest_ImageRedactionConfig{
Target: &dlppb.RedactImageRequest_ImageRedactionConfig_InfoType{
InfoType: &dlppb.InfoType{
Name: "US_SOCIAL_SECURITY_NUMBER",
},
},
RedactionColor: &dlppb.Color{
Red: 0.3,
Green: 0.1,
Blue: 0.6,
},
}
emailRedactionConfig := &dlppb.RedactImageRequest_ImageRedactionConfig{
Target: &dlppb.RedactImageRequest_ImageRedactionConfig_InfoType{
InfoType: &dlppb.InfoType{
Name: "EMAIL_ADDRESS",
},
},
RedactionColor: &dlppb.Color{
Red: 0.5,
Green: 0.5,
Blue: 1,
},
}
phoneRedactionConfig := &dlppb.RedactImageRequest_ImageRedactionConfig{
Target: &dlppb.RedactImageRequest_ImageRedactionConfig_InfoType{
InfoType: &dlppb.InfoType{
Name: "PHONE_NUMBER",
},
},
RedactionColor: &dlppb.Color{
Red: 1,
Green: 0,
Blue: 0.6,
},
}
// Construct the Inspect request to be sent by the client.
req := &dlppb.RedactImageRequest{
Parent: fmt.Sprintf("projects/%s/locations/global", projectID),
ByteItem: byteItem,
ImageRedactionConfigs: []*dlppb.RedactImageRequest_ImageRedactionConfig{
emailRedactionConfig,
phoneRedactionConfig,
ssnRedactionConfig,
},
}
// Send the request.
resp, err := client.RedactImage(ctx, req)
if err != nil {
return err
}
// Write the output file.
if err := ioutil.WriteFile(outputPath, resp.GetRedactedImage(), 0644); err != nil {
return err
}
fmt.Fprintf(w, "Wrote output to %s", outputPath)
return nil
}
Java
機密データの保護用のクライアント ライブラリをインストールして使用する方法については、機密データの保護のクライアント ライブラリをご覧ください。
機密データの保護のために認証するには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。
import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.ByteContentItem;
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
import com.google.privacy.dlp.v2.Color;
import com.google.privacy.dlp.v2.InfoType;
import com.google.privacy.dlp.v2.InspectConfig;
import com.google.privacy.dlp.v2.LocationName;
import com.google.privacy.dlp.v2.RedactImageRequest;
import com.google.privacy.dlp.v2.RedactImageRequest.ImageRedactionConfig;
import com.google.privacy.dlp.v2.RedactImageResponse;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class RedactImageFileColoredInfoTypes {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String inputPath = "src/test/resources/test.png";
String outputPath = "redacted.png";
redactImageFileColoredInfoTypes(projectId, inputPath, outputPath);
}
static void redactImageFileColoredInfoTypes(String projectId, String inputPath, String outputPath)
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 dlp = DlpServiceClient.create()) {
// Specify the content to be redacted.
ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
ByteContentItem byteItem =
ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build();
// Define types of info to redact associate each one with a different color.
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
ImageRedactionConfig ssnRedactionConfig =
ImageRedactionConfig.newBuilder()
.setInfoType(InfoType.newBuilder().setName("US_SOCIAL_SECURITY_NUMBER").build())
.setRedactionColor(Color.newBuilder().setRed(.3f).setGreen(.1f).setBlue(.6f).build())
.build();
ImageRedactionConfig emailRedactionConfig =
ImageRedactionConfig.newBuilder()
.setInfoType(InfoType.newBuilder().setName("EMAIL_ADDRESS").build())
.setRedactionColor(Color.newBuilder().setRed(.5f).setGreen(.5f).setBlue(1).build())
.build();
ImageRedactionConfig phoneRedactionConfig =
ImageRedactionConfig.newBuilder()
.setInfoType(InfoType.newBuilder().setName("PHONE_NUMBER").build())
.setRedactionColor(Color.newBuilder().setRed(1).setGreen(0).setBlue(.6f).build())
.build();
// Create collection of all redact configurations.
List<ImageRedactionConfig> imageRedactionConfigs =
Arrays.asList(ssnRedactionConfig, emailRedactionConfig, phoneRedactionConfig);
// List types of info to search for.
InspectConfig config =
InspectConfig.newBuilder()
.addAllInfoTypes(
imageRedactionConfigs.stream()
.map(ImageRedactionConfig::getInfoType)
.collect(Collectors.toList()))
.build();
// Construct the Redact request to be sent by the client.
RedactImageRequest request =
RedactImageRequest.newBuilder()
.setParent(LocationName.of(projectId, "global").toString())
.setByteItem(byteItem)
.addAllImageRedactionConfigs(imageRedactionConfigs)
.setInspectConfig(config)
.build();
// Use the client to send the API request.
RedactImageResponse response = dlp.redactImage(request);
// Parse the response and process results.
FileOutputStream redacted = new FileOutputStream(outputPath);
redacted.write(response.getRedactedImage().toByteArray());
redacted.close();
System.out.println("Redacted image written to " + outputPath);
}
}
}
Node.js
機密データの保護用のクライアント ライブラリをインストールして使用する方法については、機密データの保護のクライアント ライブラリをご覧ください。
機密データの保護のために認証するには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。
// Imports the Google Cloud Data Loss Prevention library
const DLP = require('@google-cloud/dlp');
// Imports required Node.js libraries
const mime = require('mime');
const fs = require('fs');
// Instantiates a client
const dlp = new DLP.DlpServiceClient();
// The project ID to run the API call under
// const projectId = 'my-project';
// The path to a local file to inspect. Can be a JPG or PNG image file.
// const filepath = 'path/to/image.png';
// The local path to save the resulting image to.
// const outputPath = 'result.png';
async function redactImageColoredInfoType() {
// Define types of info to redact associate each one with a different color.
const imageRedactionConfigs = [
{
infoType: {name: 'US_SOCIAL_SECURITY_NUMBER'},
redactionColor: {red: 0.3, green: 0.1, blue: 0.6},
},
{
infoType: {name: 'EMAIL_ADDRESS'},
redactionColor: {red: 0.5, green: 0.5, blue: 1},
},
{
infoType: {name: 'PHONE_NUMBER'},
redactionColor: {red: 1, green: 0, blue: 0.6},
},
];
// Load image
const fileTypeConstant =
['image/jpeg', 'image/bmp', 'image/png', 'image/svg'].indexOf(
mime.getType(filepath)
) + 1;
const fileBytes = Buffer.from(fs.readFileSync(filepath)).toString('base64');
// Construct the Redact request to be sent by the client.
const request = {
parent: `projects/${projectId}/locations/global`,
byteItem: {
type: fileTypeConstant,
data: fileBytes,
},
imageRedactionConfigs: imageRedactionConfigs,
};
// Send the request and receive response from the service.
const [response] = await dlp.redactImage(request);
const image = response.redactedImage;
fs.writeFileSync(outputPath, image);
console.log(`Saved image redaction results to path: ${outputPath}`);
}
redactImageColoredInfoType();
PHP
機密データの保護用のクライアント ライブラリをインストールして使用する方法については、機密データの保護のクライアント ライブラリをご覧ください。
機密データの保護のために認証するには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。
use Google\Cloud\Dlp\V2\ByteContentItem;
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;
use Google\Cloud\Dlp\V2\Color;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\RedactImageRequest;
use Google\Cloud\Dlp\V2\RedactImageRequest\ImageRedactionConfig;
/**
* Redact data from an image with color-coded infoTypes.
*
* @param string $callingProjectId The project ID to run the API call under.
* @param string $imagePath The local filepath of the image to inspect.
* @param string $outputPath The local filepath to save the resulting image to.
*/
function redact_image_colored_infotypes(
// TODO(developer): Replace sample parameters before running the code.
string $callingProjectId,
string $imagePath = './test/data/test.png',
string $outputPath = './test/data/sensitive-data-image-redacted-color-coding.png'
): void {
// Instantiate a client.
$dlp = new DlpServiceClient();
// Read image file into a buffer.
$imageRef = fopen($imagePath, 'rb');
$imageBytes = fread($imageRef, filesize($imagePath));
fclose($imageRef);
// Get the image's content type.
$typeConstant = (int) array_search(
mime_content_type($imagePath),
[false, 'image/jpeg', 'image/bmp', 'image/png', 'image/svg']
);
// Create the byte-storing object.
$byteContent = (new ByteContentItem())
->setType($typeConstant)
->setData($imageBytes);
// Define the types of information to redact and associate each one with a different color.
$ssnInfotype = (new InfoType())
->setName('US_SOCIAL_SECURITY_NUMBER');
$emailInfotype = (new InfoType())
->setName('EMAIL_ADDRESS');
$phoneInfotype = (new InfoType())
->setName('PHONE_NUMBER');
$infotypes = [$ssnInfotype, $emailInfotype, $phoneInfotype];
$ssnRedactionConfig = (new ImageRedactionConfig())
->setInfoType($ssnInfotype)
->setRedactionColor((new Color())
->setRed(.3)
->setGreen(.1)
->setBlue(.6));
$emailRedactionConfig = (new ImageRedactionConfig())
->setInfoType($emailInfotype)
->setRedactionColor((new Color())
->setRed(.5)
->setGreen(.5)
->setBlue(1));
$phoneRedactionConfig = (new ImageRedactionConfig())
->setInfoType($phoneInfotype)
->setRedactionColor((new Color())
->setRed(1)
->setGreen(0)
->setBlue(.6));
$imageRedactionConfigs = [$ssnRedactionConfig, $emailRedactionConfig, $phoneRedactionConfig];
// Create the configuration object.
$inspectConfig = (new InspectConfig())
->setInfoTypes($infotypes);
$parent = "projects/$callingProjectId/locations/global";
// Run request.
$redactImageRequest = (new RedactImageRequest())
->setParent($parent)
->setByteItem($byteContent)
->setInspectConfig($inspectConfig)
->setImageRedactionConfigs($imageRedactionConfigs);
$response = $dlp->redactImage($redactImageRequest);
// Save result to file.
file_put_contents($outputPath, $response->getRedactedImage());
// Print completion message.
printf('Redacted image saved to %s ' . PHP_EOL, $outputPath);
}
Python
機密データの保護用のクライアント ライブラリをインストールして使用する方法については、機密データの保護のクライアント ライブラリをご覧ください。
機密データの保護のために認証するには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。
import google.cloud.dlp
def redact_image_with_colored_info_types(
project: str,
filename: str,
output_filename: str,
) -> None:
"""Uses the Data Loss Prevention API to redact protected data in an image by
color coding the infoTypes.
Args:
project: The Google Cloud project id to use as a parent resource.
filename: The path of the image file to inspect.
output_filename: The path to which the redacted image will be written.
"""
# Instantiate a client.
dlp = google.cloud.dlp_v2.DlpServiceClient()
# Prepare image_redaction_configs, a list of dictionaries. Each dictionary
# contains an infoType and the color used for the replacement.
ssn_redaction_config = {
"info_type": {"name": "US_SOCIAL_SECURITY_NUMBER"},
"redaction_color": {
"red": 0.3,
"green": 0.1,
"blue": 0.6,
},
}
email_redaction_config = {
"info_type": {"name": "EMAIL_ADDRESS"},
"redaction_color": {
"red": 0.5,
"green": 0.5,
"blue": 1.0,
},
}
phone_redaction_config = {
"info_type": {"name": "PHONE_NUMBER"},
"redaction_color": {
"red": 1.0,
"green": 0.0,
"blue": 0.6,
},
}
image_redaction_configs = [
ssn_redaction_config,
email_redaction_config,
phone_redaction_config,
]
# Construct the configuration dictionary.
inspect_config = {"info_types": [_i["info_type"] for _i in image_redaction_configs]}
# Construct the byte_item, containing the file's byte data.
with open(filename, mode="rb") as f:
byte_item = {"type_": "IMAGE", "data": f.read()}
# Convert the project id into a full resource id.
parent = f"projects/{project}"
# Call the API.
response = dlp.redact_image(
request={
"parent": parent,
"inspect_config": inspect_config,
"image_redaction_configs": image_redaction_configs,
"byte_item": byte_item,
}
)
# Write out the results.
with open(output_filename, mode="wb") as f:
f.write(response.redacted_image)
byte_count = len(response.redacted_image)
print(f"Wrote {byte_count} to {output_filename}")
次のステップ
他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。