Firebase 專案其實就是啟用特定 Firebase 設定和服務的 Google Cloud 專案。也就是說,您在 Cloud Storage for Firebase 中使用的每個 Cloud Storage 儲存空間,都可以在 Google Cloud 中存取 (包括控制台和 API)。
服務帳戶注意事項
Firebase 會使用Google Cloud服務帳戶運作及管理服務,不會分享使用者憑證。建立使用 Cloud Storage 的 Firebase 專案時,您可能會發現專案中已提供對應的服務帳戶:。詳情請參閱「Firebase 服務帳戶總覽」。
Google Cloud Storage
您可以使用 Google Cloud Storage API 存取透過 Cloud Storage 的 Firebase SDK 上傳的檔案,特別是執行更複雜的作業,例如複製或移動檔案,或是列出參照中所有可用的檔案。
請注意,這些要求會使用Google Cloud Storage 存取權控管選項,而不是 Firebase Authentication 和 Cloud Storage Security Rules。
API
除了 Cloud Storage 的 Firebase SDK 之外,您還可以透過其他方式存取 Cloud Storage 儲存空間中的資料,具體取決於您的需求。如果您要存取伺服器上的資料,我們提供伺服器端程式庫,以及 JSON 和 S3 相容的 XML RESTful API。如果您需要編寫變更指令碼或執行其他管理工作,我們也提供實用的指令列工具。
Google Cloud 伺服器 SDK
Google Cloud 為多項雲端產品提供高品質伺服器 SDK,包括 Cloud Storage。這些程式庫適用於 Node.js、Java、Go、Python、PHP 和 Ruby。
如需更多資訊,包括安裝說明、驗證和疑難排解,請參閱上方連結的平台專屬說明文件。
以下是 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');
Ruby
# 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 提供 JSON 和 XML 格式的 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 或您自己的伺服器手動刪除檔案。
Google Cloud Functions (Beta 版)
Google Cloud Functions 是依事件執行的簡易非同步運算解決方案,可讓您建立單一用途的小型函式來回應事件,完全不須管理伺服器或執行階段環境。這些函式可用於轉碼影片、使用機器學習技術分類圖片,或將中繼資料與 Firebase Realtime Database 同步處理。Cloud Functions 的額外負擔比 App Engine 更少,是因應 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 翻譯 API,將文字翻譯成超過 90 種語言。
Google App Engine
Cloud Storage for Firebase 會根據預設值區的建立時間,使用兩種不同的命名格式:
- 2024 年
9 月 後建立的預設值區: 預設值區與 Google App Engine 無關。*.firebasestorage.app - 舊版預設值區:
預設值區會與 Google App Engine共用。*.appspot.com
Google App Engine 是一種「平台即服務」,可根據接收的流量自動調度後端邏輯。您只要上傳後端程式碼,Google 就會為您管理應用程式的可用性,不必佈建或維護伺服器。App Engine 可讓您快速輕鬆地為 Firebase 應用程式新增額外的處理能力或可信執行作業。
舊版 App Engine 整合
如果您有舊版 *.appspot.com
App Engine 的 Java、Python 和 Go 標準環境包含 App Engine Images API (Java | Python | Go),可調整大小、旋轉、翻轉及裁剪圖片,並傳回圖片服務網址,讓您在用戶端進行轉換,類似於 Cloudinary 和 Imgix。
共用值區注意事項
如果您使用舊版 *.appspot.com
Firebase Security Rules 和 App Engine 檔案:使用 App Engine 分享的檔案會共用同一個值區,並受相同的安全設定約束。如果將 Firebase Security Rules 設定為公開存取,新上傳的 App Engine 檔案也會公開。
設定權限:將現有 Google Cloud 專案匯入 Firebase 時,可能需要設定物件的預設存取控管,允許 Firebase 存取這些物件。使用
gsutil執行下列指令:gsutil -m acl ch -r -u service-PROJECT_NUMBER@gcp-sa-firebasestorage.iam.gserviceaccount.com gs://BUCKET_NAME
Cloud Storage 和 App Engine 的已知問題
目前已知有兩種情況會導致無法匯入 App Engine 應用程式:
專案包含先前的 App Engine Datastore Master/Slave 應用程式。
專案的專案 ID 前面加上網域,例如:
domain.com:project-1234。
在上述任一情況下,專案都不支援 Cloud Storage for Firebase,因此您應建立新的 Firebase 專案,才能使用 Cloud Storage。如有任何問題,請與 Firebase 支援團隊聯絡。