瞭解 Cloud Storage 的 Firebase 安全性規則
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
傳統上,安全性是應用程式開發中最複雜的部分之一。在大多數應用程式中,開發人員必須建構及執行伺服器,處理驗證 (使用者身分) 和授權 (使用者可執行的動作)。驗證和授權設定不易,更難確保正確無誤,但對產品的成功至關重要。
與 Firebase Authentication 讓您輕鬆驗證使用者身分的方式類似,Cloud Storage 的 Firebase Security Rules 可讓您輕鬆授權使用者及驗證要求。Cloud Storage Security Rules 可讓您指定路徑型權限,藉此管理複雜度。您只需幾行程式碼,即可編寫授權規則,限制對特定使用者發出的 Cloud Storage 要求,或限制上傳大小。
Firebase Realtime Database 也有類似功能,稱為「Firebase Realtime Database Security Rules」
驗證
瞭解使用者身分是建構應用程式的重要環節,而 Firebase Authentication 提供簡單易用、安全且僅限於用戶端的驗證解決方案。Firebase Security Rules,Cloud Storage與使用者安全息息相關。Firebase Authentication
使用者透過 Firebase Authentication 驗證身分後,Cloud Storage Security Rules 中的 request.auth
變數會成為物件,內含使用者的專屬 ID (request.auth.uid
) 和權杖中的所有其他使用者資訊 (request.auth.token
)。如果使用者未通過驗證,request.auth
會是 null
。這樣一來,您就能以使用者為單位,安全地控管資料存取權。詳情請參閱「驗證」一節。
授權
辨識使用者只是安全防護的一環,瞭解共用對象後,您需要控管他們在 Cloud Storage 中存取檔案的權限。
Cloud Storage 可讓您指定每個檔案和路徑的授權規則,這些規則會存放在我們的伺服器上,並決定應用程式中檔案的存取權。舉例來說,預設的 Cloud Storage Security Rules 會要求 Firebase Authentication,才能對所有檔案執行任何 read
或 write
作業:
service firebase.storage {
match /b/{bucket}/o {
match /someFolder/{fileName} {
allow read, write: if request.auth != null;
}
}
}
如要編輯這些規則,請在 Firebase 控制台中選取 Firebase 應用程式,然後查看「儲存空間」部分的「規則」Rules
分頁。
資料驗證
Firebase Security Rules Cloud Storage 也可用於資料驗證,包括驗證檔案名稱和路徑,以及 contentType
和 size
等檔案中繼資料屬性。
service firebase.storage {
match /b/{bucket}/o {
match /images/{imageId} {
// Only allow uploads of any image file that's less than 5MB
allow write: if request.resource.size < 5 * 1024 * 1024
&& request.resource.contentType.matches('image/.*');
}
}
}
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-24 (世界標準時間)。
[null,null,["上次更新時間:2025-07-24 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nTraditionally, security has been one of the most complex parts of app\ndevelopment. In most applications, developers must build and run a server that\nhandles authentication (who a user is) and authorization (what a user can do).\nAuthentication and authorization are hard to set up, harder to get right, and\ncritical to the success of your product.\n\nSimilar to how Firebase Authentication makes it easy for you to authenticate your\nusers, Firebase Security Rules for Cloud Storage makes it easy for you to authorize users\nand validate requests. Cloud Storage Security Rules manage the complexity for you by\nallowing you to specify path based permissions. In just a few lines of code, you\ncan write authorization rules that restrict Cloud Storage requests to a\ncertain user or limit the size of an upload.\n| **Note:** If you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nThe Firebase Realtime Database has a similar feature, called\n[Firebase Realtime Database Security Rules](/docs/database/security)\n\nAuthentication\n\nKnowing who your users are is an important part of building an application, and\nFirebase Authentication provides an easy to use, secure, client side only solution\nto authentication. Firebase Security Rules for Cloud Storage ties in to Firebase Authentication\nfor user based security. When a user is authenticated with Firebase Authentication,\nthe `request.auth` variable in Cloud Storage Security Rules becomes an object that\ncontains the user's unique ID (`request.auth.uid`) and all other user\ninformation in the token (`request.auth.token`). When the user is not\nauthenticated, `request.auth` is `null`. This allows you to securely control\ndata access on a per-user basis. You can learn more in the\n[Authentication](/docs/storage/security/rules-conditions#authentication) section.\n\nAuthorization\n\nIdentifying your user is only part of security. Once you know who they are, you\nneed a way to control their access to files in Cloud Storage.\n\nCloud Storage lets you specify per file and per path authorization\nrules that live on our servers and determine access to the files in your app.\nFor example, the default Cloud Storage Security Rules require Firebase Authentication in\norder to perform any `read` or `write` operations on all files: \n\n```css+lasso\nservice firebase.storage {\n match /b/{bucket}/o {\n match /someFolder/{fileName} {\n allow read, write: if request.auth != null;\n }\n }\n}\n```\n\nYou can edit these rules by selecting a Firebase app in the [Firebase console](//console.firebase.google.com/)\nand viewing the `Rules` tab of the Storage section.\n\nData Validation\n\nFirebase Security Rules for Cloud Storage can also be used for data validation, including\nvalidating file name and path as well as file metadata properties such as\n`contentType` and `size`. \n\n```gdscript\nservice firebase.storage {\n match /b/{bucket}/o {\n match /images/{imageId} {\n // Only allow uploads of any image file that's less than 5MB\n allow write: if request.resource.size \u003c 5 * 1024 * 1024\n && request.resource.contentType.matches('image/.*');\n }\n }\n}\n```\n\nNext steps\n\n- [Get started](/docs/storage/security/get-started) planning rules development\n for your Cloud Storage buckets.\n\n- Learn more about [securing your data](/docs/storage/security/core-syntax)\n using security rules."]]