本頁面由 Cloud Translation API 翻譯而成。
Switch to English

Admin Cloud Storage API簡介

用於Firebase的Cloud Storage將您的數據存儲在Google Cloud Storage中 ,這是一種具有高可用性和全局冗餘的EB級對象存儲解決方案。 Firebase Admin SDK允許您從特權環境中直接訪問Google Cloud Storage存儲桶。然後,您可以使用Google Cloud Storage API來操作存儲在存儲桶中的對象。

使用默認存儲桶

您可以在初始化Admin SDK時指定默認存儲桶名稱。然後,您可以檢索對此存儲桶的經過身份驗證的引用。值區名稱不得包含gs://或任何其他協議前綴。例如,如果Firebase控制台中顯示的存儲桶URL為gs://bucket-name.appspot.com ,則將字符串bucket-name.appspot.com傳遞給Admin SDK。

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.

爪哇

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 http://googlecloudplatform.github.io/google-cloud-java/latest/apidocs/com/google/cloud/storage/Bucket.html
// for more details.

蟒蛇

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/latest/storage/buckets.html
# for more details.

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.

您可以將Admin SDK返回的存儲桶引用與正式的Google Cloud Storage客戶端庫結合使用,以上傳,下載和修改與Firebase項目關聯的存儲桶中的內容。請注意,使用Firebase Admin SDK時不必認證Google Cloud Storage庫。 Admin SDK返回的存儲桶引用已通過用於初始化Firebase應用程序的憑據進行了身份驗證。

使用自定義存儲桶

如果要使用上面提供的默認存儲桶以外的其他存儲桶,或在單個應用程序中使用多個存儲桶,則可以檢索對自定義存儲桶的引用,如下所示:

Node.js

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

爪哇

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

蟒蛇

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

 bucket, err := client.Bucket("my-custom-bucket")

使用自定義Firebase應用

如果您要構建一個與多個Firebase應用程序交互的更複雜的應用程序 ,則可以按以下方式訪問與特定Firebase應用程序關聯的存儲桶:

Node.js

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

爪哇

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

蟒蛇

bucket = storage.bucket(app=custom_app)

otherClient, err := otherApp.Storage(context.Background())
bucket, err := otherClient.Bucket("other-app-bucket")

Google Cloud Storage客戶端庫

Firebase Admin SDK依賴於Google Cloud Storage客戶端庫來提供Cloud Storage訪問。 Admin SDK返回的存儲桶引用是這些庫中定義的對象。請參考Google Cloud Storage客戶端庫的文檔和API參考,以了解如何在用例(如文件上傳下載)中使用返回的存儲桶引用。