Skip to main content

Firebase Storage

Cloud Storage for Firebase allows you to quickly and easily upload files to a Cloud Storage bucket provided and managed by Firebase.

Create a Reference

In order to upload or download files, delete files, or get or update metadata, you must create a reference to the file you want to operate on. A reference can be thought of as a pointer to a file in the cloud.

example:

import { getStorage, ref } from "firebase/storage";

// Get a reference to the storage service, which is used to create references in your storage bucket
const storage = getStorage();

// Create a storage reference from our storage service
const storageRef = ref(storage);

create a reference to a location lower in the tree, say 'images/space.jpg' by using the child() method on an existing reference.

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();

// Create a child reference
const imagesRef = ref(storage, 'images');
// imagesRef now points to 'images'

// Child references can also take paths delimited by '/'
const spaceRef = ref(storage, 'images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

Upload Files

To upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name.

import { getStorage, ref } from "firebase/storage";

// Create a root reference
const storage = getStorage();

// Create a reference to 'mountains.jpg'
const mountainsRef = ref(storage, 'mountains.jpg');

// Create a reference to 'images/mountains.jpg'
const mountainImagesRef = ref(storage, 'images/mountains.jpg');

// While the file names are the same, the references point to different files
mountainsRef.name === mountainImagesRef.name; // true
mountainsRef.fullPath === mountainImagesRef.fullPath; // false

Once you've created an appropriate reference, you then call the put() method. put() takes files via the JavaScript File and Blob APIs and uploads them to Cloud Storage.

import { getStorage, ref, uploadBytes } from "firebase/storage";

const storage = getStorage();
const storageRef = ref(storage, 'some-child');

// 'file' comes from the Blob or File API
uploadBytes(storageRef, file).then((snapshot) => {
console.log('Uploaded a blob or file!');
});

Cloud Functions for Firebase

Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests.

The functions you write can respond to events generated by various Firebase and Google Cloud features, from Firebase Authentication triggers to Cloud Storage Triggers.

Integrate across Firebase features ussing the Admin SDK together with Cloud Functions, and integrate with third-party services by writing your own webhooks. Cloud Functions minimizes boilerplate code, making it easier to use Firebase and Google Cloud inside your function.

How does it work?

After you write and deploy a function, Google's servers begin to manage the function immediately. You can fire the function directly with an HTTP request, or, in the case of background functions, Google's servers will listen for events and run the function when it is triggered.

As the load increases or decreases, Google responds by rapidly scaling the number of virtual server instances needed to run your function. Each function runs in isolation, in its own environment with its own configuration.

Get Start with firebase

Add Firebase to your Android project

Prerequisites

Install or update Android Studio to its latest version.

Make sure that your project meets these requirements:

Targets API level 16 (Jelly Bean) or higher Uses Android 4.1 or higher Uses Jetpack (AndroidX), which includes meeting these version requirements: com.android.tools.build:gradle v3.2.1 or later compileSdkVersion 28 or later Set up a physical device or use an emulator to run your app. Note that Firebase SDKs with a dependency on Google Play services require the device or emulator to have Google Play services installed.

Sign into Firebase using your Google account.

If you don't already have an Android project and just want to try out a Firebase product, you can download one of our quickstart samples.

You can connect your Android app to Firebase using one of the following options:

Option 1: (recommended) Use the Firebase console setup workflow. Option 2: Use the Android Studio Firebase Assistant (may require additional configuration).

Option 1: Add Firebase using the Firebase console

Adding Firebase to your app involves tasks both in the Firebase console and in your open Android project (for example, you download Firebase config files from the console, then move them into your Android project).

Step 1: Create a Firebase project

Before you can add Firebase to your Android app, you need to create a Firebase project to connect to your Android app. Visit Understand Firebase Projects to learn more about Firebase projects.

Create a Firebase project

Step 2: Register your app with Firebase

To use Firebase in your Android app, you need to register your app with your Firebase project. Registering your app is often called "adding" your app to your project.

Note: Visit Understand Firebase Projects to learn more about best practices and considerations for adding apps to a Firebase project, including how to handle multiple build variants. Go to the Firebase console.

In the center of the project overview page, click the Android icon (plat_android) or Add app to launch the setup workflow.

Enter your app's package name in the Android package name field.

What's a package name, and where do you find it?

Make sure to enter the package name that your app is actually using. The package name value is case-sensitive, and it cannot be changed for this Firebase Android app after it's registered with your Firebase project. (Optional) Enter other app information: App nickname and Debug signing certificate SHA-1.

How are the App nickname and the Debug signing certificate SHA-1 used within Firebase?

Click Register app.

Step 3: Add a Firebase configuration file

Add the Firebase Android configuration file to your app:

Click Download google-services.json to obtain your Firebase Android config file (google-services.json).

Move your config file into the module (app-level) directory of your app.

What do you need to know about this config file?

To enable Firebase products in your app, add the google-services plugin to your Gradle files.

In your root-level (project-level) Gradle file (build.gradle), add rules to include the Google Services Gradle plugin. Check that you have Google's Maven repository, as well.

buildscript {

repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}

dependencies {
// ...

// Add the following line:
classpath 'com.google.gms:google-services:4.3.10' // Google Services plugin
}
}

allprojects {
// ...

repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
// ...
}
}

In your module (app-level) Gradle file (usually app/build.gradle), apply the Google Services Gradle plugin:

apply plugin: 'com.android.application'
// Add the following line:
apply plugin: 'com.google.gms.google-services' // Google Services plugin

android {
// ...
}

Step 4: Add Firebase SDKs to your app

Using the Firebase Android BoM, declare the dependencies for the Firebase products that you want to use in your app. Declare them in your module (app-level) Gradle file (usually app/build.gradle).

Analytics enabled

Java

dependencies {
// ...

// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:28.4.2')

// When using the BoM, you don't specify versions in Firebase library dependencies

// Declare the dependency for the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics'

// Declare the dependencies for any other desired Firebase products
// For example, declare the dependencies for Firebase Authentication and Cloud Firestore
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-firestore'
}

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

Sync your app to ensure that all dependencies have the necessary versions.

Getting a build failure about invoke-custom support and enabling desugaring? Here's how to fix it.

That's it! You can skip ahead to check out the recommended next steps.

If you're having trouble getting set up, though, visit the Android troubleshooting & FAQ.