일반적인 앱에서 개발자는 인증, 승인, 데이터 검증 및 개발자의 비즈니스 로직을 수행하는 여러 서버를 구축하고 관리해야 합니다. Cloud Storage for Firebase를 사용하는 앱은 Cloud Storage용 Firebase Authentication 및 Firebase Security Rules를 사용하여 서버리스 인증, 승인, 데이터 유효성 검사를 처리합니다.
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>;
규칙은 Cloud Storage 참조를 나타내는 파일 경로를 match
로 처리합니다. 규칙은 하나 이상의 파일 경로를 match
로 처리할 수 있으며 하나 이상의 규칙이 특정 request
의 파일 경로를 match
로 처리할 수 있습니다.
// Rules match specific paths match /images/profilePhoto.png { allow write: if <condition>; } match /images/croppedProfilePhoto.png { allow write: if <other_condition>; }
규칙 검증의 컨텍스트도 request
및 resource
객체를 통해 노출되며 이러한 객체는 auth 컨텍스트(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를 사용하면 Firebase Console 저장소 섹션의 규칙 탭을 통해 Cloud Storage Security Rules를 쉽게 수정할 수 있습니다.
규칙 탭에서 사용 중인 규칙을 빠르고 손쉽게 확인하고 수정할 수 있습니다. 이러한 규칙을 배포하려면 게시를 클릭하거나 파일을 저장합니다(ctrl/cmd + s
). 규칙은 Cloud Storage 서버에 즉시 업로드되지만 적용되려면 최대 5분이 걸릴 수 있습니다.
Firebase CLI를 사용하여 규칙을 배포할 수도 있습니다. firebase init
을 실행할 때 Storage
를 선택하면 기본 규칙의 사본이 포함된 storage.rules
파일이 프로젝트 디렉터리에 생성됩니다. firebase deploy
명령어를 사용하여 이러한 규칙을 배포할 수 있습니다. 프로젝트에 여러 버킷이 있는 경우 배포 대상을 사용하여 동일한 프로젝트 폴더에 있는 모든 버킷에 규칙을 동시에 배포할 수 있습니다.
파일 보안 섹션에서 파일 기반 보안의 원리를 알아보거나 사용자 보안 섹션에서 사용자 기본 보안에 대해 알아보세요.