【问题标题】:Passing Variables To Google Cloud Functions将变量传递给 Google Cloud 函数
【发布时间】:2019-02-13 11:32:28
【问题描述】:

我刚刚在 Beta Python 3.7 运行时中使用 HTTP 触发器完成了 Google Cloud Function 的编写。现在我试图弄清楚在调用它时如何将字符串变量传递给我的函数。我已经阅读了文档,但我没有找到任何关于此的内容。

我的触发器类似于:

https://us-central1-*PROJECT_ID*.cloudfunctions.net/*FUNCTION_NAME*

我是否误解了 Cloud Functions 的工作原理?您甚至可以将变量传递给它们吗?

【问题讨论】:

    标签: python-3.x google-cloud-platform google-cloud-functions


    【解决方案1】:
    推荐的答案 Google Cloud

    将变量传递给函数的方式与将变量传递给任何 URL 的方式相同:

    1。通过带有查询参数的GET

    def test(request):
        name = request.args.get('name')
        return f"Hello {name}"
    
    $ curl -X GET https://us-central1-<PROJECT>.cloudfunctions.net/test?name=World
    Hello World
    

    2。通过带有表单的POST

    def test(request):
        name = request.form.get('name')
        return f"Hello {name}"
    
    $ curl -X POST https://us-central1-<PROJECT>.cloudfunctions.net/test -d "name=World"
    Hello World
    

    3。通过带有 JSON 的POST

    def test(request):
        name = request.get_json().get('name')
        return f"Hello {name}"
    
    $ curl -X POST https://us-central1-<PROJECT>.cloudfunctions.net/test -d '{"name":"World"}'
    Hello World
    

    更多细节可以在这里找到:https://cloud.google.com/functions/docs/writing/http

    【讨论】:

    • 好东西。谢谢
    • 如果系统启动多个实例,每个实例都运行云功能以响应负载,这是否有效 - 我可以确定它们都可以处理至少一次设置参数/变量的请求吗?
    • @nsandersen 每个请求将仅由单个实例处理。
    • 如何将参数/变量传递给所有实例?让函数将它写入共享存储并让它定期检查它,以便接收它的实例将它写入磁盘,其余的稍后再取它?
    • 您能描述一下用例是什么吗?实例是短暂的,并且不会维护状态,除非您将其存储在外部(例如,在数据库中)。
    猜你喜欢
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2011-01-18
    • 2015-05-02
    相关资源
    最近更新 更多