One Firebase Storage

Firebase Storage in Android is a service provided by Firebase that allows you to store and serve user-generated content such as photos, videos, and other media files. It is built on Google Cloud Storage, providing robust, scalable, and secure object storage. Firebase Storage is designed to make it easy for developers to manage file uploads and downloads directly from the mobile device or the web. This service handles the complexities of large file uploads, including resumable uploads, and offers extensive client libraries that integrate seamlessly with Firebase Authentication, enabling secure file access and storage based on user identity.

Firebase Storage offers key features such as high reliability and availability, automatic scaling to handle varying loads, and the ability to store and retrieve files quickly. It supports resumable uploads, which is particularly useful for mobile applications where network conditions can be unpredictable. This means if an upload is interrupted, it can resume from where it left off rather than starting over. Additionally, Firebase Storage provides comprehensive security via Firebase Security Rules, allowing developers to control access to stored files based on user authentication status and other conditions. This tight integration with Firebase's other services, such as Firestore and Realtime Database, makes Firebase Storage a powerful tool for managing user content in Android applications.

Checkout full features here

Example Usage:

Class: FirebaseStorageDataStore

class FirebaseStorageDataStore : FirebaseStorageRepository, OneFirebaseStorage, OneFirebaseFirestore() {
    override val databaseRef: FirebaseFirestore = FirebaseFirestore.getInstance()
}

Description

FirebaseStorageDataStore is a Kotlin class that implements the FirebaseStorageRepository interface, extending the functionality of OneFirebaseStorage and OneFirebaseFirestore to manage file uploads and deletions in Firebase Storage and Firestore. This class provides methods to upload files to Firebase Storage and store their metadata in Firestore, as well as delete files and their corresponding metadata.

Properties

  • databaseRef: FirebaseFirestore

    • An instance of FirebaseFirestore used to interact with Firestore.

    • Initialized with FirebaseFirestore.getInstance().

  • storageReference: FirebaseStorage

    • An instance of FirebaseStorage used to interact with Firebase Storage.

    • Initialized with FirebaseStorage.getInstance().

  • storage: String

    • A string representing the storage path, initialized to "files".

Methods

uploadFile

override suspend fun uploadFile(body: StorageRequest): Flow<Resource<Boolean>> {
    return callbackFlow {
        pushFile(file = body.file, child = body.name) { url ->
            // set value to firestore after file uploaded
            body.url = url
            setValue(
                collection = "storages",
                value = body.toHashMap(),
            ) { listener ->
                trySend(listener)
            }
        }

        awaitClose(this::close)
    }
}
  • Description: Uploads a file to Firebase Storage and stores its metadata in Firestore.

  • Parameters:

    • body: StorageRequest - An object containing the file and metadata to be uploaded.

  • Returns: Flow<Resource<Boolean>> - A flow emitting the result of the upload operation.

  • Implementation:

    • Uses callbackFlow to manage the asynchronous file upload.

    • Calls pushFile to upload the file to Firebase Storage.

    • Upon successful upload, sets the file URL in the body object.

    • Calls setValue to store the file metadata in the Firestore "storages" collection.

    • Uses trySend to emit the result of the Firestore operation.

    • Ensures proper closure of the flow with awaitClose.

deleteFile

override suspend fun deleteFile(id: String): Flow<Resource<Boolean>> {
    return callbackFlow {
        removeFile(id) { deleted ->
            if (deleted) {
                deleteValue(collection = "storages", id = id) { listener ->
                    trySend(listener)
                }
            }
        }

        awaitClose(this::close)
    }
}
  • Description: Deletes a file from Firebase Storage and removes its metadata from Firestore.

  • Parameters:

    • id: String - The identifier of the file to be deleted.

  • Returns: Flow<Resource<Boolean>> - A flow emitting the result of the delete operation.

  • Implementation:

    • Uses callbackFlow to manage the asynchronous file deletion.

    • Calls removeFile to delete the file from Firebase Storage.

    • If the file is successfully deleted, calls deleteValue to remove the file metadata from the Firestore "storages" collection.

    • Uses trySend to emit the result of the Firestore operation.

    • Ensures proper closure of the flow with awaitClose.

Request Model

data class StorageRequest(
    val name: String,
    val file: File,
    var url: String? = emptyString()
) {
    @Exclude
    fun toHashMap(): HashMap<String, Any?> = hashMapOf(
        "name" to name,
        "file" to url
    )
}

The StorageRequest data class is designed to handle storage-related requests. It contains information about a file to be stored, including its name, the file object itself, and an optional URL associated with the file. Additionally, it provides a method to convert the class instance to a HashMap.

Last updated