Cloud Storage Setup and Configuration
In our previous section, we learned about Cloud Storage, its key capabilities, features, and how it works. Now, we will discuss how we set up and configure an Android application with Firebase to use Cloud Storage in our application. Just like Firebase Real-time Database and Firebase Firestore, Cloud Storage’s starting steps are the same and will use Kotlin. So let’s start with the starting steps and elaborate each step, which is performed to set up and configure the application to use Cloud Storage in Firebase.
Step 1:
In the first step, we will create a new Android Studio project with an empty activity and Kotlin language and give it name CloudStorageExample.
Step 2:
We will connect our Android Application with the Firebase either from Firebase Assistant or manually using console. After that, we will add all the required libraries and plugin to our app.gradle file. And we will also add mavenLocal() as our repository and all projects.
Step 3:
We will go to the Firebase console and look at the Firebase Cloud Storage in Developers-> Storage.
Step 4:
We will create a database by clicking on the Get started. After clicking on Get started, a popup box is open where we set storage with specific rules and click on Next.
After clicking on Next, a popup box will open. Here, we can select our Cloud Storage location depending on where we want to locate, and at last, click on Done.
Step 5:
After clicking on Done, a Cloud Storage will be created, which will look different from the real-time database and Cloud Firestore. Here, we have Files, Rules, and, Usage for file storing, security rules, and usage, respectively.
Step 6:
There is no need to change our security rules because, by default, an authenticated user can only read and write to the storage. These rules are defined as:
If we want to make our Cloud Storage public, then the modification is done in the security rules are as followed:
If we want to use these rules for data validation that it can be possible. We can validate the file name and path. These rules are also used for validating metadata properties such as contentType and size.
Setup and configuration are completed here, and now we can implement our code to use this storage.