Skip to content
Storage

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 身份验证,生成一对凭证(访问密钥 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.

Storage S3 Access keys
1
import { S3Client } from '@aws-sdk/client-s3';
2
3
const 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
})

会话令牌 #

🌐 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: anonKeypublishableKey 尚不支持
  • 会话令牌: valid jwt token

例如,使用 aws-sdk 库:

🌐 For example, using the aws-sdk library:

1
import { S3Client } from '@aws-sdk/client-s3'
2
3
const {
4
data: { session },
5
} = await supabase.auth.getSession()
6
7
const 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
})