Get started with Cloud Storage on Android  |  Cloud Storage for Firebase (2024)

Firebase is back at Google I/O on May 14! Register now.

Stay organized with collections Save and categorize content based on your preferences.

Cloud Storage for Firebase lets you upload and share user generated content, suchas images and video, which allows you to build rich media content into yourapps. Your data is stored in aGoogle Cloud Storage bucket — anexabyte scale object storage solution with high availability and globalredundancy. Cloud Storage for Firebase lets you securely upload these filesdirectly from mobile devices and web browsers, handling spotty networks withease.

Prerequisites

If you haven't already,add Firebase to your Android project.

Create a default Cloud Storage bucket

  1. From the navigation pane of the Firebase console, select Storage,then click Get started.

  2. Review the messaging about securing your Cloud Storage data using securityrules. During development, considersetting up your rules for public access.

  3. Select a location for your defaultCloud Storage bucket.

    • This location setting is your project'sdefault Google Cloud Platform (GCP) resource location.Note that this location will be used for GCP services in your projectthat require a location setting, specifically, yourCloud Firestore database and yourApp Engine app(which is required if you use Cloud Scheduler).

    • If you aren't able to select a location, then your project alreadyhas a default GCP resource location. It was set either during projectcreation or when setting up another service that requires a locationsetting.

    If you're on the Blaze plan, you cancreate multiple buckets, each with its ownlocation.

  4. Click Done.

Set up public access

Cloud Storage for Firebase provides a declarative rules language that allows youto define how your data should be structured, how it should be indexed, and whenyour data can be read from and written to. By default, read and write access toCloud Storage is restricted so only authenticated users can read or writedata. To get started without setting up Authentication, you canconfigure your rules for public access.

This does make Cloud Storage open to anyone, even people not using yourapp, so be sure to restrict your Cloud Storage again when you set upauthentication.

Add the Cloud Storage SDK to your app

In your module (app-level) Gradle file(usually <project>/<app-module>/build.gradle.kts or<project>/<app-module>/build.gradle),add the dependency for the Cloud Storage library for Android. We recommend using theFirebase Android BoMto control library versioning.

dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.8.0")) // Add the dependency for the Cloud Storage library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-storage")}

By using the Firebase Android BoM, your app will always use compatible versions of Firebase Android libraries.

(Alternative) Add Firebase library dependencieswithoutusing the BoM

If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.

Note that if you use multiple Firebase libraries in your app, we strongly recommend using the BoM to manage library versions, which ensures that all versions are compatible.

dependencies { // Add the dependency for the Cloud Storage library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-storage:20.3.0")}

Looking for a Kotlin-specific library module? Starting inOctober 2023(Firebase BoM 32.5.0), both Kotlin and Java developers candepend on the main library module (for details, see theFAQ about this initiative).

Set up Cloud Storage

The first step in accessing your Cloud Storage bucket is to create aninstance of FirebaseStorage:

Kotlin+KTX

storage = Firebase.storage

Java

FirebaseStorage storage = FirebaseStorage.getInstance();

You're ready to start using Cloud Storage!

First, let's learn how to create a Cloud Storagereference.

Advanced setup

There are a few use cases that require additional setup:

The first use case is perfect if you have users across the world, and want tostore their data near them. For instance, you can create buckets in the US,Europe, and Asia to store data for users in those regions to reduce latency.

The second use case is helpful if you have data with different access patterns.For instance: you can set up a multi-regional or regional bucket that storespictures or other frequently accessed content, and a nearline or coldline bucketthat stores user backups or other infrequently accessed content.

In either of these use cases, you'll want touse multiple Cloud Storage buckets.

The third use case is useful if you're building an app, like Google Drive, whichlets users have multiple logged in accounts (for instance, a personal accountand a work account). You canuse a custom Firebase Appinstance to authenticate each additional account.

Use multiple Cloud Storage buckets

If you want to use a Cloud Storage bucket other than the default provided above,or use multiple Cloud Storage buckets in a single app, you can create an instanceof FirebaseStorage that references your custom bucket:

Kotlin+KTX

// Get a non-default Storage bucketval storage = Firebase.storage("gs://my-custom-bucket")

Java

// Get a non-default Storage bucketFirebaseStorage storage = FirebaseStorage.getInstance("gs://my-custom-bucket");

Working with imported buckets

When importing an existing Cloud Storage bucket into Firebase, you'llhave to grant Firebase the ability to access these files using thegsutil tool, included in theGoogle Cloud SDK:

gsutil -m acl ch -r -u service-<project number>@gcp-sa-firebasestorage.iam.gserviceaccount.com gs://<your-cloud-storage-bucket>

You can find your project number as described in the introduction toFirebase projects.

This does not affect newly created buckets, as those have the default accesscontrol set to allow Firebase. This is a temporary measure, and will beperformed automatically in the future.

Use a custom Firebase App

If you're building a more complicated app using a custom FirebaseApp, you cancreate an instance of FirebaseStorage initialized with that app:

Kotlin+KTX

// Get the default bucket from a custom FirebaseAppval storage = Firebase.storage(customApp!!)// Get a non-default bucket from a custom FirebaseAppval customStorage = Firebase.storage(customApp, "gs://my-custom-bucket")

Java

// Get the default bucket from a custom FirebaseAppFirebaseStorage storage = FirebaseStorage.getInstance(customApp);// Get a non-default bucket from a custom FirebaseAppFirebaseStorage customStorage = FirebaseStorage.getInstance(customApp, "gs://my-custom-bucket");

Next steps

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-03-28 UTC.

Get started with Cloud Storage on Android  |  Cloud Storage for Firebase (2024)
Top Articles
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 5447

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.