Join us for Firebase Summit on November 10, 2021. Tune in to learn how Firebase can help you accelerate app development, release with confidence, and scale with ease. Register

Admin Cloud Storage API 簡介

雲存儲的火力地堡中存儲你的數據谷歌雲存儲,高可用性和冗餘全球的艾字節規模的對象存儲解決方案-鬥。 Firebase Admin SDK 允許您從特權環境直接訪問您的 Cloud Storage 存儲分區。然後你可以使用谷歌雲存儲API來處理存儲在桶中的對象。

使用默認存儲桶

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

節點.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.

爪哇

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.

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/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.

您可以使用管理SDK結合官方返回鬥引用谷歌雲存儲客戶端庫上傳,下載和修改與您的火力地堡項目有關的內容桶。請注意,在使用 Firebase Admin SDK 時,您不必對 Google Cloud Storage 庫進行身份驗證。 Admin SDK 返回的存儲桶引用已使用用於初始化 Firebase 應用的憑據進行身份驗證。

使用自定義存儲桶

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

節點.js

const bucket = getStorage().bucket('my-custom-bucket');

爪哇

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

Python

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

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

使用自定義 Firebase 應用

如果你正在建設一個更複雜的應用程序,與之交互的多個火力地堡的應用程序,您可以訪問特定應用程序火力地堡相關的雲端存儲分區如下:

節點.js

const bucket = getStorage(customApp).bucket();

爪哇

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

Python

bucket = storage.bucket(app=custom_app)

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

Google Cloud Storage 客戶端庫

在火力地堡管理軟件開發工具包依賴於谷歌雲存儲客戶端庫提供雲存儲服務。 Admin SDK 返回的存儲桶引用是在這些庫中定義的對象。請參閱谷歌雲存儲客戶端庫的文檔和API參考資料,以了解如何使用用例返回鬥引用,如文件的上傳下載