【发布时间】:2020-10-22 12:47:06
【问题描述】:
我正在创建一个 Google Cloud Function 来启动一个 Compute VM 实例。我指的是有关此主题的文档和几个 SO 答案-例如 Using gcloud cli within a cloud function 和 https://stackoverflow.com/a/61343478/6352160
这里是云函数:
import base64
from googleapiclient import discovery
from google.auth import compute_engine
def hello_pubsub(event, context):
"""Triggered from a message on a Cloud Pub/Sub topic.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
print(pubsub_message)
#credentials = GoogleCredentials.get_application_default()
credentials = compute_engine.Credentials()
service = discovery.build('compute', 'v1',credentials=credentials)
# Project ID for this request.
project = 'Project name'
# The name of the zone for this request.
zone = 'zone'
# Name of the instance resource to start.
instance = 'instance-name'
try:
request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()
except Exception as e:
print(e)
print('VM Instance started')
需求标签是:
google-api-python-client
当我运行这个函数时,我得到以下错误
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth TestCloudFunction lt7vm36u2i1w
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w Traceback (most recent call last): TestCloudFunction lt7vm36u2i1w
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module> TestCloudFunction lt7vm36u2i1w
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w from oauth2client.contrib.locked_file import LockedFile TestCloudFunction lt7vm36u2i1w
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file' TestCloudFunction lt7vm36u2i1w
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w TestCloudFunction lt7vm36u2i1w
E 2020-07-02T01:06:10.808Z TestCloudFunction lt7vm36u2i1w During handling of the above exception, another exception occurred: TestCloudFunction lt7vm36u2i1w
在初始化计算 API 时,它看起来像一个身份验证错误。 谁能告诉我如何解决这个问题?
更新:我注意到尽管出现了这个错误,但代码仍然成功地启动了 Compute VM 实例。但是我仍然想知道为什么会显示此错误以及如何解决此问题。
【问题讨论】:
标签: python google-cloud-platform google-cloud-functions google-compute-engine google-cloud-sdk