Occasionally (twice in the last two days) I get an error document_already_exists when uploading an image to storage. I am using ID.unique.
The code to upload the image:
final storage = Storage(client);
final file = await storage.createFile(
bucketId: storageId,
fileId: ID.unique(),
file: InputFile.fromPath(
path: event.imagePath!,
filename: event.imagePath!.split("/").last));
This doesn't happen every time, but it has happened twice in the last two days. What could be causing this and how to fix it?
TL;DR
Issue: Error "document_already_exists" occurs sporadically when uploading images to storage using ID.unique.
Solution: The error is likely due to a coincidence where the generated unique ID matches an existing document ID in the storage. To resolve this, consider implementing a check for the existence of a document with the generated ID before uploading to ensure uniqueness.