與穀歌云集成

Cloud Storage for Firebase 與Google Cloud緊密整合。適用於 Cloud Storage 的 Firebase SDK 將檔案直接儲存在Google Cloud Storage 儲存桶中,隨著應用程式的成長,您可以輕鬆整合其他Google Cloud 服務,例如App Engine 或Cloud Functions 等託管運算,或Cloud Vision 或Google 等機器學習API翻譯。

Firebase 使用 Google Cloud 服務帳號來操作和管理服務,而無需共用使用者憑證。當您建立使用 Cloud Storage 的 Firebase 專案時,您可能會注意到專案中已有對應的服務帳號: service-<project number>@gcp-sa-firebasestorage.iam.gserviceaccount.com 。有關詳細信息,請參閱此Firebase 支援指南

在幕後,Firebase SDK for Cloud Storage 使用App Engine免費層中的預設儲存桶。這使您可以快速啟動並運行 Cloud Storage,而無需插入信用卡或啟用 Cloud Billing 帳戶。它還可以讓您輕鬆地在 Firebase 和 Google Cloud 專案之間共享資料。

與 Google Cloud 整合(包括匯入現有的 Cloud Storage 儲存桶)需要 Blaze 計畫上的 Firebase 專案。在我們的定價頁面上了解有關計劃的更多資訊。

谷歌雲端儲存

您可以使用Google Cloud Storage API存取透過適用於 Cloud Storage 的 Firebase SDK 上傳的文件,尤其是執行更複雜的操作,例如複製或移動文件,或列出參考中的所有可用文件。

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

蜜蜂

除了適用於 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) {});
    

爪哇

    // 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();
    

    // 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}"
    

休息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 是一種“平台即服務”,可根據收到的流量自動擴展後端邏輯。只需上傳您的後端程式碼,Google 就會管理您的應用程式的可用性;沒有伺服器可供您配置或維護。 App Engine 是一種快速、簡單的方法,可為您的 Firebase 應用程式添加額外的處理能力或可信任執行。

適用於雲端儲存的 Firebase SDK 使用 App Engine 預設儲存桶,這表示如果您建置 App Engine 應用,則可以使用內建 App Engine API 在 Firebase 和 App Engine 之間共用資料。這對於執行音訊編碼、視訊轉碼和圖像轉換以及其他計算密集型後台處理非常有用。

App Engine 的 Java、Python 和 go標準環境包括 App Engine 圖像 API( JavaPython ),它可以調整圖像大小、旋轉、翻轉和裁剪圖像,並返回允許客戶端轉換的圖像服務 URL ,類似於Cloudinary 和Imgix 。

將現有 Google Cloud 專案匯入 Firebase 時,如果您想要讓任何現有 App Engine 物件在 Firebase 中可用,則需要使用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 資料儲存主/從應用程式。
  2. 此專案具有以網域為前綴的專案 ID,例如: domain.com:project-1234 1234 。

在這兩種情況下,專案都不支援 Cloud Storage for Firebase,您應該建立一個新的 Firebase 專案才能使用 Cloud Storage。聯絡支援人員以便我們為您提供協助。

Google 雲端函數(測試版)

Google Cloud Functions是一種輕量級、基於事件的非同步運算解決方案,可讓您建立小型、單一用途的函數來回應事件,而無需管理伺服器或執行時間環境。這些函數可用於對視訊進行轉碼、使用機器學習對影像進行分類或將元資料與 Firebase 即時資料庫同步。 Cloud Functions 的開銷甚至比 App Engine 還要少,是對雲端儲存中的變更做出反應的最快方式。

Google雲端視覺API

Google Cloud Vision API將強大的機器學習模型封裝在易於使用的 API 中,使開發人員能夠理解圖像的內容。它可以快速將影像分類為數千個類別,偵測影像中的單一物件和臉部,尋找和讀取影像中包含的印刷文字,識別攻擊性內容,甚至提供影像情緒分析。

谷歌雲端語音API

與 Vision API 類似, Google Cloud Speech API使開發人員能夠從儲存在 Cloud Storage 中的音訊檔案中提取文字。該 API 可識別 80 多種語言及其變體,以支援您的全球用戶群。當與Google Cloud Natural Language API結合使用時,開發人員既可以提取原始文本,又可以推斷該文本的含義。如果需要全球受眾,請將其與Google Translate API結合起來,將文字翻譯成 90 多種語言。