Google Cloud Function + Python

迪迪埃·简·查尔斯

向右跳...

我在Google Cloud Function中具有以下python代码

main.py

import flask, requests

def gce_test(request):
  test = {"data": {}, "meta": {}, "error": {}}

  metadata_server = "http://metadata/computeMetadata/v1/instance/"
  metadata_flavor = {'Metadata-Flavor' : 'Google'}

  gce_id = requests.get(metadata_server + 'id', headers = metadata_flavor).text
  gce_name = requests.get(metadata_server + 'hostname', headers = metadata_flavor).text
  gce_machine_type = requests.get(metadata_server + 'machine-type', headers = metadata_flavor).text

  test["data"]["gce_id"] = gce_id
  test["data"]["gce_name"] = gce_name
  test["data"]["gce_machine_type"] = gce_machine_type

  test["meta"]["metadata_server"] = metadata_server
  test["meta"]["metadata_flavor"] = metadata_flavor
  test["meta"]["metadata_server_full"] = metadata_server + 'id'

  test["meta"]['generator'] = 'google-cloud-function'

  return flask.jsonify(test)

我现在面临的问题是,我得到一个404 page not found用于gce_idgce_machine_typegce_name这意味着请求metadata_server未取得...

我的问题是如何向metadata_server发出HTTP Get请求,并将实例名称作为参数传递给它...这样做可行吗?

星期五推

您当前正在向GCE元数据服务器URL发出请求。Cloud Functions在GAE Standard之上运行,因此最终使用http://metadata.google.internal网址。我提供了一个与当前云功能配合使用以记录项目ID的示例。

import flask, requests

def gce_test(request):
  test = {"data": {}, "meta": {}, "error": {}}

  metadata_server = "http://metadata.google.internal/computeMetadata/v1/project/project-id"
  metadata_flavor = {'Metadata-Flavor' : 'Google'}


  project = requests.get(metadata_server, headers = metadata_flavor).text
  test["data"]["gce_id"] = project
  return flask.jsonify(test)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Cloud Function(Python)写入Google Cloud Storage

从本地Python函数到Google Cloud Function

使用服务帐户从python从python调用Google Cloud Function

从Google Cloud Function python3.7连接到Google Cloud SQL

从Google Cloud Function(Python)将新文件写入Google Cloud Storage存储桶

Google Cloud Function:支持Google Cloud KMS

从Google Cloud Function发出POST请求时Python超时

通过对等方重置Python Google Cloud Function Connection

Google Storage // Cloud Function // Python 修改 Bucket 中的 CSV 文件

Google Cloud Function无法安装python软件包

如何在Python Google Cloud Function中以特定状态返回

在Firebase托管中使用Python Google Cloud Function

Google Cloud Function中针对Python的仅开发要求

使用 Python 3 使用 Google Cloud Function 重命名对象

部署使用 google-api-python-client 库作为 Google Cloud Function 的 Python 脚本

使用Python和SQLAlchemy从Google Cloud Function连接到Cloud SQL

无法调用 Google Cloud Function

Google Cloud Function部署失败

限制对Google Cloud Function的访问

未能创建 Google Cloud Function

将 Google Calendar API 与 Python 3.7 Google Cloud Function 结合使用

Google Cloud Functions中的Python

树莓派上的Google Cloud Python

记录 Python 和 Google Cloud

Python中的Google Cloud Function:TypeError:post_tweet()缺少1个必需的位置参数:“ context”

在本地测试(Python)Google Cloud Function时应用程序上下文错误

如何使用python脚本提取多部分zip文件以在Google Cloud Function中运行

通过Python Google Cloud Function发送电子邮件的最佳方法是什么?

如何使WebSockets连接到Google Cloud Function?