- Published at
Setting Firebase Storage CORS Rules: A Practical Guide
Learn how to configure CORS rules for Firebase Storage using Google Cloud SDK, enabling cross-origin requests from your web applications. Step-by-step instructions with code examples.
- Authors
-
-
- Name
- James Lau
- Indie App Developer at Self-employed
-
Table of Contents
title: Setting Firebase Storage CORS Rules
Firebase Storage leverages the same infrastructure as Google Cloud Storage. While there isn’t a direct Firebase method to manage CORS (Cross-Origin Resource Sharing) rules, you can effectively configure them using the Google Cloud SDK (gcloud). This guide provides a practical walkthrough for intermediate learners.
Prerequisites
Before we begin, ensure you have the Google Cloud SDK installed. If not, follow these steps:
- Install gcloud:
curl https://sdk.cloud.google.com | bash - Restart your shell:
exec -l $SHELL - Initialize gcloud: This will prompt you to select an account and authenticate.
gcloud init
Creating the CORS Configuration File (firebase_cors.json)
The firebase_cors.json file defines which origins are allowed to access your Firebase Storage bucket. Here’s a sample configuration:
[
{
"origin": ["http://localhost:5173/", "https://your-web.org", "https://*.your-web.org"],
"responseHeader": ["Content-Type"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
Explanation:
origin: An array of allowed origins. Replacehttps://your-web.organdhttps://*.your-web.orgwith your actual domain(s).http://localhost:5173/is commonly used for local development.responseHeader: Specifies which response headers to expose.Content-Typeis essential for many requests.method: Defines the allowed HTTP methods (e.g., GET, POST).maxAgeSeconds: Sets the cache duration in seconds.
Applying the CORS Rules
Once you have your firebase_cors.json file, use the following command to apply the rules to your Firebase Storage bucket:
gsutil cors set firebase_cors.json gs://your-firbase-app-33117.appspot.com
Important: Replace gs://your-firbase-app-33117.appspot.com with the actual name of your Firebase Storage bucket.

Real-World Context and Troubleshooting
- Local Development: The
http://localhost:5173/origin is crucial for testing your application locally during development. - Production Deployment: Ensure that the origins in your
firebase_cors.jsonfile accurately reflect your production domain(s). - Troubleshooting CORS Errors: If you encounter CORS errors, double-check your
firebase_cors.jsonconfiguration and verify that the origin matches the requesting website’s URL exactly.
By following these steps, you can effectively manage CORS rules for your Firebase Storage bucket using gcloud, enabling seamless cross-origin requests from your web applications.