簡體   English   中英

Python3 firebase存儲SDK無法上傳到Emulator

[英]Python3 firebase storage SDK cannot upload to Emulator

我有以下代碼片段:

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 模擬器進行存儲,我已經使用方法調用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 Admin SDK 目前不支持存儲模擬器根據文檔https://firebase.google.com/docs/emulator-suite/install_and_configure#admin_sdk_availability

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM