'Python3 firebase storage SDK cannot upload to Emulator

I have the following code snippet:

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)


I'm using Firebase Emulators for Storage, I have verified that downloads work using the method call firebase_storage.download().

However, when I try to call upload() the following exception is thrown:


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>)

My storage.rules look like this:


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

And so, it would appear that public read/write access is allowed.

Everything is working, I have other emulators (Firestore, Auth) that is working fine, but Storage uploads refuse to work :(

Any help would be greatly appreciated thank you!



Solution 1:[1]

Maybe there is a problem initializing your app. I see your are taking granted that the app is initialized if there is an error while initializing. Try checking connection first! It may help...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MohammadArik