我已经在python运行时环境中创建了一个由google cloud pubsub触发的google cloud函数,并且我还添加了需求依赖项,但是当我运行该函数时,它可以正常运行,但是记录了一些导入错误,所以我该如何解决这个问题问题。
python代码:
import base64
import json
from google.oauth2 import service_account
from googleapiclient import discovery
from googleapiclient.errors import HttpError
class IotDevice:
def __init__(self, service_account_json, project_id, cloud_region, registry_id, device_id):
api_scopes = ['https://www.googleapis.com/auth/cloud-platform']
api_version = 'v1'
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
service_name = 'cloudiotcore'
credentials = service_account.Credentials.from_service_account_file(service_account_json)
scoped_credentials = credentials.with_scopes(api_scopes)
discovery_url = '{}?version={}'.format(discovery_api, api_version)
self.client = discovery.build(service_name, api_version, discoveryServiceUrl=discovery_url,
credentials=scoped_credentials)
self.project_id = project_id
self.cloud_region = cloud_region
self.registry_id = registry_id
self.device_id = device_id
def set_config(self, version, config):
device_path = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(
self.project_id, self.cloud_region, self.registry_id, self.device_id)
config_body = {
'versionToUpdate': version,
'binaryData': base64.urlsafe_b64encode(config.encode('utf-8')).decode('ascii')
}
try:
return self.client.projects().locations().registries().devices().modifyCloudToDeviceConfig(
name=device_path, body=config_body).execute()
except HttpError as e:
print(e.content.decode())
def get_config_versions(self):
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
self.project_id, self.cloud_region, self.registry_id)
device_name = '{}/devices/{}'.format(registry_name, self.device_id)
devices = self.client.projects().locations().registries().devices()
configs = devices.configVersions().list(name=device_name).execute().get('deviceConfigs', [])
return configs[0].get('version')
# [END iot_get_device_configs]
def main(data, context):
if data is not None:
payload = base64.b64decode(data['data']).decode('utf-8')
payload = json.loads(payload)
attributes = data['attributes']
project_id = attributes['projectId']
location = attributes['deviceRegistryLocation']
registry_id = attributes['deviceRegistryId']
device_id = attributes['deviceId']
device = IotDevice('./service_account.json', project_id, location, registry_id, device_id)
config_version = device.get_config_versions()
value = payload["value"]
thresold = payload["thresold"]
if value > thresold:
config = json.dumps({'action': 'ON'})
else:
config = json.dumps({'action': 'OFF'})
device.set_config(version=config_version, config=config)
requirement.txt
google-api-python-client==1.7.8
google-auth==1.6.2
main 512547093010642 Function execution started D main 512547093010642
main 512547093010642 file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth E main 512547093010642
main 512547093010642 Traceback (most recent call last): E main 512547093010642
main 512547093010642 File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect E main 512547093010642
main 512547093010642 from google.appengine.api import memcache E main 512547093010642
main 512547093010642 ModuleNotFoundError: No module named 'google.appengine' E main 512547093010642
main 512547093010642 {"insertId":"000005-70ff3337-da0f-440d-8fa3-d520879375ef","resource":{"type":"cloud_function","labels":{"region":"us-central1","function_name":"main","project_id":"robot-11"}},"timestamp":"2019-04-17T05:23:53.316Z","severity":"ERROR","labels":{"execution_id":"512547093010642"},"logName":"projects/ro… E main 512547093010642
main 512547093010642 During handling of the above exception, another exception occurred: E main 512547093010642
main 512547093010642 {"insertId":"000007-70ff3337-da0f-440d-8fa3-d520879375ef","resource":{"type":"cloud_function","labels":{"function_name":"main","project_id":"robot-11","region":"us-central1"}},"timestamp":"2019-04-17T05:23:53.316Z","severity":"ERROR","labels":{"execution_id":"512547093010642"},"logName":"projects/ro… E main 512547093010642
main 512547093010642 Traceback (most recent call last): E main 512547093010642
main 512547093010642 File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module> E main 512547093010642
main 512547093010642 from oauth2client.contrib.locked_file import LockedFile E main 512547093010642
main 512547093010642 ModuleNotFoundError: No module named 'oauth2client' E main 512547093010642
main 512547093010642 {"insertId":"000012-70ff3337-da0f-440d-8fa3-d520879375ef","resource":{"type":"cloud_function","labels":{"project_id":"robot-11","region":"us-central1","function_name":"main"}},"timestamp":"2019-04-17T05:23:53.316Z","severity":"ERROR","labels":{"execution_id":"512547093010642"},"logName":"projects/ro… E main 512547093010642
main 512547093010642 During handling of the above exception, another exception occurred: E main 512547093010642
main 512547093010642 {"insertId":"000014-70ff3337-da0f-440d-8fa3-d520879375ef","resource":{"type":"cloud_function","labels":{"project_id":"robot-11","region":"us-central1","function_name":"main"}},"timestamp":"2019-04-17T05:23:53.316Z","severity":"ERROR","labels":{"execution_id":"512547093010642"},"logName":"projects/ro… E main 512547093010642
main 512547093010642 Traceback (most recent call last): E main 512547093010642
main 512547093010642 File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module> E main 512547093010642
main 512547093010642 from oauth2client.locked_file import LockedFile E main 512547093010642
main 512547093010642 ModuleNotFoundError: No module named 'oauth2client' E main 512547093010642
main 512547093010642 {"insertId":"000019-70ff3337-da0f-440d-8fa3-d520879375ef","resource":{"type":"cloud_function","labels":{"project_id":"robot-11","region":"us-central1","function_name":"main"}},"timestamp":"2019-04-17T05:23:53.316Z","severity":"ERROR","labels":{"execution_id":"512547093010642"},"logName":"projects/ro… E main 512547093010642
main 512547093010642 During handling of the above exception, another exception occurred: E main 512547093010642
main 512547093010642 {"insertId":"000021-70ff3337-da0f-440d-8fa3-d520879375ef","resource":{"type":"cloud_function","labels":{"function_name":"main","project_id":"robot-11","region":"us-central1"}},"timestamp":"2019-04-17T05:23:53.316Z","severity":"ERROR","labels":{"execution_id":"512547093010642"},"logName":"projects/ro… E main 512547093010642
main 512547093010642 Traceback (most recent call last): E main 512547093010642
main 512547093010642 File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect E main 512547093010642
main 512547093010642 from . import file_cache E main 512547093010642
main 512547093010642 File "/env/local/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module> E main 512547093010642
main 512547093010642 'file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth') E main 512547093010642
main 512547093010642 ImportError: file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth E main 512547093010642
main 512547093010642 URL being requested: GET https://cloudiot.googleapis.com/$discovery/rest?version=v1 I main 512547093010642
main 512547093010642 URL being requested: GET https://cloudiot.googleapis.com/v1/projects/robot-11/locations/us-central1/registries/tour-registry/devices/fan/configVersions?alt=json I main 512547093010642
main 512547093010642 URL being requested: POST https://cloudiot.googleapis.com/v1/projects/robot-11/locations/us-central1/registries/tour-registry/devices/fan:modifyCloudToDeviceConfig?alt=json I main 512547093010642
main 512547093010642 Function execution took 627 ms, finished with status: 'ok'
答案 0 :(得分:0)
您可以隐藏日志消息:
import logging
logging.getLogger('googleapicliet.discovery_cache').setLevel(logging.ERROR)`
或在使用cache_discovery=False
时设置discovery.build()
:
self.client = discovery.build(
service_name,
api_version,
discoveryServiceUrl=discovery_url,
credentials=scoped_credentials,
cache_discovery=False,
)