2

I use firebase storage for cloud storage

While uploading files I dont want to upload if the file is already exist in the storage reference. I just need to skip the upload and continue with the next file.

Currently I upload like this

    String pathString = "/Files/" + filename;                        
    StorageReference filepathReference = rootreference.child(pathString);
    StorageReference uploadRef = filepathReference.child(pathString);
    UploadTask uploadTask = uploadRef.putFile(file);

Which, apparently upload again and replace if the file already exists. I just want to skip the unwanted upload by making a check for the filename already exist in the storage bucket. Is that possible?

doe
  • 971
  • 2
  • 11
  • 27

2 Answers2

7

Try to get metadata. If it failed, then file do not exist

uploadRef.getMetadata()
    .addOnSuccessListener({ /*File exists*/ })
    .addOnFailureListener({
        //File do not exist
        UploadTask uploadTask = uploadRef.putFile(file);
    })

If you want to completely prohibit file overwrite, not just check file existence, you can specify security rule for storage like that

allow write: if resource.metadata == null; // 

But I've never tested it, so I'm not sure about it, maybe just resource == null will work too

Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
  • Its working fine. Thank you. Here also an extra network request will be requred for all the upload tasks rite. first for checking metadata , second the actual upload. Is there any option where I can set a criteria in the upload request , skip the file if exist with success result? – doe Oct 04 '18 at 12:05
  • @doe you can actually specify security rules, so file could not be overwritten, want an example? In this case, upload task will just fail – Dmytro Rostopira Oct 04 '18 at 12:09
  • It would be great if you could add an example in your answer! – doe Oct 04 '18 at 12:12
  • @doe added example – Dmytro Rostopira Oct 04 '18 at 12:17
  • So, this will directly fail in the upload task itself right? We can assume, if task failed, it could be because of file already exist – doe Oct 04 '18 at 12:23
  • 1
    @doe yes, it should explicitly give you `SecurityException` in failure listener – Dmytro Rostopira Oct 04 '18 at 12:31
1

There is no such function as per now in firebase but you can do it manually by saving filename in the database. You can check for the filename is exists or not before uploading. You can use firestore for this operation.

This is to check image is exists or not

    DocumentReference docRef = db.collection("images").document("file1");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                  // your code for uploading next image
                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
            } else {
               // your code for uploading this image
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

This is for saving file name to the Database

   Map<String, Object> image = new HashMap<>();
        image.put("name", "file1");

        db.collection("images").document("file1")
                .set(image)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {

                        Log.d(TAG, "DocumentSnapshot successfully written!");
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w(TAG, "Error writing document", e);
                    }
                });
mayank modi
  • 129
  • 1
  • 4