S3 认证
Learn about authenticating with Supabase Storage S3.
你有两种方式来通过 Supabase Storage S3 进行身份验证:
🌐 You have two options to authenticate with Supabase Storage S3:
- 使用从你的项目设置生成的 S3 访问密钥(仅供服务器端使用)
- 使用会话令牌,这将允许你使用用户 JWT 令牌进行身份验证,并通过行级安全(RLS)提供有限访问。
S3 访问密钥 #
🌐 S3 access keys
保管好这些凭证
S3 访问密钥可以完全访问所有 S3 操作和所有存储桶,还能绕过 RLS 策略。这些只打算在服务器上使用。
🌐 S3 access keys provide full access to all S3 operations across all buckets and bypass RLS policies. These are meant to be used only on the server.
要进行 S3 身份验证,生成一对凭证(访问密钥 ID 和秘密访问密钥),然后从S3 配置页面复制端点和区域。
🌐 To authenticate with S3, generate a pair of credentials (Access Key ID and Secret Access Key), copy the endpoint and region from the S3 configuration page.
这是你连接到 Supabase 存储并使用任何兼容 S3 的服务所需的所有信息。
🌐 This is all the information you need to connect to Supabase Storage using any S3-compatible service.

为了在上传大文件时获得最佳性能,你应该始终使用直接存储主机名。这能提供多种性能提升,大大改善上传大文件时的表现。
🌐 For optimal performance when uploading large files you should always use the direct storage hostname. This provides several performance enhancements that will greatly improve performance when uploading large files.
别用 https://project-id.supabase.co,用 https://project-id.storage.supabase.co
🌐 Instead of https://project-id.supabase.co use https://project-id.storage.supabase.co
1import { S3Client } from '@aws-sdk/client-s3';23const client = new S3Client({4 forcePathStyle: true,5 region: 'project_region',6 endpoint: 'https://project_ref.storage.supabase.co/storage/v1/s3',7 credentials: {8 accessKeyId: 'your_access_key_id',9 secretAccessKey: 'your_secret_access_key',10 }11})在本地开发中,使用这些数值:
🌐 On local development, use these values:
region:localendpoint:IP 和端口,例如http://127.0.0.1:54321/storage/v1/s3
会话令牌 #
🌐 Session token
你可以使用用户 JWT 令牌来验证 Supabase S3,从而通过 RLS 为所有 S3 操作提供有限访问。这在你想在服务器上以特定用户为范围初始化 S3 客户端,或者直接在客户端使用 S3 客户端时非常有用。
🌐 You can authenticate to Supabase S3 with a user JWT token to provide limited access via RLS to all S3 operations. This is useful when you want initialize the S3 client on the server scoped to a specific user, or use the S3 client directly from the client side.
使用会话令牌执行的所有 S3 操作都仅限于已认证的用户。存储架构上的 RLS 策略也会被遵守。
🌐 All S3 operations performed with the Session Token are scoped to the authenticated user. RLS policies on the Storage Schema are respected.
要使用会话令牌进行 S3 身份验证,请使用以下凭证:
🌐 To authenticate with S3 using a Session Token, use the following credentials:
- 访问密钥ID:
project_ref - secret_access_key:
anonKey(publishableKey尚不支持) - 会话令牌:
valid jwt token
例如,使用 aws-sdk 库:
🌐 For example, using the aws-sdk library:
通常我们建议不要使用 getSession,因为会话信息是从本地存储读取的,你不能依赖它来做认证决策。不过在这个例子中,代码只需要原始访问令牌字符串,将其作为凭证转发给 S3 服务,由服务器端验证令牌。因为没有基于会话数据在客户端做认证决策,所以这里用 getSession 是合适的。
🌐 Typically we advise against using getSession, because the session is read from local storage and you can't trust its claims for auth decisions. In this case however, the code only needs the raw access token string to forward as a credential to the S3 service, which validates the token server-side. Since no client-side auth decision is made based on the session data, getSession is appropriate here.
1import { S3Client } from '@aws-sdk/client-s3'23const {4 data: { session },5} = await supabase.auth.getSession()67const client = new S3Client({8 forcePathStyle: true,9 region: 'project_region',10 endpoint: 'https://project_ref.storage.supabase.co/storage/v1/s3',11 credentials: {12 accessKeyId: 'project_ref',13 secretAccessKey: 'anonKey',14 sessionToken: session.access_token,15 },16})