Check if a folder exists of a certain name in Firebase storage

Viewed 2569

In the Firebase Storage, I have a folder named uploads/. Inside that folder, I have multiple folders named after the userIds of the registered users.

This is what I'm trying to do. When the user enters the 'ProfileActivity', the code looks in the Firebase Database if there is any folder named after the userId of that user under the uploads/ folder. And then runs code accordingly if the folder exists or not.

This is what I have done so far, but here the addValueEventListener is not resolved. Am I doing it the right way? What I'm missing here?

public class ProfileActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private StorageReference mStorageRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        mAuth = FirebaseAuth.getInstance();
        mStorageRef = FirebaseStorage.getInstance().getReference("uploads");
    }


    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser user = mAuth.getCurrentUser();
        String userID = user.getUid();
        if (user.isEmailVerified()) {
            StorageReference userFolder = mStorageRef.child(userID);
            userFolder.addValueEventListener(new ValueEventListener() {
                @Override
                void onDataChange(DataSnapshot snapshot) {
                    if (snapshot.exists()) {
                        // run some code
                    } else {
                        //Code
                    }
                }
            });

        }
    }
}
2 Answers

Cloud Storage for Firebase doesn't have event listners like Realtime Database. You're trying to do something that isn't supported (which is why you're getting a compiler error). Please see the API documentation for StorageReference to find out what you can do with that object.

On top of that, there is no way to check if a "folder" exists in Cloud Storage. Cloud Storage doesn't actually have any folders. It just has files with path components that look like folders.

If you want to know if a file exists (not a "folder"), then you could use the getMetadata method on a StorageReference that refers to the file.

If the question was about checking whether a folder exists in a realtime firebase database using C#, then the answer is below:

You can use the json returned from the DataSnapshot, when searching for an item, to determine whether the directory exists or not. If the json is null, even though the value may not be, it doesn't exist (or is empty). This can be done using DataSnapshot.GetRawJsonValue()

Here's an example:

My Firebase Database structure looks like this:

  • Users
    • Bob
      • Name : "Bob"
      • Age : 24

Here's the full code of how to check if the value 'Bob' exists in the database or not:

    string jsonConfig = "Text from the google-services.json file you get from the database";
    AppOptions settings = AppOptions.LoadFromJsonConfig(jsonConfig);
    app = FirebaseApp.Create(settings);

    FirebaseDatabase database = FirebaseDatabase.GetInstance(app);

    // Gets the folder 'Users'
    database.GetReference("Users")
        // Gets the child called 'Bob' (whether it exists or not)
        .Child("Bob").GetValueAsync().ContinueWith(task =>
        {
            if (task.IsCompleted)
            {
                if (task.Result.GetRawJsonValue() == null)
                {
                    // 'Bob' doesn't exist
                }
                else
                {
                    // 'Bob' exists
                }
            }
        });
Related