如何使用本地依赖项部署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我刚刚尝试了函数框架
,它按预期工作,尽管我刚刚修改了脚本以检查导入的类是否正确。您认为导入失败的原因是什么?如果它成功部署,但在触发时失败,则表明函数本身中的某些内容导致了崩溃。当您说“无日志崩溃”时,您看到什么表示崩溃?这是一个正在修复的已知问题: