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
Swift 'Unable to Dequeue a Cell with Identifier Intervalcellidentifier
Text from [String] to a Label.Text Isn't Working the First Time
Concatenate Literal with Optional String
Crash When Running on Device After Second Launch
Swiftui - Make Toolbar's Navigationlink Use Detail View
Way to Purge All But One Object Types (Models) in a Realm
How to Move Platform with Velocity
Fetch Coredata with One to Many Relationship in Swift
Why Strings Are Not Equal in My Case
Expression Pattern of Type 'String' Cannot Match Values of Type 'Nsstoryboardsegue.Identifier
How to Use Spritekit Archives with Skspritenode Subclasses
Searchbar Problem While Trying to Search Firestore and Reload the Tableview
Shows the Alert When Uitextfield's Are Full or Empty Swift
Why Upload Alamofire Background Request Don't Executes in Background
How to Get Timezone Offset as ±Hh:Mm
Dyld: Library Not Loaded, App Requires Afnetworking 2.0.0 But Provides Version 1.0.0
Alternative to Switch Statement in Swiftui Viewbuilder Block