Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy Firebase Storage rules to specific buckets?

I'd like to know how storage rules deployment works in Firebase.

In Firebase Storage I have 2 buckets app.appspot.com and app-mybucket. On my local machine I have a storage.rules file which looks something like this:

service firebase.storage {
  match /b/app.appspot.com/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
  match /b/app-mybucket/o {
    match /{userId}/{allPaths=**} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

When I firebase deploy --only storage these rules get sent to the default app.appshpot.com bucket and nothing seems to get sent to app-mybucket. I'd like to know of a way I can deploy rules to all buckets.

like image 624
galki Avatar asked Dec 05 '18 18:12

galki


People also ask

How do I deploy Firebase Storage rules?

Open the Firebase console and select your project. Then, select Realtime Database, Cloud Firestore or Storage from the product navigation, then click Rules to navigate to the Rules editor. Edit your rules directly in the editor.

How do you deploy firestore security rules?

To set up and deploy your first set of rules, open the Rules tab in the Cloud Firestore section of the Firebase console. Write your rules in the online editor, then click Publish.


2 Answers

In your firebase.json file, you can specify something like this:

  "storage": [{
    "rules": "my-appspot.rules",
    "bucket": "app.appspot.com"
  },
  {
    "rules": "my-bucket.rules",
    "bucket": "app-mybucket"
  }]

The example above uses different rules per bucket, you can use the same rules for each bucket as well if you'd like.

like image 135
dvdmmc Avatar answered Nov 15 '22 10:11

dvdmmc


You can also use deploy targets. More info can be found here.

Example

  1. Apply new target (in this case "main") using CLI
firebase target:apply storage main myproject.appspot.com myproject-eu myproject-ja
  1. Update firebase.json
{
  "storage": [ {
      "target": "main",  // "main" is the applied target name for the group of Storage buckets
      "rules": "storage.main.rules"  // the file that contains the shared security rules
    }
  ]
}
  1. Use
firebase deploy --only storage:main
like image 26
Erik van den Hoorn Avatar answered Nov 15 '22 10:11

Erik van den Hoorn