在一般應用程式中,開發人員必須建構及維護許多執行 以及驗證、授權和資料驗證 商業邏輯使用「Cloud Storage for Firebase」的應用程式會使用 Firebase Authentication 和 Firebase Security Rules (需要 Cloud Storage 處理無伺服器作業) 驗證、授權和資料驗證
使用 Cloud Storage 和 Cloud Storage Security Rules 就表示您可以專注於 打造絕佳使用者體驗,而無需管理基礎架構或 編寫複雜的伺服器端驗證和授權碼!
總覽
Cloud Storage Security Rules 可用於判斷誰具有讀取和寫入權限
儲存在 Cloud Storage 中的檔案,以及檔案的結構
或所含中繼資料規則的基本類型是 allow
規則,
如果選擇性條件為,則允許 read
和 write
作業
因此建議您建立快訊政策
在符合特定條件時通知您以下列舉一些規則:
// Rules can optionally specify a condition allow write: if <condition>;
代表規則 match
檔案路徑
Cloud Storage 參考資料。規則
可能會match
一或多個檔案路徑,而多項規則可match
這個檔案
指定 request
中的路徑:
// Rules match specific paths match /images/profilePhoto.png { allow write: if <condition>; } match /images/croppedProfilePhoto.png { allow write: if <other_condition>; }
規則評估的情境也會透過 request
和
resource
物件,提供驗證環境等資訊
(request.auth
) 和現有物件的大小 (resource.size
)。
// Rules can specify conditions that consider the request context match /images/profilePhoto.png { allow write: if request.auth != null && request.resource.size < 5 * 1024 * 1024; }
如要進一步瞭解「Cloud Storage Security Rules」,請前往 安全檔案一節。
範例規則
Cloud Storage Security Rules 必須先指定 service
(在本例中為
firebase.storage
),以及 Cloud Storage 值區
(透過 match /b/{bucket}/o
)
做為評估依據預設規則需要 Firebase Authentication,但以下是
一些具有不同存取權控管的常見規則範例。
預設
// Only authenticated users can read or write to the folder
service firebase.storage {
match /b/{bucket}/o {
match /someFolder/{fileName} {
allow read, write: if request.auth != null;
}
}
}
公開
// Anyone can read or write to the folder, even non-users of your app.
// Because it is shared with App Engine, this will also make
// files uploaded via App Engine public.
service firebase.storage {
match /b/{bucket}/o {
match /someFolder/{fileName} {
allow read, write;
}
}
}
使用者
// Grants a user access to a node matching their user ID
service firebase.storage {
match /b/{bucket}/o {
// Files look like: "user/<UID>/file.txt"
match /user/{userId}/{fileName} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}
私人
// Access to files through Cloud Storage for Firebase is completely disallowed.
// Files may still be accessible through App Engine or Google Cloud Storage APIs.
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
}
}
}
在開發期間,您可以使用公開規則來取代預設規則 允許您將檔案設為可公開讀取及寫入這對使用者來說非常實用 ,不必設定 Firebase Authentication 即可開始使用 但因為 Cloud Storage 與預設共用值區 App Engine 個應用程式,這項規則也會讓該應用程式使用的所有資料 也公開發布
使用者規則可讓您為通過驗證的每位使用者提供各自的個人資訊 以及檔案儲存空間或是使用私密瀏覽模式 但請注意,您的使用者將無法讀取或撰寫任何內容 透過 Cloud Storage 套用這些規則。從以下使用者存取檔案的使用者: 您的 App Engine 應用程式或 Google Cloud Storage 個 API 可能仍有存取權。
編輯規則
Cloud Storage 可讓你輕鬆編輯Cloud Storage Security Rules
(位於 Firebase 控制台「儲存空間」部分的「規則」分頁)。
您可以在「規則」分頁中,輕鬆快速地查看並修改目前的
不過,編寫這類演算法並不容易
因為我們無法寫出所有可能的規則如要部署這些規則,請點選「發布」或儲存檔案。
(ctrl/cmd + s
)。規則會立即上傳至「Cloud Storage」
但最多可能需要五分鐘的時間才會生效。
Firebase CLI 也可以用來部署規則。如果您選取:
執行 firebase init
時,Storage
,1 個 storage.rules
檔案,副本:
系統會建立預設規則
。您可以使用
firebase deploy
指令。如果專案中有多個值區
可以使用部署目標,將規則
同時從同一個專案資料夾中
匯入 10 個值區
如要進一步瞭解檔案式安全性的運作方式,請參閱 「安全檔案」部分,或瞭解使用者 保護使用者 專區。