簡體   English   中英

谷歌雲函數在函數視圖日志中導入錯誤

[英]google cloud function import errors in function view log

我在 python 運行時環境中創建了一個由谷歌雲 pubsub 觸發的谷歌雲函數,我還添加了需求依賴項,但是當我運行該函數時它工作正常但它記錄了一些導入錯誤所以我該如何解決這個問題。

蟒蛇代碼:

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)

需求.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'

您可以抑制日志消息:

import logging

logging.getLogger('googleapicliet.discovery_cache').setLevel(logging.ERROR)`

或者在使用discovery.build()時設置cache_discovery=False

self.client = discovery.build(
    service_name,
    api_version,
    discoveryServiceUrl=discovery_url,
    credentials=scoped_credentials,
    cache_discovery=False,
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM