Firebase Cloud Storage How-to and Use Case

JetRuby Agency
JetRuby Agency
Published in
4 min readAug 29, 2018

--

What is Firebase Cloud Storage?

Firebase Cloud Storage is a modern technology that allows for storing and managing various media content generated by mobile app users.

One of its biggest advantages is reliability. Firebase SDK for Cloud Storage works regardless of the network quality. in other words, if a file stops uploading because of slow Internet connection, the process will be automatically restarted. For regular users, this feature saves minutes of waiting.

From a security viewpoint, Firebase Cloud Storage is doing great. Firebase SDK for Cloud Storage can be integrated with Firebase Authentication which provides a simple and user-friendly mechanism of authentication for developers. It allows you to use a declarative security model to enable access based on the name of a file, its size, content type, and other kinds of metadata.

Firebase Cloud Storage SDK also uses a declarative data protection language which allows you to manage the access to the files in the storage and make them public or private.

In plain English, Cloud Storage takes full care of protecting the files you upload.

How to integrate Firestore Cloud Storage in an Android app

First, you need to register your application in Firebase Console. When that’s done, add the generated file “google-services.json” to the app.

Go to the “Rules” section and enable the ability to read from and write into the storage. In future, you should provide access only to authorized users.

Now you need to create Storage. All the files will be stored in Bucket and organized in the form of a tree structure (just like the file system on a hard drive). To get access to a specific file, you need to create a link to it that can be used for uploading or editing the metadata and removing the files.

Now let’s connect Google Services to build.gradle(Project):

After that, we need to connect Storage SDK to build.gradle(Module):

In the app, we’ll pick am image and send it to Storage. To pick an image, we’ll use Intent. The Uri of the image will be stored in onActivityResult.

Now let’s create Firebase Storage:

To upload the file to Firebase Storage, we need to create a link to the file “img/fileUri”. This will create a child object “child(“img/${fileUri.lastPathSegment}”)” in the Storage. The “img” folder is where the file “fileUri.lastPathSegment” will be uploaded.

We can also create the metadata for the file:

Let’s create a Task that will upload the image with the metadata to Storage putFile(fileUri, metadata):

To listen on a state of the file upload, we need to add OnProgressListener:

Also, to listen on the state of successful upload or upload failure, we need to add OnCompleteListener, OnFailureListener:

Now we need to make sure the file has been created in the storage:

and it has all the necessary metadata:

Now it’s time to create a link to the file in the storage to upload it to the device. Another thing that we’ll need is calling the getFile() method:

To delete the file, we can use the delete() method:

Conclusion

Firebase Cloud Storage is a powerful yet easy-to-use tool for storing all sorts of objects. With its help, you can store images, audio, video, and other types of user content.

Firebase Cloud Storage allows you to smoothly switch from an app prototype to a full-fledged product thanks to being highly-scalable and the ability to process exabytes of data.

If you want to have a more detailed look at the capabilities of Firebase Cloud Storage, you can refer to the official documentation by this link:

https://firebase.google.com/docs/storage

--

--

JetRuby is a Digital Agency that doesn’t stop moving. We expound on subjects as varied as developing a mobile app and through to disruptive technologies.