复制对象
Learn how to copy and move objects
复制对象 #
🌐 Copy objects
你可以在不同的桶之间或者在同一个桶内复制对象。目前通过 API 只能复制最大 5 GB 的对象。
🌐 You can copy objects between buckets or within the same bucket. Currently only objects up to 5 GB can be copied using the API.
当复制一个对象时,新对象的所有者将是发起复制操作的用户。
🌐 When making a copy of an object, the owner of the new object will be the user who initiated the copy operation.
在同一个桶中复制对象 #
🌐 Copying objects within the same bucket
要在同一个存储桶内复制对象,使用 copy 方法。
🌐 To copy an object within the same bucket, use the copy method.
1import { createClient } from '@supabase/supabase-js'2const supabase = createClient('your_project_url', 'your_supabase_api_key')34// ---cut---5await supabase.storage.from('avatars').copy('public/avatar1.png', 'private/avatar2.png')在不同存储桶之间复制对象 #
🌐 Copying objects across buckets
要在不同存储桶之间复制对象,使用 copy 方法并指定目标存储桶。
🌐 To copy an object across buckets, use the copy method and specify the destination bucket.
1import { createClient } from '@supabase/supabase-js'2const supabase = createClient('your_project_url', 'your_supabase_api_key')34// ---cut---5await supabase.storage.from('avatars').copy('public/avatar1.png', 'private/avatar2.png', {6 destinationBucket: 'avatars2',7})移动对象 #
🌐 Move objects
你可以在不同的桶之间或同一个桶内移动对象。目前通过 API 只能移动大小不超过 5GB 的对象。
🌐 You can move objects between buckets or within the same bucket. Currently only objects up to 5GB can be moved using the API.
当移动一个对象时,新的对象所有者将是发起移动操作的用户。一旦对象被移动,原来的对象将不再存在。
🌐 When moving an object, the owner of the new object will be the user who initiated the move operation. Once the object is moved, the original object will no longer exist.
在同一个桶中移动对象 #
🌐 Moving objects within the same bucket
要在同一个存储桶中移动一个对象,你可以使用 move 方法。
🌐 To move an object within the same bucket, you can use the move method.
1import { createClient } from '@supabase/supabase-js'2const supabase = createClient('your_project_url', 'your_supabase_api_key')34// ---cut---5const { data, error } = await supabase.storage6 .from('avatars')7 .move('public/avatar1.png', 'private/avatar2.png')在桶之间移动物品 #
🌐 Moving objects across buckets
要将一个对象在桶之间移动,使用 move 方法并指定目标桶。
🌐 To move an object across buckets, use the move method and specify the destination bucket.
1import { createClient } from '@supabase/supabase-js'2const supabase = createClient('your_project_url', 'your_supabase_api_key')34// ---cut---5await supabase.storage.from('avatars').move('public/avatar1.png', 'private/avatar2.png', {6 destinationBucket: 'avatars2',7})权限 #
🌐 Permissions
要复制对象,用户需要对源对象拥有 select 权限,并对目标对象拥有 insert 权限。如果用户还有 update 权限,复制可以作为 upsert 执行,这会在目标对象已存在时将其覆盖。
🌐 To copy objects, users need select permission on the source object and insert permission on the destination object. If the user also has update permission, the copy can be performed as an upsert, which will overwrite the destination object if it already exists.
要移动对象,用户需要对该对象拥有select和update权限。
🌐 To move objects, users need select and update permissions on the object.
例如:
🌐 For example:
1create policy "User can select their own objects (in any buckets)"2on storage.objects3for select4to authenticated5using (6 owner_id = (select auth.uid())7);89create policy "User can insert in their own folders (in any buckets)"10on storage.objects11for insert12to authenticated13with check (14 (storage.foldername(name))[1] = (select auth.uid())15);1617create policy "User can update their own objects (in any buckets)"18on storage.objects19for update20to authenticated21using (22 owner_id = (select auth.uid())23);