0

My Database is Like this:

UID -> Image, name, email,

And when a picture is posted it has a name, and if i put the same name on a new picture it overwrites the older one.. I wanted it to create a new one on the storage...

Here is my code to upload the files to storage and database:

    createHomePost(pictureName: string, picture: string, username, email): firebase.Promise<any> {
    firebase.storage().ref('/homePictures/').child(pictureName)
        .child('picture.jpg')
        .putString(picture, 'base64', { contentType: 'image/jpg' })
        .then((savedPicture) => {
            this.HbRef.push({
                picture: savedPicture.downloadURL,
                name: pictureName,
                username: firebase.auth().currentUser.displayName,
                email: firebase.auth().currentUser.email
            })
        });
    return
}

Thanks in advance.

Luis Pinho
  • 55
  • 1
  • 1
  • 10

1 Answers1

2

It's not possible to have multiple objects to share the same exact name at a given path. It has to be unique.

You should instead consider object versioning to keep old version of the object. (see here https://cloud.google.com/storage/docs/object-versioning)

Other options could be appending something to the old object. or save it on other path (like archive)

goblin
  • 1,513
  • 13
  • 13
  • Thanks! Just one more thing is there any possibility to create instead of a name, some other form of reference to a image that can be duplicated? – Luis Pinho Mar 23 '18 at 22:42
  • Not that I know of. There can't be duplicates. – goblin Mar 23 '18 at 22:50
  • ok, Thanks man :D, sorry last thing if i had some sort of unique id to each image or a loop so that each image gains a new number probably? – Luis Pinho Mar 23 '18 at 23:15
  • yeah that's fine as long as it is unique. you can check the answers here https://stackoverflow.com/questions/37444685/store-files-with-unique-random-names https://stackoverflow.com/questions/25884840/how-to-store-same-name-file-in-google-cloud-storage – goblin Mar 23 '18 at 23:25