與 Google Cloud 整合

Cloud Storage for Firebase 與 Google Cloud 緊密整合。Cloud Storage 專用的 Firebase SDK 會將檔案直接儲存在 Google Cloud Storage 值區中,隨著應用程式的規模增加,您也可以輕鬆整合其他 Google Cloud 服務,例如 App Engine 或 Cloud Functions 等代管運算服務,或是 Cloud Vision 或 Google Translate 等機器學習 API。

Firebase 會在不共用使用者憑證的情況下,使用 Google Cloud 服務帳戶來操作及管理服務。建立使用 Cloud Storage 的 Firebase 專案時,您可能會發現專案中已有對應的服務帳戶:service-<project number>@gcp-sa-firebasestorage.iam.gserviceaccount.com。詳情請參閱這份 Firebase 支援指南

實際上,Cloud Storage 專用的 Firebase SDK 會使用 App Engine 免費方案中的預設值區。這可讓您快速開始使用 Cloud Storage,而不必輸入信用卡或啟用 Cloud Billing 帳戶。也能讓您在 Firebase 和 Google Cloud 專案之間輕鬆共用資料。

您必須具備 Blaze 方案的 Firebase 專案,才能與 Google Cloud 整合,包括匯入現有的 Cloud Storage 值區。如要進一步瞭解方案,請前往定價頁面

Google Cloud Storage

您可以使用 Google Cloud Storage API 存取透過 Cloud Storage 專用 Firebase SDK 上傳的檔案,尤其是執行更複雜的作業,例如複製或移動檔案,或列出參照的所有可用檔案。

請注意,這些要求使用的是 Google Cloud Storage 存取權控管選項,而不是 Firebase 驗證和 Cloud Storage 安全性規則。

API

除了 Cloud Storage 專用的 Firebase SDK 外,您還可以根據您要執行的操作,透過多種方式存取 Cloud Storage 值區中儲存的資料。如果您存取伺服器上的資料,我們也提供伺服器端程式庫以及 JSON 和 S3 相容的 XML RESTful API。如需變更指令碼或執行其他管理工作,我們提供了實用的指令列工具。

Google Cloud 伺服器 SDK

Google Cloud 為多項雲端產品 (包括 Cloud Storage) 提供高品質的伺服器 SDK。這些程式庫提供 Node.jsJavagoPythonPHPRuby 版本。

如需安裝操作說明、驗證和疑難排解等更多資訊,請參閱上方連結的平台說明文件。

以下是 Google Cloud Storage SDK 的使用範例:

Node.js

    // Require gcloud
    var gcloud = require('google-cloud');

    // Enable Cloud Storage
    var gcs = gcloud.storage({
      projectId: 'grape-spaceship-123',
      keyFilename: '/path/to/keyfile.json'
    });

    // Reference an existing bucket.
    var bucket = gcs.bucket('my-existing-bucket');

    // Upload a local file to a new file to be created in your bucket.
    bucket.upload('/photos/zoo/zebra.jpg', function(err, file) {
      if (!err) {
        // "zebra.jpg" is now in your bucket.
      }
    });

    // Download a file from your bucket.
    bucket.file('giraffe.jpg').download({
      destination: '/photos/zoo/giraffe.jpg'
    }, function(err) {});
    

Java

    // Enable Cloud Storage
    Storage storage = StorageOptions.builder()
      .authCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json"))
      .build()
      .service();

    // Upload a local file to a new file to be created in your bucket.
    InputStream uploadContent = ...
    BlobId blobId = BlobId.of("my-existing-bucket", "zebra.jpg");
    BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
    Blob zebraBlob = storage.create(blobInfo, content);

    // Download a file from your bucket.
    Blob giraffeBlob = storage.get("my-existing-bucket", "giraffe.jpg", null);
    InputStream downloadContent = giraffeBlob.getInputStream();
    

Go

    // Enable Cloud Storage
    client, err := storage.NewClient(ctx, option.WithServiceAccountFile("path/to/keyfile.json"))
    if err != nil {
        log.Fatal(err)
    }

    // Download a file from your bucket.
    rc, err := client.Bucket("my-existing-bucket").Object("giraffe.jpg").NewReader(ctx)
    if err != nil {
        log.Fatal(err)
    }
    defer rc.Close()
    body, err := ioutil.ReadAll(rc)
    if err != nil {
        log.Fatal(err)
    }
    

Python

    # Import gcloud
    from google.cloud import storage

    # Enable Cloud Storage
    client = storage.Client()

    # Reference an existing bucket.
    bucket = client.get_bucket('my-existing-bucket')

    # Upload a local file to a new file to be created in your bucket.
    zebraBlob = bucket.get_blob('zebra.jpg')
    zebraBlob.upload_from_filename(filename='/photos/zoo/zebra.jpg')

    # Download a file from your bucket.
    giraffeBlob = bucket.get_blob('giraffe.jpg')
    giraffeBlob.download_as_string()
    

PHP

    // Require gcloud
    require 'vendor/autoload.php';
    use Google\Cloud\Storage\StorageClient;

    // Enable Cloud Storage
    $storage = new StorageClient([
        'projectId' => 'grape-spaceship-123'
    ]);

    // Reference an existing bucket.
    $bucket = $storage->bucket('my-existing-bucket');

    // Upload a file to the bucket.
    $bucket->upload(
        fopen('/photos/zoo/zebra.jpg', 'r')
    );

    // Download a file from your bucket.
    $object = $bucket->object('giraffe.jpg');
    $object->downloadToFile('/photos/zoo/giraffe.jpg');
    

小茹

    # Require gcloud
    require "google/cloud"

    # Enable Cloud Storage
    gcloud = Google::Cloud.new "grape-spaceship-123", "/path/to/keyfile.json"
    storage = gcloud.storage

    # Reference an existing bucket.
    bucket = storage.bucket "my-existing-bucket"

    # Upload a file to the bucket.
    bucket.create_file "/photos/zoo/zebra.jpg", "zebra.jpg"

    # Download a file from your bucket.
    file = bucket.file "giraffe.jpg"
    file.download "/photos/zoo/#{file.name}"
    

REST API

如果您使用沒有用戶端程式庫的語言、想要執行用戶端程式庫沒有的功能,或想使用您喜愛的 HTTP 用戶端,Google Cloud Storage 提供 JSONXML 適用的 API。

除了這些儲存空間資料存取 API,如要管理用於 Firebase 專案的 Cloud Storage 值區,您也可以使用 Cloud Storage for Firebase API

gsutil

gsutil 是一項指令列工具,可讓您直接存取 Cloud Storage。您可以使用 gsutil 執行多種值區和物件管理工作,包括:

  • 上傳、下載及刪除物件。
  • 列出值區和物件。
  • 移動、複製及重新命名物件。
  • 編輯物件和值區 ACL。

gsutil 可執行其他進階作業,例如將檔案從一個目錄移至另一個目錄,或刪除特定位置下的所有檔案。

將所有檔案從一個參照移至另一個參照,方法很簡單:

gsutil mv gs://bucket/old/reference gs://bucket/new/reference

批次刪除參照下方的所有檔案也同樣符合直覺:

# Delete all files under a path
gsutil rm -r gs://bucket/reference/to/delete

# Delete all the files in a bucket but not the bucket gsutil rm -r gs://bucket/**

# Delete all the files AND the bucket # Removing the default bucket will break the Firebase SDKs for Cloud Storage and is strongly discouraged gsutil rm -r gs://bucket

要求比率

Google Cloud Storage 是一項高擴充性服務,採用自動調整資源配置技術,要求比率極高。

Google Cloud Storage 屬於多用戶群服務,意味著使用者會共用同一組基礎資源。為了充分運用這些共用資源,值區設有初始 IO 容量。

如果您打算將 Cloud Storage for Firebase 整合到應用程式中,請考慮應用程式需要達到良好效能所需的最低要求率,以及如何有效率地提出要求。請參閱要求比率指南,尤其是提高要求比率

物件版本管理

您是否曾不小心刪除資料,且沒有備份資料? Google Cloud Storage 支援物件版本管理,這項功能會自動備份資料,並從這些備份還原。您可以使用 gsutil versioning set 指令啟用物件版本管理功能:

gsutil versioning set on gs://<your-cloud-storage-bucket>

Cloud Storage 一律會選擇最新版本,因此如果您要還原物件,您必須使用上述的其他 API 或工具,將所需物件設為最新版本。

物件生命週期管理

對許多應用程式而言,能夠自動封存或刪除過時檔案的功能是很實用的功能。幸好,Google Cloud Storage 提供物件生命週期管理功能,可讓您在一定時間後刪除或封存物件。

假設相片分享應用程式可在一天內刪除所有相片。您可以設定物件生命週期政策,如下所示:

// lifecycle.json
{
  "lifecycle": {
    "rule":
    [
      {
        "action": {"type": "Delete"},
        "condition": {"age": 1}
      }
    ]
  }
}

並使用 gsutil lifecycle set 指令部署:

gsutil lifecycle set lifecycle.json gs://<your-cloud-storage-bucket>

請注意,這適用於值區中的所有檔案,因此如果要儲存重要的使用者備份內容,以及要每天刪除的相片,您可能會想要使用兩個單獨的值區,或使用 gsutil 或自己的伺服器手動執行刪除作業。

App Engine

App Engine 是一種「平台式服務」,可根據其接收的流量自動調整後端邏輯。只要上傳後端程式碼,Google 就會管理應用程式的可用性,不必佈建或維護任何伺服器。App Engine 可讓您輕鬆快速地將額外的處理能力或受信任執行功能新增至 Firebase 應用程式。

Cloud Storage 適用的 Firebase SDK 會使用 App Engine 預設值區,這代表如果您建構 App Engine 應用程式,可以使用內建的 App Engine API 在 Firebase 和 App Engine 之間共用資料。適合執行音訊編碼、影片轉碼、圖片轉換,以及其他需要大量運算的背景處理。

App Engine 的 Java、Python 和 Go 標準環境包含 App Engine Images API (JavaPython),可以調整、旋轉、翻轉和裁剪圖片,並傳回圖片提供網址,允許用戶端轉換 (類似 Cloudinary 和 Imgix)。

將現有的 Google Cloud 專案匯入 Firebase 時,如果想在 Firebase 中使用任何現有的 App Engine 物件,您必須在物件上設定預設的存取權控管機制,以使用 gsutil 執行下列指令來允許 Firebase 存取這些物件:

gsutil -m acl ch -r -u service-<project number;gt@gcp-sa-firebasestorage.iam.gserviceaccount.com gs://<your-cloud-storage-bucket>

已知問題

您無法匯入 App Engine 應用程式的可能原因如下:

  1. 專案含有先前的 App Engine Datastore 主從應用程式。
  2. 這項專案含有網域前置字串的專案 ID,例如:domain.com:project-1234

無論是哪一種情況,專案都會不支援 Cloud Storage for Firebase,且您應建立新的 Firebase 專案,以便使用 Cloud Storage。請與支援團隊聯絡,以便我們提供協助。

Google Cloud Functions (Beta 版)

Google Cloud Functions 是輕量、事件導向的非同步運算解決方案,可讓您建立單一用途的小型函式來回應事件,而不必管理伺服器或執行階段環境。這些函式可用於影片轉碼、使用機器學習技術將圖片分類,或將中繼資料與 Firebase 即時資料庫同步處理。相較於 App Engine 的負擔更少,Cloud Functions 是回應 Cloud Storage 變更最快的方式。

Google Cloud Vision API

Google Cloud Vision API 將強大的機器學習模型封裝於容易使用的 API 中,讓開發人員瞭解圖片內容。它能快速將圖片分成數千種類別、偵測圖片中的個別物件與臉孔、尋找及讀取圖片中的印刷文字、識別令人反感的內容,甚至提供圖片情緒分析。

Google Cloud Speech API

與 Vision API 類似,Google Cloud Speech API 可讓開發人員從儲存在 Cloud Storage 的音訊檔案中擷取文字。這個 API 可辨識超過 80 種語言和方言,滿足全球各地的使用者族群。與 Google Cloud Natural Language API 搭配使用時,開發人員可以擷取原始文字並推論出該文字的意義。如果需要全球觀眾,只要搭配使用 Google Translate API,即可將文字翻譯成超過 90 種語言。