I am developing a real time web chat application using firebase. The issue now is that I want to implement file uploads(images, videos and documents) and want to generate the hashing keys of these files prior to upload and then query the storage or firestore as the case maybe if a file with similar hash key already exists. If it exists, there won't be a need to upload the file anymore and the file can then be forwarded to the user from firestore. If there is no matching hash key, the file upload proceeds and is delivered to the user.
While researching, the only method I found in the documentation needs the reference to an existing file specified. Isn't it possible to query the whole database for the specific hashkey since having to query a specific file in the database defeats the whole purpose.
This is the method I am talking about:
// Create a reference to the file whose metadata we want to retrieve
const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');
// Get metadata properties
getMetadata(forestRef)
.then((metadata) => {
// Metadata now contains the metadata for 'images/forest.jpg'
})
.catch((error) => {
// Uh-oh, an error occurred!
});
Is that possible and how can it be done?
CodePudding user response:
There is indeed no way to query Cloud Storage through Firebase to get only files with a specific value in the metadata.
I see two main options:
- Use the hash value as the (basis for the) name of the file, so that you can check whether a file with the given name already exists.
- Store the hash values in Firestore (or Realtime Database or any other cloud-hosted database), and use the query API that offers to check for the existing of a file.
I'd recommend the former, as the latter has a race condition that's gonna be pretty tricky to work around, while the former is pretty straightforward.