相关文章推荐
强健的领带  ·  QT:QLineEdit ...·  1 年前    · 
逼格高的铅笔  ·  SqliteConnection.Creat ...·  1 年前    · 
大力的山羊  ·  sqlite convert int to ...·  1 年前    · 
买醉的野马  ·  ptmx/pts_qq635f6f89e56 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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 Jul 3, 2016 at 15:34 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 URL 7hacker Jul 3, 2016 at 16:28 Ah ok it appears that NSUUID will generate a globally unique id, so that solves my problem! 7hacker Jul 3, 2016 at 17:26

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?

So I had the same problem and I reached this solution:

I named the files with time and date and the user uid, so it is almost impossible to have two files with the same name and they will be different every single time.

DateFormat dtForm = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.");
String date = dtForm.format(Calendar.getInstance().getTime());
String fileName = date + FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseStorage
                .getInstance()
                .getReference("Folder/SubFolder/" + fileName)
                .putFile(yourData)

With this the name of the files are going to be like this "2022.09.12.11.50.59.WEFfwds2234SA11" for example

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.