使用Google Cloud DLP API时,如何获取扫描文件的位置



我正在扫描云存储存储桶中的嵌套目录。尽管我有inclage_quote,但结果不包含匹配的值(QUOTE)。另外,如何获得具有匹配和匹配值的文件的名称?我正在使用Python。这就是我到目前为止的。如您所见,API找到了匹配,但是我没有得到有关字样(和文件)标记的详细信息。

inspect_job = {
  'inspect_config': {
      'info_types': info_types,
      'min_likelihood': MIN_LIKELIHOOD,
      'include_quote': True,
      'limits': {
          'max_findings_per_request': MAX_FINDINGS
      },
  },
  'storage_config': {
      'cloud_storage_options': {
          'file_set': {
              'url':
                  'gs://{bucket_name}/{dir_name}/**'.format(
                      bucket_name=STAGING_BUCKET, dir_name=DIR_NAME)
          }
      }
  }

operation = dlp.create_dlp_job(parent, inspect_job)
dlp.get_dlp_job(operation.name)

这是结果:

result {
processed_bytes: 64
total_estimated_bytes: 64
info_type_stats {
  info_type {
    name: "EMAIL_ADDRESS"
  }
  count: 1
}
info_type_stats {
  info_type {
    name: "PHONE_NUMBER"
  }
  count: 1
}
info_type_stats {
  info_type {
    name: "FIRST_NAME"
  }
  count: 2
}

您需要遵循https://cloud.google.com/dlp/docs/inspecting-storage中的"检索检查结果"部分,并指定"保存结果"操作https://cloud。google.com/dlp/docs/reference/rest/v2/inspectjobconfig#savefindings

我认为您没有得到报价值,因为您的InspectConfig不太正确:根据位于https://cloud.google.com/dlp/docs/reference/rest/rest/v2/inspectconfig的文档,您应该设置

  "includeQuote": true 

编辑:添加有关获取文件的信息:之后以下示例:https://cloud.google.com/solutions/automation-classification-of-data-uploaded-to-cloud-storage

云函数的代码resolve_dlp从作业详细信息中获取文件名

def resolve_DLP(data, context):
...
job = dlp.get_dlp_job(job_name)
...
file_path = (
      job.inspect_details.requested_options.job_config.storage_config
      .cloud_storage_options.file_set.url)
  file_name = os.path.basename(file_path)
...

编辑2:现在我看到使用'include_quote'的最新python api客户端:作为dict键....所以不是...

编辑3:摘自Python API代码:

message Finding {
  // The content that was found. Even if the content is not textual, it
  // may be converted to a textual representation here.
  // Provided if `include_quote` is true and the finding is
  // less than or equal to 4096 bytes long. If the finding exceeds 4096 bytes
  // in length, the quote may be omitted.
  string quote = 1;

因此,较小的文件可能会产生报价

rondo,感谢您的输入。我相信您提到的云存储示例仅扫描每个作业的一个文件。它不使用SaveFindings对象。

乔什,你是对的。似乎需要将输出引导到BigQuery或Pub/sub才能查看完整的结果。

来自https://cloud.google.com/dlp/docs/inspecting-storage#retrieving-inspection-results:

对于完整的检查工作结果,您有两个选择。根据您选择的动作,检查工作是:

在指定的表中保存到bigquery(保存程序对象)。在查看或分析结果之前,首先要确保通过使用projects.dlpjobs.get方法完成工作,如下所述。请注意,您可以使用Outputschema对象指定用于存储发现的模式。发布到Cloud Pub/sub主题(PublishToPubSub对象)。该主题必须给出了运行DLPJOB发送通知的云DLP服务帐户的发布权。

我通过修改解决方案如何使用DLP扫描BigQuery表来工作?

这是我的最终工作脚本:

import google.cloud.dlp
dlp = google.cloud.dlp.DlpServiceClient()
inspect_job_data = {
    'storage_config': {
      'cloud_storage_options': {
          'file_set': {
              'url':
                  'gs://{bucket_name}/{dir_name}/**'.format(
                      bucket_name=STAGING_BUCKET, dir_name=DIR_NAME)
          }
      }
  },
'inspect_config': {
    'include_quote': include_quote,
    'info_types': [
        {'name': 'ALL_BASIC'},
    ],
},
'actions': [
    {
        'save_findings': {
            'output_config':{
                'table':{
                    'project_id': GCP_PROJECT_ID,
                    'dataset_id': DATASET_ID,
                    'table_id': '{}_DLP'.format(TABLE_ID)
                }
            }
        },
    },
]

}

operation = dlp.create_dlp_job(parent=dlp.project_path(GCP_PROJECT_ID), 
inspect_job=inspect_job_data)

最新更新