정책으로 접근 권한 관리
Supabase Storage 정책을 사용하여 파일에 대한 세밀한 접근 제어를 구현하고 데이터 보안을 보장합니다.
정책으로 접근 권한 관리은(는) CoddyKit의 무료 Supabase Backend as a Service 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Supabase Backend as a Service 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Supabase Backend as a Service 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Intro to Storage Policies
Welcome! In this lesson, we'll dive into Supabase Storage Policies. These policies are your key to controlling who can access, upload, or delete files in your buckets.
Think of them as security guards for your cloud files, ensuring only authorized actions happen.
Why Storage Security Matters
Without proper policies, your files could be vulnerable. Anyone could potentially upload malicious content, or sensitive user data might be publicly exposed.
- Prevent unauthorized access: Keep private files secure.
- Control uploads: Ensure only legitimate users can add files.
- Manage deletions: Prevent accidental or malicious file removal.
Policies are essential for building secure applications.
Enabling RLS for Buckets
Just like with database tables, Supabase Storage uses Row-Level Security (RLS) for buckets. Before you can apply any policies, RLS must be enabled for your storage bucket.
You can do this in the Supabase dashboard under 'Storage' by clicking on a bucket and toggling 'Enable RLS', or directly with SQL:
ALTER TABLE storage.buckets
ENABLE ROW LEVEL SECURITY;Understanding Policy Syntax
Storage policies are written using SQL, similar to database RLS. They specify what actions are allowed (SELECT, INSERT, UPDATE, DELETE) and under what conditions.
Key parts of a policy:
- CREATE POLICY: Starts the policy definition.
- ON storage.objects: Specifies the table policies apply to.
- FOR [action]: Defines the operation (SELECT, INSERT, UPDATE, DELETE).
- USING / WITH CHECK: Sets the conditions for the policy.
Policy: Public Read Access
Let's create a policy to allow anyone to read files from a specific bucket, for example, a 'public-images' bucket. This is common for assets like profile pictures or product images.
The auth.role() = 'anon' part means unauthenticated users, and auth.role() = 'authenticated' means logged-in users. We'll allow both here.
CREATE POLICY "Allow public read access"
ON storage.objects FOR SELECT
TO public
USING (bucket_id = 'public-images');Policy: Authenticated User Uploads
Now, let's create a policy that allows only authenticated users to upload files to a 'user-uploads' bucket. Crucially, we want them to only upload into a folder named after their own user ID.
We use auth.uid() to get the current user's ID and path_tokens[1] to check the first part of the file path.
CREATE POLICY "Allow authenticated user upload"
ON storage.objects FOR INSERT
TO authenticated
WITH CHECK (bucket_id = 'user-uploads' AND auth.uid()::text = path_tokens[1]);Policy: Authenticated User Read Their Own
To complement the upload policy, let's ensure authenticated users can *only* read files that they themselves own within the 'user-uploads' bucket.
This policy uses auth.uid() to match the owner of the file (which is stored in the owner column of storage.objects) and also checks the file path.
CREATE POLICY "Allow authenticated user read their own"
ON storage.objects FOR SELECT
TO authenticated
USING (bucket_id = 'user-uploads' AND auth.uid() = owner);The storage.objects Table
Storage policies operate on the hidden storage.objects table. This table stores metadata about every file in your buckets. Understanding its columns is vital for writing effective policies.
- id: Unique identifier for the object.
- bucket_id: The ID of the bucket the object belongs to.
- name: The full path and filename.
- owner: The
auth.uid()of the user who uploaded the file. - path_tokens: An array of strings representing the path segments.
Policy: Admin-Only Delete
Sometimes, only specific users (like administrators) should be able to delete files. Let's create a policy that allows deletion only if the user has a specific role, for example, an 'admin' role.
This requires a custom function to check user roles, or you can simplify by checking a specific `owner` UID if only one admin account exists.
CREATE POLICY "Allow admins to delete"
ON storage.objects FOR DELETE
TO authenticated
USING (bucket_id = 'sensitive-data' AND auth.jwt() ->> 'user_role' = 'admin');Testing Policies Effectively
After creating policies, it's crucial to test them thoroughly. You can do this by:
- Logging in as different users: Test with authenticated users, unauthenticated users, and users with different roles.
- Attempting forbidden actions: Try to upload, read, or delete files that your policies should prevent.
- Using the Supabase client: Make API calls with
supabase-jsor other client libraries and observe the responses (e.g., expecting 403 Forbidden errors).
Always verify your policies work as intended before deploying to production.
Quick Policy Check
You've learned how to create various Storage policies. Which of the following conditions would allow an authenticated user to insert a file into a bucket named 'private-docs' ONLY if the file is placed in a folder matching their user ID?
Recap: Storage Policies
Great job! You've learned how to secure your Supabase Storage with policies. We covered:
- The importance of RLS for storage buckets.
- Creating policies for various actions (read, insert, delete).
- Using
auth.uid()andpath_tokensfor granular control. - Understanding the
storage.objectstable.
Implementing strong storage policies is crucial for the security and integrity of your application's file management.
AI 튜터와 함께 Supabase Backend as a Service을(를) 배우세요 — 무료
브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.
- 코스
- 11
- 레슨
- 40
자주 묻는 질문
“정책으로 접근 권한 관리” 강의는 무료인가요?
네 — “정책으로 접근 권한 관리” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Supabase Backend as a Service 강의 전체를 잠금 해제할 수 있습니다. Supabase Backend as a Service 강의에는 총 4개의 강의가 포함되어 있습니다.
“정책으로 접근 권한 관리”에서 뭘 배우나요?
Supabase Storage 정책을 사용하여 파일에 대한 세밀한 접근 제어를 구현하고 데이터 보안을 보장합니다. 브라우저에서 직접 실행하는 실습 코드로 Supabase Backend as a Service을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Supabase Backend as a Service을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Supabase Backend as a Service은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“정책으로 접근 권한 관리” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Supabase Backend as a Service 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Supabase Backend as a Service 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Supabase 버킷에 파일 저장
- 정책으로 접근 권한 관리
- 미디어 업로드와 가져오기
- 이미지 변환과 서명된 URL