首页 > 解决方案 > 使用 Pyrebase 从 Google App Engine 将 JSON 文件上传到 Firebase 存储

问题描述

我有一个在 GAE 中运行的非常简单的 Flask Web 应用程序,它从 Firebase 存储下载一个 JSON 文件,并在必要时用更新的文件替换它。一切正常,但每当我尝试创建新文件时,GAE 都会引发 IOError 异常。我正在使用 Firebase 存储,因为我知道在 GAE 环境中无法读取/写入文件,但是我应该如何使用 Pyrebasestorage.child('foo.json').put('foo.json')函数呢?我做错了什么?请在下面检查我的代码。

firebase_config = {my_firebase_config_dict}

pyrebase_app = pyrebase.initialize_app(firebase_config)
storage = pyrebase_app.storage()

@app.route('/')
def check_for_updates() :
    try :
        json_feeds = json.loads(requests.get('http://my-firebase-storage-url/example.json').text()
        # Here I check if I need to update example.json
        # ...
        with open("example.json", "w") as file:
            json.dump(info, file)
            file.close()
            storage.child('example.json').put('example.json')
        return 'finished successfully!'
    except IOError :
        return 'example.json doesn't exists'

标签: python-3.xgoogle-app-engineflaskfirebase-storagepyrebase

解决方案


如果我理解正确,您只需要在 GAE 中临时使用此文件,然后将其放入云存储中。根据此文档,您可以像在普通操作系统中一样执行此操作,但在 /tmp 文件夹中:

如果您的应用只需要写入临时文件,您可以使用标准 Python 3.7 方法将文件写入名为 /tmp 的目录

我希望它会有所帮助!


推荐阅读