Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用本地依赖项部署python Google云函数_Python_Python 3.x_Google Cloud Platform_Google Cloud Functions_Google Cloud Python - Fatal编程技术网

如何使用本地依赖项部署python Google云函数

如何使用本地依赖项部署python Google云函数,python,python-3.x,google-cloud-platform,google-cloud-functions,google-cloud-python,Python,Python 3.x,Google Cloud Platform,Google Cloud Functions,Google Cloud Python,我有一些googlecloud函数,它们之间有共同的代码。 因此,我选择制作一个包,并将其放置在每个文件夹中,以实现我的功能。 以以下目录结构结束 google-cloud-function ├── main.py ├── requirements.txt └── helpers ├── tools.py └── __init__.py 我根据文档制作了我的包助手。 我的requirements.txt如下所示: firebase-admin==4.0.0 google-clo

我有一些
googlecloud函数
,它们之间有共同的代码。 因此,我选择制作一个包,并将其放置在每个文件夹中,以实现我的功能。 以以下目录结构结束

google-cloud-function
├── main.py
├── requirements.txt
└── helpers
    ├── tools.py
    └── __init__.py
我根据文档制作了我的包
助手
。 我的
requirements.txt
如下所示:

firebase-admin==4.0.0
google-cloud-storage==1.26.0
google-cloud-firestore==1.6.2
该功能可以正确部署,但在触发时会在没有日志的情况下崩溃。
这是导入语句

from helpers.tools import Event, CleanUpUser
我如何适当地部署这个
python
googlecloud功能

编辑:
main.py

import firebase_admin
from google.cloud import firestore

from helpers.tools import Event

firebase_admin.initialize_app(options={'databaseURL': 'https://database_name.firebaseio.com/'})
firestore_db = firestore.Client()

EVENTS_NAME = 'database-events'


def user_cleanup(data, context):
    if Event(EVENTS_NAME).check_event(context):
        return 'Done'
    user_id = data['uid']
    user_ref = firestore_db.document(f'user_data/{user_id}')
    return 'Done'
import logging
import sys
import traceback
from datetime import datetime

import firebase_admin
import pytz
from google.cloud import firestore


class Event:
    def __init__(self, database_name: str):
        init_logger()
        firebase_admin.initialize_app(options={'databaseURL': 'https://{database_name}.firebaseio.com/'})

    def check_event(self, context):
        event_id = context.event_id.replace('/', '--')
        timestamp = context.timestamp
        timestamp = timestamp[0:23] if len(timestamp) >= 24 else timestamp[0:19]
        try:
            millis = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%f').replace(tzinfo=pytz.utc).timestamp()*1000
        except ValueError:
            millis = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=pytz.utc).timestamp()*1000
        event_ref = firebase_admin.db.reference().child(event_id)
        event = event_ref.get()
        if not event:
            event_ref.set({
                'timestamp': millis
            })
            return False
        else:
            logging.warning('Event already in the list, exiting this instance.')
            return True
tools.py

import firebase_admin
from google.cloud import firestore

from helpers.tools import Event

firebase_admin.initialize_app(options={'databaseURL': 'https://database_name.firebaseio.com/'})
firestore_db = firestore.Client()

EVENTS_NAME = 'database-events'


def user_cleanup(data, context):
    if Event(EVENTS_NAME).check_event(context):
        return 'Done'
    user_id = data['uid']
    user_ref = firestore_db.document(f'user_data/{user_id}')
    return 'Done'
import logging
import sys
import traceback
from datetime import datetime

import firebase_admin
import pytz
from google.cloud import firestore


class Event:
    def __init__(self, database_name: str):
        init_logger()
        firebase_admin.initialize_app(options={'databaseURL': 'https://{database_name}.firebaseio.com/'})

    def check_event(self, context):
        event_id = context.event_id.replace('/', '--')
        timestamp = context.timestamp
        timestamp = timestamp[0:23] if len(timestamp) >= 24 else timestamp[0:19]
        try:
            millis = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%f').replace(tzinfo=pytz.utc).timestamp()*1000
        except ValueError:
            millis = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=pytz.utc).timestamp()*1000
        event_ref = firebase_admin.db.reference().child(event_id)
        event = event_ref.get()
        if not event:
            event_ref.set({
                'timestamp': millis
            })
            return False
        else:
            logging.warning('Event already in the list, exiting this instance.')
            return True

编辑:我的问题是,我没有通过预先设置一个
f
来格式化
数据库URL
,但我仍然能够调试,使用
try/catch
日志从未显示错误原因,只是
…完成状态:“崩溃”
,在中打开了一个问题此模式应该可以工作,您是否可能在其他地方出错?你能包括你的
main.py
tools.py
吗?您是否能够在本地运行此程序并验证它是否正常工作?您可以使用Functions Framework在本地运行它:@DustinIngram我已经添加了
main.py
tools.py
。我没有在本地尝试过,部署时没有出现错误,但在没有任何
日志的情况下触发该函数时会崩溃
vscode
确实给了我有关导入的警告<代码>\uuuu init\uuuuu.py
为空。@DustinIngram我刚刚尝试了
函数框架
,它按预期工作,尽管我刚刚修改了脚本以检查导入的类是否正确。您认为导入失败的原因是什么?如果它成功部署,但在触发时失败,则表明函数本身中的某些内容导致了崩溃。当您说“无日志崩溃”时,您看到什么表示崩溃?这是一个正在修复的已知问题: