Uploading files to Firebase Storage using REST API

Right now, the Firebase documentation guides you on uploading files to Firebase Storage by using their JavaScript library.

I am operating a server without NodeJS installed. Is it possible to upload files like image, audio, through the Firebase REST API?

At the moment I am using curl in a bash script to send JSON. I would prefer not to store base64 encoding in a database field, I want the file to be stored in a folder inside the Storage section.

Storage folder shown below:

Storage Folder

Firebase Storage uses Google Cloud Storage under the hood, so you can use the GCS REST API to get 90% of the way there. (Docs here.)

There’s a couple differences.

  1. Download URLs won’t be automatically generated until you access it through the Firebase Storage SDK.
  2. You won’t be able to use Firebase Authentication to upload through the GCS REST endpoint. You’ll either need to set up OAuth (which, you might be able to get through Google Sign in with Authentication) or a service account. (Here’s an example of how you can authenticate from Node.js to GCS.)

It is possible to upload a document to Firebase storage using the REST API. I have implemented in Python with the requests library to set HTTP headers and make a POST request. The process should be similar in other languages or with cURL.

To upload without auth:

def firebase_upload():
Executes a POST request to upload an image to Firebase Storage.
args: None
returns: response (json) with the response to the request
usage: res = firebase_upload()
    response = None

    file2upload = "/Your/Path/To/your_pic.png"
    file_binary = open(file2upload, "rb").read()

    # HTTP
    headers = {"Content-Type": "image/png"}

    r = requests.post(url2file, data=file_binary, headers=headers)
    response = r.json()

    return response

This uploads the image to a folder named ‘stash’. Be sure to use %2F instead of forward-slash in the URL. Also, you will need to make your storage bucket public using Firebase Rules. A successful response will return JSON like the following:

    {'name': 'stash/your_pic.png',
    'bucket': '<your-project-ID>.appspot.com',
    'generation': '1608437408388390',
    'metageneration': '1',
    'contentType': 'image/png',
    'timeCreated': '2020-12-20T04:10:08.388Z',
    'updated': '2020-12-20T04:10:08.388Z',
    'storageClass': 'STANDARD',
    'size': '52628',
    'md5Hash': 'mmkqwEek6tMAZmvooQ9X7g==',
    'contentEncoding': 'identity',
    'contentDisposition': "inline; filename*=utf-8''your_pic.png",
    'crc32c': 'fhlSmw==',
    'etag': 'CKaq+6LY2+0CEAE=',
    'downloadTokens': '<secure_token>'}

To upload with auth, the procedure is the same except you pass an auth token (obtained using the REST end-point for auth) with the HTTP header. Modify one line of code in the function like so.

    headers = {"Content-Type": "image/png", "Authorization": "Bearer "+auth_token}

The download URL is the url2file with the addition of ‘downloadTokens’ from the API response. Add: ‘?alt=media&token=’ followed by the token string.

Read More:   How to catch the event of clicking the app window's close button in Electron app

The example shows how to add an image to Firebase storage and all CRUD operations can be performed with the REST API by tweaking this pattern.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts