2
votes

I noticed that to use Firebase Storage (Google Cloud Storage) I need to come up with a unique file name to upload a file.

I then plan to keep a copy of that Storage file location (https URL or gs URL) in the Firebase Realtime database, where the clients will be able to read and download it separately

However I am unable to come up with unique filenames for the files located on Firebase Storage. Using a UUID generator might cause collisions in my case since several clients are uploading images to a single Firebase root

Here's my plan. I'd like to know if it will work

Lets call my firebase root : Chatrooms, which consists of keys : chatroom_1, chatroom_2 ...chatroom_n

under chatroom_k I have a root called "Content", which stores Push keys that are uniquely generated by Firebase to store content. Each push key represents a content, but the actual content is stored in Firebase Storage and a key called URL references the URL of the actual content. Can the filename for this content on Firebase storage have the same randomized Push key as long as the bucket hierarchy represents chatroom_k?

1
If your UUID generator is creating the same UUID for multiple calls, I'd switch to a different generator. But using something based on push IDs is also fine: they're essentially UUIDs that happened to be chronologically ordered.Frank van Puffelen
If multiple clients are running the same UUID generator (say whatever is provided by IOS platform) don't they run the risk of generating the same UUIDs? Do you have a way to use a UUID generator that can also be customized on a client basis? The push key idea is tricky too. It will require me to first generate a Firebase database write, get the key that generated the write, now write to storage, and now update the database with the URL7hacker
Ah ok it appears that NSUUID will generate a globally unique id, so that solves my problem!7hacker

1 Answers

7
votes

I am not sure if storage provides push() function but a suggestion would be the following:

Request a push() to a random location to your firebase database and use this key for a name.

At any case you will probably need to store this name to the database too.

In my application I have a node called "photos" and there I store the information about the images I upload. I first do a push() to get a new key and I use this key to rename the uploaded image to.

Is this what you need or I misunderstood something?