python-3.x - 谷歌云函数中的 Python 问题
问题描述
我已将以下代码放入 Python 3.8 Google Cloud Function 中。我所做的只是更新“Hello-World”示例并在顶部添加导入行。我很困惑,因为我这周刚开始使用 GCP,我认为在存储桶中建立连接会更容易。我在日志中看到的错误位于代码下方。
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!'
错误消息是:
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…
解决方案
mkdir ooo
cd ooo
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!'
cat requirements.txt
google-cloud-storage
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'
一切都按预期工作。
推荐阅读
- keras - keras LSTM 中的训练损失为 nan
- javascript - 我添加了三个输入数字的最大值并且它有效但是如何添加递增递减按钮而不丢失最大值?
- opengl - 我们可以输入随机值来使用 PyOpenGL 在立方体上渲染颜色吗?
- r - 如何根据日期差异对数据框进行子集化?
- c# - 我可以在不使用反射的情况下获取类中的字段或属性的 PropertyInfo 吗?
- javascript - React 应用程序的 Webpack 再生器编译问题
- reactjs - 如何将文本字段设置为禁用,并且在键入时将再次启用?
- drake - 使用 autoDiffToGradientMatrix 的一些问题
- android - 未从通知托盘中获取捆绑包中的 Pubnub 有效负载。当应用程序被杀死时
- excel - 我们能否将 sharepoint 中的电子表格连接到 Einstein Analytics