【问题标题】:Python3 firebase storage SDK cannot upload to EmulatorPython3 firebase 存储 SDK 无法上传到模拟器
【发布时间】:2021-08-30 12:35:12
【问题描述】:

我有以下代码sn-p:

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,我已验证使用方法调用 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)工作正常,但存储上传拒绝工作:(

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

【问题讨论】:

    标签: python firebase firebase-storage firebase-tools


    【解决方案1】:

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

    【讨论】:

    • 感谢您的帮助。不幸的是,我检查了没有抛出错误,但它仍然失败
    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 2022-10-07
    • 2020-02-05
    • 2019-11-06
    • 2016-11-28
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多