Skip to content
Back

Is there a way to prevent duplicated filenames in a bucket?

  • 0
  • Storage
Sir Laban
16 Jan, 2025, 16:51

Buckets are really nice, but in my app theres a specific bucket in which the files should have unique filenames, is this possible?

TL;DR
A potential solution to prevent duplicated filenames in a bucket is to provide a custom file ID generated from the md5 hash of the file name. This way, each file will have a unique ID even if the names are similar. Additionally, consider creating a document in a collection for each file to store metadata and further customize attributes. This ensures the uniqueness of files while using the bucket mainly for storage.
Meldiron
17 Jan, 2025, 16:04

Hello, files in buckets were designed to allow duplicate names. This was mainly due to storage being used in file uploads such as profile picture, blog cover page, or product image. In typical scenario, requiring unique names could cause a lot of trouble across products, or even across different end users.

What must be unique is fileID. A simple solution for you would be to provide custom file ID when uploading, and generating the ID from ms5 hash of file name. This will provide you unique email for every file, but makes duplication error when file with same name is uploaded.

Would this solution work well for you?

Meldiron
17 Jan, 2025, 16:05

If you plan to do more complex logic with files, a good future-proof solution could be to upload a file, but then make a document for it in a collection. This way you can have more attributes, permissions, and further customize indexes. This way you only use buckets to store files, but source of truth for metadata would be document

Sir Laban
17 Jan, 2025, 16:36

providing a custom fileID seems like a good solution for me in the meantime , thanks

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more