Firebase Storage Overwriting Files

Firebase Storage Overwriting Files

That is happening because you're storing them with the same name. In Firebase Storage you're the one in charge to decide the name of the files, there is no ".childByAutoId()".

If you want to have different files, you can create random values to name them, that is well explained here: How does one generate a random number in Apple's Swift language?

Your final code should look like this:

StorageRefrenece.child("posters").child(currentUser!.uid).child(<#Any Random Value#>)

Firebase Storage - Prevent Overwriting of Files

You need to use a separate location or separate file name for each upload.

You can create a custom location like this:

 FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://myfirebaseproject.appspot.com");
StorageReference fileRef =
storageRef.child(channelId)
.child(String.valueOf(message.getSender()))
.child(String.valueOf(message.getTimestamp()));

UploadTask uploadTask = fileRef.putFile(message.getAttachmentUri());
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
sendMessage(channelId, message.getMessageText(), taskSnapshot.getStorage().toString(), message.getAttachmentType(), callback);
}
});
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
callback.onError(e.toString());
}
});

You can see we are adding custom locations to our base storage reference so that we don't overwrite existing items.

how to overwrite a file on Firebase Storage?

The problem was that my rules were wrong.

Changing the rules solved the problem

match /{allPaths=**} {
allow read, write;
}

Understanding Firebase storage when uploading files

There is no Firebase API to upload multiple files to Cloud Storage in one go. So if you upload 20 files, that'll be 20 separate uploads. If you're using Firebase's upload session URIs to continue uploads across restarts, that will also apply to each individual file in this case.

If you want to upload multiple files atomically, consider combining them into a single (e.g. zip) file, and then using a Cloud Function trigger to unpack the individual files from that archive.



Related Topics



Leave a reply



Submit