创建桶
你可以使用 Supabase 控制面板创建一个存储桶。由于存储与你的 Postgres 数据库互通,你也可以使用 SQL 或我们的客户端库。这里我们创建了一个名为“avatars”的存储桶:
🌐 You can create a bucket using the Supabase Dashboard. Since storage is interoperable with your Postgres database, you can also use SQL or our client libraries. Here we create a bucket called "avatars":
1import { createClient } from '@supabase/supabase-js'2const supabase = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_KEY!)34// ---cut---5// Use the JS library to create a bucket.67const { data, error } = await supabase.storage.createBucket('avatars', {8 public: true, // default: false9})限制上传 #
🌐 Restricting uploads
在创建存储桶时,你可以添加额外的配置来限制这个存储桶中可以包含的文件类型或大小。
🌐 When creating a bucket you can add additional configurations to restrict the type or size of files you want this bucket to contain.
例如,假设你想允许用户只向 avatars 桶上传图片,并且大小不能超过 1MB。你可以通过提供 allowedMimeTypes 和 maxFileSize 来实现以下内容:
🌐 For example, imagine you want to allow your users to upload only images to the avatars bucket and the size must not be greater than 1MB. You can achieve the following by providing allowedMimeTypes and maxFileSize:
1import { createClient } from '@supabase/supabase-js'2const supabase = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_KEY!)34// ---cut---5// Use the JS library to create a bucket.67const { data, error } = await supabase.storage.createBucket('avatars', {8 public: true,9 allowedMimeTypes: ['image/*'],10 fileSizeLimit: '1MB',11})如果上传请求不符合上述限制,它将被拒绝。更多信息请参见 文件限制。
🌐 If an upload request doesn't meet the above restrictions it will be rejected. See File Limits for more information.