Google pubsub进入HTTP触发的云功能吗? [英] Google pubsub into HTTP triggered cloud function?

查看:56
本文介绍了Google pubsub进入HTTP触发的云功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以触发pubsub消息来响应HTTP云功能?

Is it possible to trigger an HTTP cloud function in response to a pubsub message?

在编辑订阅时,Google可以将消息推送到HTTPS端点,但是出于滥用的原因,人们必须能够证明您拥有域才能执行此操作,当然您不能证明您拥有Google自己的*.cloudfunctions.net域,这是它们部署所在的域.

When editing a subscription, google makes it possible to push the message to an HTTPS endpoint, but for abuse reasons one has to be able to prove that you own the domain in order to do this, and of course you can't prove that you own google's own *.cloudfunctions.net domain which is where they get deployed.

我要订阅的特定主题是公共主题projects/pubsub-public-data/topics/taxirides-realtime.答案可能是使用后台功能,而不是HTTP触发的,但是由于不同的原因,该方法不起作用:

The particular topic I'm trying to subscribe to is a public one, projects/pubsub-public-data/topics/taxirides-realtime. The answer might be use a background function rather than HTTP triggered, but that doesn't work for different reasons:

gcloud functions deploy echo --trigger-resource projects/pubsub-public-data/topics/taxirides-realtime --trigger-event google.pubsub.topic.publish ERROR: gcloud crashed (ArgumentTypeError): Invalid value 'projects/pubsub-public-data/topics/taxirides-realtime': Topic must contain only Latin letters (lower- or upper-case), digits and the characters - + . _ ~ %. It must start with a letter and be from 3 to 255 characters long.

gcloud functions deploy echo --trigger-resource projects/pubsub-public-data/topics/taxirides-realtime --trigger-event google.pubsub.topic.publish ERROR: gcloud crashed (ArgumentTypeError): Invalid value 'projects/pubsub-public-data/topics/taxirides-realtime': Topic must contain only Latin letters (lower- or upper-case), digits and the characters - + . _ ~ %. It must start with a letter and be from 3 to 255 characters long.

这似乎表明只允许在我拥有的主题上使用,这是一个奇怪的限制.

This seems to indicate this is only permitted on topics I own, which is a strange limitation.

推荐答案

可以从发布/订阅主题发布到云函数.我正在寻找一种将消息从项目A中的主题发布到项目B中的函数的方法.使用常规主题触发器无法实现,但是使用http-trigger可以实现.要遵循的总体步骤:

It is possible to publish from a pub/sub topic to a cloud function. I was looking for a way to publish messages from a topic in project A to a function in project B. This was not possible with a regular topic trigger, but it is possible with http-trigger. Overall steps to follow:

  • 在项目B中创建一个由http触发的函数.
  • 在项目A中创建一个主题.
  • 在项目A中的该主题上创建推送订阅.
  • 域验证

推送订阅

在这里,我们必须填写三件事:运行该功能的端点,受众和服务帐户.

Here we have to fill in three things: the endpoint, the audience and the service account under which the function runs.

  • Push Endpoint: https://REGION-PROJECT_ID.cloudfunctions.net/FUNC_NAME/ (slash at end)
  • Audience: https://REGION-PROJECT_ID.cloudfunctions.net/FUNC_NAME (no slash at end)
  • Service Account: Choose a service account under which you want to send the actual message. Be sure the service account has the "roles/cloudfunctions.invoker" role on the cloud function that you are sending the messages to. Since november 2019, http-triggered functions are automatically secured because AllUsers is not set by default. Do not set this property unless you want your http function to be public!

域验证

现在,您可能由于错误而无法保存您的订阅,这是因为端点未通过Google验证.因此,您需要在以下位置将功能URL列入白名单: https://console. cloud.google.com/apis/credentials/domainverification?project=PROJECT_NAME .

Now you probably can't save your subscription because of an error, that is because the endpoint is not validated by Google. Therefore you need to whitelist the function URL at: https://console.cloud.google.com/apis/credentials/domainverification?project=PROJECT_NAME.

按照此步骤操作还将带您进入Google Search Console,在此您还需要验证您是否拥有该端点.可悲的是,在撰写本文时,该过程无法实现自动化.

Following this step will also bring you to the Google Search Console, where you would also need to verify you own the endpoint. Sadly, at the time of writing this process cannot be automated.

  • 接下来,我们需要在您的云函数中添加以下内容(python示例),以允许Google验证该函数:

  • Next we need to add something in the lines of the following (python example) to your cloud function to allow google to verify the function:

if request.method == 'GET':
    return '''
        <html>
            <head>
                <meta name="google-site-verification" content="{token}" />
            </head>
            <body>
            </body>
        </html>
    '''.format(token=config.SITE_VERIFICATION_CODE)

瞧!现在应该可以了.

来源: https://github.com/googleapis/nodejs-pubsub/issues/118#issuecomment-379823198 https://cloud.google.com/functions/docs/calling/http

这篇关于Google pubsub进入HTTP触发的云功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆