Skip to content

Updated

Copy Objects

Learn how to copy and move objects

Copy objects

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. For how Storage assigns owners, see Storage ownership.

Copying objects within the same bucket

To copy an object within the same bucket, use the copy method. Replace YOUR_USER_ID with the signed-in user's ID so the destination's first folder matches their JWT sub claim.

await ..('avatars').('public/avatar1.png', 'YOUR_USER_ID/avatar2.png')

Copying objects across buckets

To copy an object across buckets, use the copy method and specify the destination bucket.

await ..('avatars').('public/avatar1.png', 'YOUR_USER_ID/avatar2.png', {
: 'avatars2',
})

Move objects

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

To move an object within the same bucket, you can use the move method.

const { , } = await .
.('avatars')
.('public/avatar1.png', 'private/avatar2.png')

Moving objects across buckets

To move an object across buckets, use the move method and specify the destination bucket.

await ..('avatars').('public/avatar1.png', 'private/avatar2.png', {
: 'avatars2',
})

Copy or move a version

In a bucket with object versioning turned on, pass sourceVersionId to copy or move to use a specific version of the source object. The destination gets a new version ID.

await ..('avatars').('public/avatar1.png', 'private/avatar1-backup.png', {
: 'a1f2e3d4-5b6c-4d7e-8f9a-0b1c2d3e4f5a',
})

Moving a version copies it and then permanently deletes the source version. To restore a version to its own path, see Restore a version.

Permissions

To copy objects, users need select permission on the source object and insert permission on the destination object. To copy over an existing object or delete marker, users also need update permission. With object versioning turned on, the replaced object is kept as a noncurrent version.

To move objects, users need select and update permissions on the object. If either bucket has versioning turned on, a move copies the object and then deletes the source version, so users need the copy permissions and delete permission on the source object.

For example:

create policy "User can select their own objects (in any buckets)"
on storage.objects
for select
to authenticated
using (
owner_id = (select auth.jwt()->>'sub')
);
create policy "User can insert in their own folders (in any buckets)"
on storage.objects
for insert
to authenticated
with check (
(storage.foldername(name))[1] = (select auth.jwt()->>'sub')
);
create policy "User can update their own objects (in any buckets)"
on storage.objects
for update
to authenticated
using (
owner_id = (select auth.jwt()->>'sub')
);