画像内の機密データの検査

Cloud Data Loss Prevention(DLP)を使用すると、画像内の機密データを検出して分類できます。Cloud DLP は、infoType 検出器と光学式文字認識(OCR)を使用して、テキストを Base64 でエンコードした画像を検査し、テキスト内の機密データを検出します。そして、検出した機密データの場所を返します。

入力として画像が指定されると、Cloud DLP は画像内の機密データを検出します。検査オペレーションの出力には、検出された infoType、一致の可能性、Cloud DLP が機密データを検出した領域を示すピクセル座標と長さの値が含まれます。画像の左下隅の座標は (0,0) です。

画像にあるすべてのデフォルトの infoType を検査する

画像に含まれる機密データを検査するには、base64 でエンコードされた画像を DLP API の content.inspect メソッドに送信します。検索する特定の情報タイプ(infoType)を指定しない限り、Cloud DLP は最も一般的な infoType を検索します。

画像にあるデフォルトの infoType を検査するには:

  1. 画像を base64 でエンコードします。
  2. DLP API の content.inspect メソッドにリクエストを送信します。デフォルトの infoType を検査する場合は、リクエストに base64 エンコード画像のみを含める必要があります。

たとえば、次の画像について考えてみます。この画像は、紙のドキュメントをスキャンして生成された一般的な画像ファイルの例です。

元の画像(クリックして拡大)

この画像のデフォルトの infoType を検査するには、次のリクエストを DLP API の content.inspect メソッドに送信します。

プロトコル

{
  "item": {
    "byteItem": {
      "data": "[BASE64-ENCODED-IMAGE]",
      "type": "IMAGE_JPEG"
    }
  }
}

Cloud DLP は以下を返します。

{
  "result": {
    "findings": [
      {
        "infoType": {
          "name": "PHONE_NUMBER"
        },
        "likelihood": "UNLIKELY",
        "location": {
          "contentLocations": [
            {
              "imageLocation": {
                "boundingBoxes": [
                  {
                    "top": 98,
                    "left": 986,
                    "width": 102,
                    "height": 117
                  },
                  {
                    "top": 98,
                    "left": 1092,
                    "width": 29,
                    "height": 114
                  },
                  {
                    "top": 95,
                    "left": 1111,
                    "width": 82,
                    "height": 115
                  },
                  {
                    "top": 95,
                    "left": 1197,
                    "width": 29,
                    "height": 114
                  },
                  {
                    "top": 90,
                    "left": 1203,
                    "width": 185,
                    "height": 118
                  }
                ]
              }
            }
          ]
        },
        "createTime": "2019-11-01T22:07:05.870Z"
      },
      {
        "infoType": {
          "name": "US_SOCIAL_SECURITY_NUMBER"
        },
        "likelihood": "VERY_LIKELY",
        "location": {
          "contentLocations": [
            {
              "imageLocation": {
                "boundingBoxes": [
                  {
                    "top": 98,
                    "left": 986,
                    "width": 102,
                    "height": 117
                  },
                  {
                    "top": 98,
                    "left": 1092,
                    "width": 29,
                    "height": 114
                  },
                  {
                    "top": 95,
                    "left": 1111,
                    "width": 82,
                    "height": 115
                  },
                  {
                    "top": 95,
                    "left": 1197,
                    "width": 29,
                    "height": 114
                  },
                  {
                    "top": 90,
                    "left": 1203,
                    "width": 185,
                    "height": 118
                  }
                ]
              }
            }
          ]
        },
        "createTime": "2019-11-01T22:07:05.871Z"
      },
      {
        "infoType": {
          "name": "EMAIL_ADDRESS"
        },
        "likelihood": "LIKELY",
        "location": {
          "contentLocations": [
            {
              "imageLocation": {
                "boundingBoxes": [
                  {
                    "top": 340,
                    "left": 334,
                    "width": 57,
                    "height": 58
                  },
                  {
                    "top": 340,
                    "left": 384,
                    "width": 12,
                    "height": 58
                  },
                  {
                    "top": 340,
                    "left": 387,
                    "width": 79,
                    "height": 59
                  },
                  {
                    "top": 341,
                    "left": 467,
                    "width": 12,
                    "height": 58
                  },
                  {
                    "top": 340,
                    "left": 476,
                    "width": 119,
                    "height": 61
                  },
                  {
                    "top": 341,
                    "left": 589,
                    "width": 12,
                    "height": 58
                  },
                  {
                    "top": 342,
                    "left": 592,
                    "width": 45,
                    "height": 58
                  }
                ]
              }
            }
          ]
        },
        "createTime": "2019-11-01T22:07:05.869Z"
      },
      {
        "infoType": {
          "name": "PHONE_NUMBER"
        },
        "likelihood": "POSSIBLE",
        "location": {
          "contentLocations": [
            {
              "imageLocation": {
                "boundingBoxes": [
                  {
                    "top": 394,
                    "left": 335,
                    "width": 50,
                    "height": 77
                  },
                  {
                    "top": 394,
                    "left": 380,
                    "width": 17,
                    "height": 77
                  },
                  {
                    "top": 394,
                    "left": 387,
                    "width": 51,
                    "height": 77
                  },
                  {
                    "top": 394,
                    "left": 433,
                    "width": 17,
                    "height": 77
                  },
                  {
                    "top": 394,
                    "left": 436,
                    "width": 77,
                    "height": 77
                  }
                ]
              }
            }
          ]
        },
        "createTime": "2019-11-01T22:07:05.870Z"
      },
      {
        "infoType": {
          "name": "DATE"
        },
        "likelihood": "LIKELY",
        "location": {
          "contentLocations": [
            {
              "imageLocation": {
                "boundingBoxes": [
                  {
                    "top": 572,
                    "left": 1129,
                    "width": 71,
                    "height": 38
                  }
                ]
              }
            }
          ]
        },
        "createTime": "2019-11-01T22:07:05.921Z"
      }
    ]
  }
}

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.ContentItem;
import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.InspectContentRequest;
import com.google.privacy.dlp.v2.InspectContentResponse;
import com.google.privacy.dlp.v2.LocationName;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;

class InspectImageFileAllInfoTypes {

  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/sensitive-data-image.jpeg";
    inspectImageFileAllInfoTypes(projectId, inputPath);
  }

  static void inspectImageFileAllInfoTypes(String projectId, String inputPath)
      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 inspected.
      ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
      ByteContentItem byteItem =
          ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build();

      // Construct the Inspect request to be sent by the client.
      // Do not specify the type of info to inspect.
      InspectContentRequest request =
          InspectContentRequest.newBuilder()
              .setParent(LocationName.of(projectId, "global").toString())
              .setItem(ContentItem.newBuilder().setByteItem(byteItem).build())
              .build();

      // Use the client to send the API request.
      InspectContentResponse response = dlp.inspectContent(request);

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

Cloud DLP では、次の infoType が検出されましたが、それぞれの一致の可能性は異なります。

  • 電話番号(一致する可能性が低い)
  • 米国社会保障番号(一致する可能性が非常に高い)
  • メールアドレス(一致する可能性が高い)
  • 電話番号(一致する可能性がある)
  • 日付(一致する可能性が高い)

画像上に境界ボックスを描画すると、次のようになります。Cloud DLP では、1 つの機密データのインスタンスが画像内のどこにあるかを示すために、複数のボックスが使われることが多いので注意してください。

画像に境界ボックスを重ねて表示(クリックで拡大)

手書きの社会保障番号、メールアドレス、電話番号の検出に加えて、Cloud DLP では年も検出されていることに注目してください。これが最適な動作ではないと仮定して、次の例では特定の infoType のみを検査する方法を示します。

画像にある特定の infoType を検査する

特定の機密データ タイプに対してのみ画像を検査するには、対応する組み込みの infoType を指定します。

画像にある特定の infoType を検査するには:

  1. 画像を base64 でエンコードします。
  2. DLP API の content.inspect メソッドにリクエストを送信します。リクエストには以下を含める必要があります。

前のセクションの元の画像について考えてみます。米国社会保障番号、メールアドレス、電話番号のみを検査するには、次の JSON を DLP API の content.inspect メソッドに送信します。

プロトコル

{
  "item": {
    "byteItem": {
      "data": "[BASE64-ENCODED-IMAGE]",
      "type": "IMAGE_JPEG"
    }
  },
  "inspectConfig": {
    "infoTypes": [
      {
        "name": "US_SOCIAL_SECURITY_NUMBER"
      },
      {
        "name": "PHONE_NUMBER"
      },
      {
        "name": "EMAIL_ADDRESS"
      }
    ]
  }
}

Cloud DLP は以下を返します。

{
  "result": {
    "findings": [
      {
        "infoType": {
          "name": "US_SOCIAL_SECURITY_NUMBER"
        },
        "likelihood": "VERY_LIKELY",
        "location": {
          "contentLocations": [
            {
              "imageLocation": {
                "boundingBoxes": [
                  {
                    "top": 98,
                    "left": 986,
                    "width": 102,
                    "height": 117
                  },
                  {
                    "top": 98,
                    "left": 1092,
                    "width": 29,
                    "height": 114
                  },
                  {
                    "top": 95,
                    "left": 1111,
                    "width": 82,
                    "height": 115
                  },
                  {
                    "top": 95,
                    "left": 1197,
                    "width": 29,
                    "height": 114
                  },
                  {
                    "top": 90,
                    "left": 1203,
                    "width": 185,
                    "height": 118
                  }
                ]
              }
            }
          ]
        },
        "createTime": "2019-11-01T22:58:56.985Z"
      },
      {
        "infoType": {
          "name": "EMAIL_ADDRESS"
        },
        "likelihood": "LIKELY",
        "location": {
          "contentLocations": [
            {
              "imageLocation": {
                "boundingBoxes": [
                  {
                    "top": 340,
                    "left": 334,
                    "width": 57,
                    "height": 58
                  },
                  {
                    "top": 340,
                    "left": 384,
                    "width": 12,
                    "height": 58
                  },
                  {
                    "top": 340,
                    "left": 387,
                    "width": 79,
                    "height": 59
                  },
                  {
                    "top": 341,
                    "left": 467,
                    "width": 12,
                    "height": 58
                  },
                  {
                    "top": 340,
                    "left": 476,
                    "width": 119,
                    "height": 61
                  },
                  {
                    "top": 341,
                    "left": 589,
                    "width": 12,
                    "height": 58
                  },
                  {
                    "top": 342,
                    "left": 592,
                    "width": 45,
                    "height": 58
                  }
                ]
              }
            }
          ]
        },
        "createTime": "2019-11-01T22:58:56.984Z"
      },
      {
        "infoType": {
          "name": "PHONE_NUMBER"
        },
        "likelihood": "POSSIBLE",
        "location": {
          "contentLocations": [
            {
              "imageLocation": {
                "boundingBoxes": [
                  {
                    "top": 394,
                    "left": 335,
                    "width": 50,
                    "height": 77
                  },
                  {
                    "top": 394,
                    "left": 380,
                    "width": 17,
                    "height": 77
                  },
                  {
                    "top": 394,
                    "left": 387,
                    "width": 51,
                    "height": 77
                  },
                  {
                    "top": 394,
                    "left": 433,
                    "width": 17,
                    "height": 77
                  },
                  {
                    "top": 394,
                    "left": 436,
                    "width": 77,
                    "height": 77
                  }
                ]
              }
            }
          ]
        },
        "createTime": "2019-11-01T22:58:56.985Z"
      }
    ]
  }
}

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.ContentItem;
import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.InfoType;
import com.google.privacy.dlp.v2.InspectConfig;
import com.google.privacy.dlp.v2.InspectContentRequest;
import com.google.privacy.dlp.v2.InspectContentResponse;
import com.google.privacy.dlp.v2.LocationName;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class InspectImageFileListedInfoTypes {

  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/sensitive-data-image.jpeg";
    inspectImageFileListedInfoTypes(projectId, inputPath);
  }

  static void inspectImageFileListedInfoTypes(String projectId, String inputPath)
      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 inspected.
      ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
      ByteContentItem byteItem =
          ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build();

      // Specify the type of info the inspection will look for.
      List<InfoType> infoTypes = new ArrayList<>();
      // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
      for (String typeName :
          new String[] {"US_SOCIAL_SECURITY_NUMBER", "EMAIL_ADDRESS", "PHONE_NUMBER"}) {
        infoTypes.add(InfoType.newBuilder().setName(typeName).build());
      }

      // Construct the configuration for the Inspect request.
      InspectConfig inspectConfig =
          InspectConfig.newBuilder()
              .addAllInfoTypes(infoTypes)
              .build();

      // Construct the Inspect request to be sent by the client.
      InspectContentRequest request =
          InspectContentRequest.newBuilder()
              .setParent(LocationName.of(projectId, "global").toString())
              .setItem(ContentItem.newBuilder().setByteItem(byteItem).build())
              .setInspectConfig(inspectConfig)
              .build();

      // Use the client to send the API request.
      InspectContentResponse response = dlp.inspectContent(request);

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

出力からわかるように、Cloud DLP は米国社会保障番号、メールアドレス、電話番号を検出しました。

JSON で Cloud DLP API を使用する方法については、JSON クイックスタートをご覧ください。

コードの例

次の複数の言語のサンプルコードは、Cloud DLP を使用して画像の機密データを検査する方法を示しています。

Node.js

// Imports the Google Cloud Data Loss Prevention library
const DLP = require('@google-cloud/dlp');

// Import other required libraries
const fs = require('fs');
const mime = require('mime');

// Instantiates a client
const dlp = new DLP.DlpServiceClient();

// The project ID to run the API call under
// const callingProjectId = process.env.GCLOUD_PROJECT;

// The path to a local file to inspect. Can be a text, JPG, or PNG file.
// const filepath = 'path/to/image.png';

// The minimum likelihood required before returning a match
// const minLikelihood = 'LIKELIHOOD_UNSPECIFIED';

// The maximum number of findings to report per request (0 = server maximum)
// const maxFindings = 0;

// The infoTypes of information to match
// const infoTypes = [{ name: 'PHONE_NUMBER' }, { name: 'EMAIL_ADDRESS' }, { name: 'CREDIT_CARD_NUMBER' }];

// The customInfoTypes of information to match
// const customInfoTypes = [{ infoType: { name: 'DICT_TYPE' }, dictionary: { wordList: { words: ['foo', 'bar', 'baz']}}},
//   { infoType: { name: 'REGEX_TYPE' }, regex: '\\(\\d{3}\\) \\d{3}-\\d{4}'}];

// Whether to include the matching string
// const includeQuote = true;

// Construct file data to inspect
const fileTypeConstant =
  ['image/jpeg', 'image/bmp', 'image/png', 'image/svg'].indexOf(
    mime.getType(filepath)
  ) + 1;
const fileBytes = Buffer.from(fs.readFileSync(filepath)).toString('base64');
const item = {
  byteItem: {
    type: fileTypeConstant,
    data: fileBytes,
  },
};

// Construct request
const request = {
  parent: `projects/${callingProjectId}/locations/global`,
  inspectConfig: {
    infoTypes: infoTypes,
    customInfoTypes: customInfoTypes,
    minLikelihood: minLikelihood,
    includeQuote: includeQuote,
    limits: {
      maxFindingsPerRequest: maxFindings,
    },
  },
  item: item,
};

// Run request
try {
  const [response] = await dlp.inspectContent(request);
  const findings = response.result.findings;
  if (findings.length > 0) {
    console.log('Findings:');
    findings.forEach(finding => {
      if (includeQuote) {
        console.log(`\tQuote: ${finding.quote}`);
      }
      console.log(`\tInfo type: ${finding.infoType.name}`);
      console.log(`\tLikelihood: ${finding.likelihood}`);
    });
  } else {
    console.log('No findings.');
  }
} catch (err) {
  console.log(`Error in inspectFile: ${err.message || err}`);
}

Python



def inspect_file(
    project,
    filename,
    info_types,
    min_likelihood=None,
    custom_dictionaries=None,
    custom_regexes=None,
    max_findings=None,
    include_quote=True,
    mime_type=None,
):
    """Uses the Data Loss Prevention API to analyze a file for protected data.
    Args:
        project: The Google Cloud project id to use as a parent resource.
        filename: The path to the file to inspect.
        info_types: A list of strings representing info types to look for.
            A full list of info type categories can be fetched from the API.
        min_likelihood: A string representing the minimum likelihood threshold
            that constitutes a match. One of: 'LIKELIHOOD_UNSPECIFIED',
            'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', 'LIKELY', 'VERY_LIKELY'.
        max_findings: The maximum number of findings to report; 0 = no maximum.
        include_quote: Boolean for whether to display a quote of the detected
            information in the results.
        mime_type: The MIME type of the file. If not specified, the type is
            inferred via the Python standard library's mimetypes module.
    Returns:
        None; the response from the API is printed to the terminal.
    """

    import mimetypes

    # Import the client library.
    import google.cloud.dlp

    # Instantiate a client.
    dlp = google.cloud.dlp_v2.DlpServiceClient()

    # Prepare info_types by converting the list of strings into a list of
    # dictionaries (protos are also accepted).
    if not info_types:
        info_types = ["FIRST_NAME", "LAST_NAME", "EMAIL_ADDRESS"]
    info_types = [{"name": info_type} for info_type in info_types]

    # Prepare custom_info_types by parsing the dictionary word lists and
    # regex patterns.
    if custom_dictionaries is None:
        custom_dictionaries = []
    dictionaries = [
        {
            "info_type": {"name": "CUSTOM_DICTIONARY_{}".format(i)},
            "dictionary": {"word_list": {"words": custom_dict.split(",")}},
        }
        for i, custom_dict in enumerate(custom_dictionaries)
    ]
    if custom_regexes is None:
        custom_regexes = []
    regexes = [
        {
            "info_type": {"name": "CUSTOM_REGEX_{}".format(i)},
            "regex": {"pattern": custom_regex},
        }
        for i, custom_regex in enumerate(custom_regexes)
    ]
    custom_info_types = dictionaries + regexes

    # Construct the configuration dictionary. Keys which are None may
    # optionally be omitted entirely.
    inspect_config = {
        "info_types": info_types,
        "custom_info_types": custom_info_types,
        "min_likelihood": min_likelihood,
        "limits": {"max_findings_per_request": max_findings},
    }

    # If mime_type is not specified, guess it from the filename.
    if mime_type is None:
        mime_guess = mimetypes.MimeTypes().guess_type(filename)
        mime_type = mime_guess[0]

    # Select the content type index from the list of supported types.
    supported_content_types = {
        None: 0,  # "Unspecified"
        "image/jpeg": 1,
        "image/bmp": 2,
        "image/png": 3,
        "image/svg": 4,
        "text/plain": 5,
    }
    content_type_index = supported_content_types.get(mime_type, 0)

    # Construct the item, containing the file's byte data.
    with open(filename, mode="rb") as f:
        item = {"byte_item": {"type": content_type_index, "data": f.read()}}
    # Convert the project id into a full resource id.
    parent = dlp.project_path(project)

    # Call the API.
    response = dlp.inspect_content(parent, inspect_config, item)

    # Print out the results.
    if response.result.findings:
        for finding in response.result.findings:
            try:
                print("Quote: {}".format(finding.quote))
            except AttributeError:
                pass
            print("Info type: {}".format(finding.info_type.name))
            print("Likelihood: {}".format(finding.likelihood))
    else:
        print("No findings.")

Go

import (
	"context"
	"fmt"
	"io"
	"io/ioutil"

	dlp "cloud.google.com/go/dlp/apiv2"
	dlppb "google.golang.org/genproto/googleapis/privacy/dlp/v2"
)

// inspectTextFile inspects a text file at a given filePath, and prints results.
func inspectTextFile(w io.Writer, projectID, filePath string) error {
	// projectID := "my-project-id"
	// filePath := "path/to/image.png"
	ctx := context.Background()

	// Initialize client.
	client, err := dlp.NewClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close() // Closing the client safely cleans up background resources.

	// Gather the resources for the request.
	data, err := ioutil.ReadFile(filePath)
	if err != nil {
		return err
	}

	// Create and send the request.
	req := &dlppb.InspectContentRequest{
		Parent: fmt.Sprintf("projects/%s/locations/global", projectID),
		Item: &dlppb.ContentItem{
			DataItem: &dlppb.ContentItem_ByteItem{
				ByteItem: &dlppb.ByteContentItem{
					Type: dlppb.ByteContentItem_TEXT_UTF8,
					Data: data,
				},
			},
		},
		InspectConfig: &dlppb.InspectConfig{
			InfoTypes: []*dlppb.InfoType{
				{Name: "PHONE_NUMBER"},
				{Name: "EMAIL_ADDRESS"},
				{Name: "CREDIT_CARD_NUMBER"},
			},
			IncludeQuote: true,
		},
	}
	resp, err := client.InspectContent(ctx, req)
	if err != nil {
		return fmt.Errorf("InspectContent: %v", err)
	}

	// Process the results.
	fmt.Fprintf(w, "Findings: %d\n", len(resp.Result.Findings))
	for _, f := range resp.Result.Findings {
		fmt.Fprintf(w, "\tQoute: %s\n", f.Quote)
		fmt.Fprintf(w, "\tInfo type: %s\n", f.InfoType.Name)
		fmt.Fprintf(w, "\tLikelihood: %s\n", f.Likelihood)
	}
	return nil
}

PHP

use Google\Cloud\Dlp\V2\DlpServiceClient;
use Google\Cloud\Dlp\V2\ContentItem;
use Google\Cloud\Dlp\V2\InfoType;
use Google\Cloud\Dlp\V2\InspectConfig;
use Google\Cloud\Dlp\V2\ByteContentItem;
use Google\Cloud\Dlp\V2\ByteContentItem\BytesType;
use Google\Cloud\Dlp\V2\Likelihood;

/** Uncomment and populate these variables in your code */
// $projectId = 'YOUR_PROJECT_ID';
// $filepath = 'path/to/image.png';

// Instantiate a client.
$dlp = new DlpServiceClient();

// Get the bytes of the file
$fileBytes = (new ByteContentItem())
    ->setType(BytesType::IMAGE_PNG)
    ->setData(file_get_contents($filepath));

// Construct request
$parent = $dlp->projectName($projectId);
$item = (new ContentItem())
    ->setByteItem($fileBytes);
$inspectConfig = (new InspectConfig())
    // The infoTypes of information to match
    ->setInfoTypes([
        (new InfoType())->setName('PHONE_NUMBER'),
        (new InfoType())->setName('EMAIL_ADDRESS'),
        (new InfoType())->setName('CREDIT_CARD_NUMBER')
    ])
    // Whether to include the matching string
    ->setIncludeQuote(true);

// Run request
$response = $dlp->inspectContent($parent, [
    'inspectConfig' => $inspectConfig,
    'item' => $item
]);

// Print the results
$findings = $response->getResult()->getFindings();
if (count($findings) == 0) {
    print('No findings.' . PHP_EOL);
} else {
    print('Findings:' . PHP_EOL);
    foreach ($findings as $finding) {
        print('  Quote: ' . $finding->getQuote() . PHP_EOL);
        print('  Info type: ' . $finding->getInfoType()->getName() . PHP_EOL);
        $likelihoodString = Likelihood::name($finding->getLikelihood());
        print('  Likelihood: ' . $likelihoodString . PHP_EOL);
    }
}

Ruby

# project_id   = "Your Google Cloud project ID"
# filename     = "The file path to the file to inspect"
# max_findings = "Maximum number of findings to report per request (0 = server maximum)"

require "google/cloud/dlp"

dlp = Google::Cloud::Dlp.dlp_service
inspect_config = {
  # The types of information to match
  info_types:     [{ name: "PERSON_NAME" }, { name: "PHONE_NUMBER" }],

  # Only return results above a likelihood threshold (0 for all)
  min_likelihood: :POSSIBLE,

  # Limit the number of findings (0 for no limit)
  limits:         { max_findings_per_request: max_findings },

  # Whether to include the matching string in the response
  include_quote:  true
}

# The item to inspect
file = File.open filename, "rb"
item_to_inspect = { byte_item: { type: :BYTES_TYPE_UNSPECIFIED, data: file.read } }

# Run request
parent = "projects/#{project_id}/locations/global"
response = dlp.inspect_content parent:         parent,
                               inspect_config: inspect_config,
                               item:           item_to_inspect

# Print the results
if response.result.findings.empty?
  puts "No findings"
else
  response.result.findings.each do |finding|
    puts "Quote:      #{finding.quote}"
    puts "Info type:  #{finding.info_type.name}"
    puts "Likelihood: #{finding.likelihood}"
  end
end

C#


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;
using Google.Protobuf;
using static Google.Cloud.Dlp.V2.ByteContentItem.Types;

public class DlpInspectFile
{
    public static IEnumerable<Finding> InspectFile(string projectId, string filePath, BytesType fileType)
    {
        // Instantiate a client.
        var dlp = DlpServiceClient.Create();

        // Get the bytes from the file.
        ByteString fileBytes;
        using (Stream f = new FileStream(filePath, FileMode.Open))
        {
            fileBytes = ByteString.FromStream(f);
        }

        // Construct a request.
        var request = new InspectContentRequest
        {
            Parent = new LocationName(projectId, "global").ToString(),
            Item = new ContentItem
            {
                ByteItem = new ByteContentItem()
                {
                    Data = fileBytes,
                    Type = fileType
                }
            },
            InspectConfig = new InspectConfig
            {
                // The info types of information to match
                InfoTypes =
                {
                    new InfoType { Name = "PHONE_NUMBER" },
                    new InfoType { Name = "EMAIL_ADDRESS" },
                    new InfoType { Name = "CREDIT_CARD_NUMBER" }
                },
                // The minimum likelihood before returning a match
                MinLikelihood = Likelihood.Unspecified,
                // Whether to include the matching string
                IncludeQuote = true,
                Limits = new InspectConfig.Types.FindingLimits
                {
                    // The maximum number of findings to report per request
                    // (0 = server maximum)
                    MaxFindingsPerRequest = 0
                }
            }
        };

        // Execute request
        var response = dlp.InspectContent(request);

        // Inspect response
        var findings = response.Result.Findings;
        if (findings.Any())
        {
            Console.WriteLine("Findings:");
            foreach (var finding in findings)
            {
                Console.WriteLine($"Quote: {finding.Quote}");
                Console.WriteLine($"InfoType: {finding.InfoType}");
                Console.WriteLine($"Likelihood: {finding.Likelihood}");
            }
        }
        else
        {
            Console.WriteLine("No findings.");
        }
        return findings;
    }
}

演習

content.inspect のリファレンス ページの API Explorer で、このページの各例を実際に試すことができます。または、独自の画像で試すこともできます。

API Explorer に移動

次のステップ