Introduction to the Admin Cloud Storage API

Cloud Storage for Firebase stores your data in a Google Cloud Storage bucket — an exabyte scale object storage solution with high availability and global redundancy. The Firebase Admin SDK allows you to directly access your Cloud Storage buckets from privileged environments. Then you can use Google Cloud Storage APIs to manipulate the objects stored in the buckets.

The Admin SDK also lets you to create shareable URLs so users can download objects in your buckets.

Make sure your Firebase project is on the pay-as-you-go Blaze pricing plan, which is a requirement to use Cloud Storage for Firebase. If you're new to Firebase and Google Cloud, check if you're eligible for a $300 Free Trial credit.

Use a default bucket

You can specify a default bucket name when initializing the Admin SDK. Then you can retrieve an authenticated reference to this bucket.

The bucket name must not contain gs:// or any other protocol prefixes. For example, if the bucket URL displayed in the Firebase console is gs://PROJECT_ID.firebasestorage.app, pass the string PROJECT_ID.firebasestorage.app to the Admin SDK.

Node.js

const { initializeApp, cert } = require('firebase-admin/app');
const { getStorage } = require('firebase-admin/storage');

const serviceAccount = require('./path/to/serviceAccountKey.json');

initializeApp({
  credential: cert(serviceAccount),
  storageBucket: '<BUCKET_NAME>.appspot.com'
});

const bucket = getStorage().bucket();

// 'bucket' is an object defined in the @google-cloud/storage library.
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/latest/storage/bucket
// for more details.

Java

FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");

FirebaseOptions options = FirebaseOptions.builder()
    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
    .setStorageBucket("<BUCKET_NAME>.appspot.com")
    .build();
FirebaseApp.initializeApp(options);

Bucket bucket = StorageClient.getInstance().bucket();

// 'bucket' is an object defined in the google-cloud-storage Java library.
// See https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket
// for more details.

Python

import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage

cred = credentials.Certificate('path/to/serviceAccountKey.json')
firebase_admin.initialize_app(cred, {
    'storageBucket': 'PROJECT_ID.firebasestorage.app'
})

bucket = storage.bucket()

# 'bucket' is an object defined in the google-cloud-storage Python library.
# See https://googlecloudplatform.github.io/google-cloud-python/latest/storage/buckets.html
# for more details.

Go

import (
	"context"
	"log"

	firebase "firebase.google.com/go/v4"
	"firebase.google.com/go/v4/auth"
	"google.golang.org/api/option"
)

config := &firebase.Config{
	StorageBucket: "<BUCKET_NAME>.appspot.com",
}
opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")
app, err := firebase.NewApp(context.Background(), config, opt)
if err != nil {
	log.Fatalln(err)
}

client, err := app.Storage(context.Background())
if err != nil {
	log.Fatalln(err)
}

bucket, err := client.DefaultBucket()
if err != nil {
	log.Fatalln(err)
}
// 'bucket' is an object defined in the cloud.google.com/go/storage package.
// See https://godoc.org/cloud.google.com/go/storage#BucketHandle
// for more details.