1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Firebase tips(Storage rule)

Last updated at Posted at 2017-09-28

FIREBASE TIPS

1. Allow user to be able to upload profile.png in own directory.

storage.rule
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read: if request.auth != null;
    }
    
    match /images/{userId}/profile.png {
      allow read;
      allow write: if request.auth.uid == userId;
    }
  }
}
test1
<script>
    firebase.initializeApp(config);
    firebase.auth().signInWithEmailAndPassword('coffeeandcode.tokyo@gmail.com', 'xxxxx').catch((error) => {
        console.log('code:' + error.code + 'message' + error.message);
    });

    firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
            let uid = user.uid;
            console.log('login success');
        } else { }
    });

    var storage = firebase.storage();
    function upload() {
        var file = document.getElementById('loadFile').files[0];
        var storageRef = storage.ref('/images/eFcFmn2lbqTzXf83vtHPdSCID9W2/profile.png');
        storageRef.put(file).then(function (snapshot) {
            console.log('Uploaded a blob or file!');
        });
    }
</script>

result
result.png

result
result2.png

test2
<script>
    firebase.initializeApp(config);
    firebase.auth().signInWithEmailAndPassword('coffeeandcode.tokyo2@gmail.com', 'xxxxx').catch((error) => {
        console.log('code:' + error.code + 'message' + error.message);
    });

    firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
            let uid = user.uid;
            console.log('login success');
        } else { }
    });

    var storage = firebase.storage();
    function upload() {
        var file = document.getElementById('loadFile').files[0];
        var storageRef = storage.ref('/images/eFcFmn2lbqTzXf83vtHPdSCID9W2/profile.png');
        storageRef.put(file).then(function (snapshot) {
            console.log('Uploaded a blob or file!');
        });
    }
</script>

result
result.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?