首页 > 解决方案 > Python3 firebase 存储 SDK 无法上传到模拟器

问题描述

我有以下代码片段:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage 
from google.cloud import storage

class firebase_storage():

    def __init__(self, path_to_sak, root_bucket):
        try:
            self.cred = credentials.Certificate(path_to_sak)
            firebase_admin.initialize_app(self.cred)
        except Exception as e:
            print("Firebase App may have already been initialized")

        self.bucket = firebase_admin.storage.bucket(root_bucket)

    def upload(self, key, file_path): 
        blob = storage.Blob(key, self.bucket) 
        blob.upload_from_filename(file_path)

    def download(self, key, file_path):
        blob = storage.Blob(key, self.bucket)
        blob.download_to_filename(file_path)

    def upload_string(self, key, string, mime_type):
        blob = storage.Blob(key, self.bucket)  
        blob.upload_from_string(string, content_type=mime_type)


我正在使用 Firebase Emulators for Storage,我已使用方法 call 验证了下载工作firebase_storage.download()

但是,当我尝试调用upload()以下异常时:


Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 2348, in upload_from_file
    created_json = self._do_upload(
  File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 2170, in _do_upload
    response = self._do_multipart_upload(
  File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 1732, in _do_multipart_upload
    response = upload.transmit(
  File "/usr/local/lib/python3.8/dist-packages/google/resumable_media/requests/upload.py", line 149, in transmit
    self._process_response(response)
  File "/usr/local/lib/python3.8/dist-packages/google/resumable_media/_upload.py", line 116, in _process_response
    _helpers.require_status_code(response, (http_client.OK,), self._get_status_code)
  File "/usr/local/lib/python3.8/dist-packages/google/resumable_media/_helpers.py", line 99, in require_status_code
    raise common.InvalidResponse(
google.resumable_media.common.InvalidResponse: ('Request failed with status code', 400, 'Expected one of', <HTTPStatus.OK: 200>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "boot.py", line 55, in <module>
    run()
  File "boot.py", line 35, in run
    fb_storage.upload(key, file)
  File "/root/python_db_client/src/firebase_storage.py", line 20, in upload
    blob.upload_from_filename(file_path)
  File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 2475, in upload_from_filename
    self.upload_from_file(
  File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 2364, in upload_from_file
    _raise_from_invalid_response(exc)
  File "/usr/local/lib/python3.8/dist-packages/google/cloud/storage/blob.py", line 3933, in _raise_from_invalid_response
    raise exceptions.from_http_status(response.status_code, message, response=response)
google.api_core.exceptions.BadRequest: 400 POST http://myserver.com:9194/upload/storage/v1/b/xxxxxx.appspot.com/o?uploadType=multipart: Bad Request: ('Request failed with status code', 400, 'Expected one of', <HTTPStatus.OK: 200>)

我的 storage.rules 如下所示:


rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow write, read: if true;
    }
  }
}

因此,似乎允许公共读/写访问。

一切正常,我有其他模拟器(Firestore,Auth)运行良好,但存储上传拒绝工作:(

任何帮助将不胜感激,谢谢!

标签: pythonfirebasefirebase-storagefirebase-tools

解决方案


可能在初始化您的应用程序时出现问题。我看到您认为如果初始化时出现错误,应用程序已初始化。尝试先检查连接!它可能会有所帮助...


推荐阅读