Introduction to the Admin Cloud Storage API

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

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.

Node.js

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

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

var bucket = admin.storage().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 = new FirebaseOptions.Builder()
    .setCredential(FirebaseCredentials.fromCertificate(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 http://googlecloudplatform.github.io/google-cloud-java/latest/apidocs/com/google/cloud/storage/Bucket.html
// 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': '<BUCKET_NAME>.appspot.com'
})

bucket = storage.bucket()

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

You can use the bucket references returned by the Admin SDK in conjunction with the official Google Cloud Storage client libraries to upload, download, and modify content in the buckets associated with your Firebase projects. Note that you do not have to authenticate Google Cloud Storage libraries when using the Firebase Admin SDK. The bucket references returned by the Admin SDK are already authenticated with the credentials used to initialize your Firebase app.

Use custom buckets

If you want to use a storage bucket other than the default provided above, or use multiple storage buckets in a single app, you can retrieve a reference to a custom bucket as shown below:

Node.js

var bucket = admin.storage().bucket("my-custom-bucket");

Java

Bucket bucket = StorageClient.getInstance().bucket("my-custom-bucket");

Python

bucket = storage.bucket('my-custom-bucket')

Use a custom Firebase app

If you are building a more complicated application that interacts with multiple Firebase apps, you can access the storage buckets assocaited with a specific Firebase app as follows:

Node.js

var bucket = customApp.storage().bucket();

Java

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

Python

bucket = storage.bucket(app=custom_app)

Google Cloud Storage client libraries

Firebase Admin SDK depends on the Google Cloud Storage client libraries to provide Cloud Storage access. The bucket references returned by the Admin SDK are objects defined in these libraries. Please refer to the documentation and API references of the Google Cloud Storage client libraries to learn how to use the returned bucket references in use cases like file upload and download.

Send feedback about...

Need help? Visit our support page.