使用 Pyrebase 将 JSON 文件从 Google App Engine 上传到 Firebase 存储

Uploading a JSON file with Pyrebase to Firebase Storage from Google App Engine

我在 GAE 中有一个非常简单的 Flask 网络应用程序 运行,它从 Firebase 存储下载一个 JSON 文件,并在必要时用更新的文件替换它。一切正常,但每当我尝试创建新文件时,GAE 都会抛出 IOError 异常。我正在使用 Firebase 存储,因为我知道在 GAE 环境中 read/write 文件是不可能的,但是我应该如何使用 Pyrebase storage.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'

如果我理解正确的话,您只需要在 GAE 中临时使用此文件,然后将其放入云存储中。根据这个 doc 你可以像在正常 OS 中那样做,但是在 /tmp 文件夹中:

if your app only needs to write temporary files, you can use standard Python 3.7 methods to write files to a directory named /tmp

希望对您有所帮助!

我终于这样做了,但我不知道这与@vitooh 解决方案相比是更好、更差还是仅等同于此。请让我知道:

firebase_config = {my_firebase_config_dict}

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

@app.route('/')
def check_for_updates() :
    try :
        blob = bucket.blob('example.json')
        example = json.loads(blob.download_as_string()
        # Here I check if I need to update example.json
        # ...
        if something_changed :
            blob.upload_from_string(example, content_type = 'application/json')
        return 'finished successfully!'
    except IOError :
        return 'example.json doesn't exists'