简体   繁体   English

谷歌云函数中的 Python 问题

[英]Problem with Python inside a Google Cloud Function

I have put the following code into a Python 3.8 Google Cloud Function.我已将以下代码放入 Python 3.8 Google Cloud Function 中。 All I did was update the "Hello-World" example and added the import line at the top.我所做的只是更新“Hello-World”示例并在顶部添加导入行。 I'm so confused as I literally just started to use GCP this week and I thought it'd be easier to set up a connection into a bucket.我很困惑,因为我这周刚开始使用 GCP,我认为在存储桶中建立连接会更容易。 The error I'm seeing in the logs is below the code.我在日志中看到的错误在代码下方。

from google.cloud import storage
def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        return f'Hello World!'

The error messages are:错误消息是:

2020-11-20 15:22:20.289 MSTCloud FunctionsUpdateFunctionus-central1:faautoingestiongcp*******@gmail.com {@type: type.googleapis.com/google.cloud.audit.AuditLog, authenticationInfo: {…}, methodName: google.cloud.functions.v1.CloudFunctionsService.UpdateFunction, resourceName: projects/reference-rain-293921/locations/us-central1/functions/faautoingestiongcp, serviceName: cloudfunctions.googleapis.com, s…

{@type: type.googleapis.com/google.cloud.audit.AuditLog, authenticationInfo: {…}, methodName: google.cloud.functions.v1.CloudFunctionsService.UpdateFunction, resourceName: projects/reference-rain-293921/locations/us-central1/functions/faautoingestiongcp, serviceName: cloudfunctions.googleapis.com, s…
  1. mkdir ooo

  2. cd ooo

  3. cat main.py

from google.cloud import storage
def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        return f'Hello World!'


  1. cat requirements.txt
google-cloud-storage
  1. gcloud functions deploy hello_world --runtime python37 --trigger-http --allow-unauthenticated
entryPoint: hello_world
httpsTrigger:
  url: https://us-central1-xxxx.cloudfunctions.net/hello_world
ingressSettings: ALLOW_ALL
labels:
  deployment-tool: cli-gcloud
name: projects/xxxxxxx/locations/us-central1/functions/hello_world
runtime: python37

.....................
us: ACTIVE
timeout: 60s
updateTime: '2020-11-21T12:50:41.095Z'
versionId: '2'

Everything worked as expected.一切都按预期进行。

Quickstart: Using the gcloud Command-Line Tool快速入门:使用 gcloud 命令行工具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM