图像转换与签名 URL
按需提供调整尺寸并经过优化的图像,并使用 Supabase Storage 签名 URL 临时授予对私有文件的访问权限。
图像转换与签名 URL 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Smarter File Delivery
Supabase Storage can transform images on request and create signed URLs for time-limited access to private files. Both reduce custom backend work.
Public vs Private Buckets
Public buckets serve files via a permanent URL. Private buckets require a signed URL or an authenticated request, controlled by policies.
Getting a Public URL
For public buckets, build the URL directly, no token needed.
const { data } = supabase.storage
.from('avatars')
.getPublicUrl('user-42.png');
console.log(data.publicUrl);On-the-Fly Resizing
Pass transform options to request a resized version. Supabase generates and caches it.
const { data } = supabase.storage
.from('avatars')
.getPublicUrl('user-42.png', {
transform: { width: 100, height: 100, resize: 'cover' }
});Why Transform at the Edge?
Serving a 100px thumbnail instead of a full-size photo saves bandwidth and speeds up pages, without storing many copies yourself.
Quality and Format
You can also tune quality and request modern formats to shrink payloads further.
function thumbOpts(size) {
return { width: size, height: size, quality: 75, resize: 'cover' };
}
console.log(thumbOpts(64));Creating a Signed URL
For private files, generate a URL that expires after a set number of seconds.
const { data } = await supabase.storage
.from('invoices')
.createSignedUrl('inv-2026.pdf', 60);
console.log(data.signedUrl);Choosing an Expiry
Keep expiry as short as the use case allows. A download link might need 60 seconds; an embedded image might need an hour.
function expirySeconds(useCase) {
return useCase === 'download' ? 60 : 3600;
}
console.log(expirySeconds('download'));Signed URLs With Transforms
You can combine a signed URL with an image transform to serve resized private images securely.
Caching Behavior
Transformed images are cached at the edge, so repeated requests for the same size are fast. Vary the size sparingly to keep cache hits high.
Putting It Together
Use public URLs and transforms for open assets, and signed URLs for private files, with short expiries and edge caching for performance.
Quick Check
Test your understanding of URLs and transforms.
Recap
You learned to build public URLs, apply on-the-fly image transforms for performance, and create expiring signed URLs for private files, plus how edge caching keeps it fast.
常见问题解答
「图像转换与签名 URL」课时是免费的吗?
是的 — 「图像转换与签名 URL」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 4 节课。
「图像转换与签名 URL」这节课中我会学到什么?
按需提供调整尺寸并经过优化的图像,并使用 Supabase Storage 签名 URL 临时授予对私有文件的访问权限。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Supabase Backend as a Service 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「图像转换与签名 URL」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?
能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 在 Supabase 存储桶中存储文件
- 使用策略管理访问权限
- 上传并获取媒体文件
- 图像转换与签名 URL