python - 来自 google 的 pub_sub 操作示例代码错误,缺少 1 个必需的位置参数 : 'callback'

标签 python google-cloud-dlp

我正在一个大型查询表上设置 Google DLP 扫描,以查找可识别的个人信息。我一直在为此研究谷歌示例代码,但代码的 pub/sub 元素遇到了问题

这是一个调用 google dlp 的 python google 云函数,使用 google 示例 here使用方法inspect_bigquery。

...

actions = [{
    'pub_sub': {'topic': '{}/topics/{}'.format(parent, topic_id)},
    'save_findings': {
        'output_config': {
                'table': {
                    'project_id': project,
                    'dataset_id': dataset_id,
                    'table_id': table_id + '_inspection_results',
                }
            }
    },
}]

...

subscriber = google.cloud.pubsub.SubscriberClient()
subscription_path = subscriber.subscription_path(
    project, subscription_id)
#    subscription = subscriber.subscribe(subscription_path, callback)
subscription = subscriber.subscribe(subscription_path)

...

def callback(message):
    try:
        if (message.attributes['DlpJobName'] == operation.name):
            # This is the message we're looking for, so acknowledge it.
            message.ack()

            # Now that the job is done, fetch the results and print them.
            job = dlp.get_dlp_job(operation.name)
            if job.inspect_details.result.info_type_stats:
                for finding in job.inspect_details.result.info_type_stats:
                    print('Info type: {}; Count: {}'.format(
                        finding.info_type.name, finding.count))
            else:
                print('No findings.')

            # Signal to the main thread that we can exit.
            job_done.set()
        else:
            # This is not the message we're looking for.
            message.drop()
    except Exception as e:
        # Because this is executing in a thread, an exception won't be
        # noted unless we print it manually.
        print(e)
        raise

# Register the callback and wait on the event.
subscription.open(callback)
finished = job_done.wait(timeout=timeout)
if not finished:
    print('No event received before the timeout. Please verify that the '
          'subscription provided is subscribed to the topic provided.')

我遇到了两个错误,当我将订阅方法保留为订阅路径时,它会出现 TypeError: subscribe() Missing 1 requiredpositional argument: 'callback' 错误。

当我将回调放入订阅方法时,它失败了 函数执行花费了 60002 毫秒,完成状态为:“超时” 超时之前未收到任何事件。请验证所提供的订阅是否订阅了所提供的主题。

但是,保存结果操作确实有效,几秒钟后我就可以在 bigquery 中看到结果。

谢谢

最佳答案

几件事: 1. 如您所知,如果您不想生成 table_id,则可以将 table_id 留空。

但对于你的实际问题:

  • 您是否偶然在有执行截止日期的 Cloud Functions 中运行此程序? (https://cloud.google.com/functions/docs/concepts/exec#timeout)
  • 如果是,您实际上希望云函数通过触发器( https://cloud.google.com/functions/docs/calling/pubsub )订阅发布/订阅,而不是在代码中以避免超时。这里有一个特定的 DLP 解决方案指南 https://cloud.google.com/solutions/automating-classification-of-data-uploaded-to-cloud-storage#create_pubsub_topic_and_subscription

    有帮助吗?

    关于python - 来自 google 的 pub_sub 操作示例代码错误,缺少 1 个必需的位置参数 : 'callback' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54094780/

    相关文章:

    google-bigquery - 如何使用 DLP 扫描 BigQuery 表以查找敏感数据?

    google-api - Google Cloud 数据丢失防护 (DLP) 扫描 GCS 中的 .parquet 文件

    python - 如何将字符串转换为 datetime.timedelta()?

    python - Pandas 将字符串转换为整数

    python - 将字符串计算为值

    python - 如何为 python 配置谷歌云平台数据丢失预防客户端库以在 SSL 代理后面工作?

    google-cloud-platform - 如何在防数据泄露(谷歌云平台)去识别模板中使用自定义信息类型?

    python - 在python中使用正则表达式从文本中删除html标签

    python - 配置根记录器的格式