'Enabling CORS on Firebase Storage for localhost tests

My web app will run on Firebase Hosting, but I need to test it locally through firebase serve on localhost:5000 I am incurring in a CORS error while trying to read a file on Firebase Storage.

I have followed documentation, and added the relative heading section to firebase.json:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
  "headers": [ {
      "source": "**",
      "headers": [ {
        "key": "Access-Control-Allow-Origin",
        "value": "*"
      } ]
    }]
  },
  "storage": {
    "rules": "storage.rules"
  }
}

But this didn't fix. I am still blocked on CORS error.

EDIT with solution

I have fixed this by using the python library and a short script:

#!/usr/bin/env python3

import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage


CREDFILE = home + 'credentials.json'
cred = credentials.Certificate(CREDFILE)
firebase_admin.initialize_app(cred, {
    'storageBucket': '<myapp>.appspot.com'
})
bucket = storage.bucket()

bucket.cors = [{
  'origin': ['*'],
  'method': ['GET'],
  'maxAgeSeconds': 86400
}]

bucket.update()


Solution 1:[1]

This is how to set up storage cors using gsutil:

  1. Install gsutil from cloud.google.com, connect your google account

2. Backup current setup with gsutil cors get <MY_FIRESTORE_BUCKET>, where <MY_FIRESTORE_BUCKET> is the address of your firestore bucket in following format: gs://example.appspot.com 3. Create json file with cors setup, eg my-cors.json:

[
 {
 "origin": ["*"],
 "method": ["GET"],
 "maxAgeSeconds": 3600
 }
]
  1. Navigate in command prompt to my-cors.json file, and set that file as a new cors config for your bucket with this command: gsutil cors set my-cors.json <MY_FIRESTORE_BUCKET>

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 ego