Page updated Jan 16, 2024

Copy files

copy method copies an existing file to a different key and returns a {key: 'destKey'} object upon success.

copy can copy an object up to 5 GB in a single operation.

To copy a file, you need to provide a source key in source and a destination key in destination. accessLevel can be 'guest', 'private', or 'protected', defaulting to 'guest'.

1import { copy } from 'aws-amplify/storage';
2
3const copyFile = async () => {
4 try {
5 const response = await copy({
6 source: {
7 key: 'srcKey',
8 accessLevel: 'private' // optional 'guest', 'private', or 'protected'. Defaults to 'guest'.
9 targetIdentityId: 'targetIdentityId' // optional, set it to other user's identity ID if copy from other user
10 },
11 destination: {
12 key: 'destKey',
13 accessLevel: 'private' // optional 'guest', 'private', or 'protected'. Defaults to 'guest'.
14 },
15 });
16 } catch (error) {
17 console.error('Error', err);
18 }
19};

Copy files within the same access levels

You can copy a file from the specified key to another key within the same File Access Level (Defaults to 'guest').

1import { copy, list } from 'aws-amplify/storage';
2
3// Copies 'existing/srcKey' to 'copied/destKey' within the 'guest' access level
4const copied = await copy({
5 source: { key: 'existing/srcKey' },
6 destination: { key: 'copied/destKey' }
7});
8
9// There should now be a new file with key "copied/destKey"
10console.log((await list({ prefix: 'copied/' })).items); // [ { ..., key: 'copied/destKey' } ]
11console.log(copied); // { key: 'copied/destKey' }

Copy files across access levels

To copy a file from or to an access level other than default 'guest', you'll need to explicitly provide the access level: 'private' or 'protected'.

1import { copy, list } from 'aws-amplify/storage';
2
3// Copies 'existing/srcKey' to 'copied/destKey' from 'guest' to 'private'
4const copied = await copy({
5 source: {
6 key: 'existing/srcKey'
7 },
8 destination: {
9 key: 'copied/destKey',
10 accessLevel: 'private'
11 }
12});
13
14// There should now be a new file with key "copied/destKey" in the 'private' level
15console.log(
16 (await list({ prefix: 'copied/', options: { accessLevel: 'private' } })).items
17); // [ { ..., key: 'copied/destKey' } ]
18console.log(copied); // { key: 'copied/destKey' }

Copy protected files from another user

You can also copy a protected file from another user by providing their identity id

1import { copy, list } from 'aws-amplify/storage';
2
3// Copies 'existing/srcKey' to 'copied/destKey' from 'protected' of another identity ID to 'private' of the current
4// authenticated user
5
6const copied = await copy({
7 source: {
8 key: 'existing/srcKey',
9 accessLevel: 'protected',
10 targetIdentityId: 'identityId'
11 },
12 destination: {
13 key: 'copied/destKey',
14 accessLevel: 'private'
15 }
16});
17
18// There should now be a new file with key "copied/destKey"
19console.log(
20 (await list({ prefix: 'copied/', options: { accessLevel: 'private' } })).items
21); // [..., key: 'copied/destKey']
22console.log(copied); // { key: 'copied/destKey' }

The format of the response will look similar to the below example

1{
2 key: 'copied/destKey';
3}

Cross identity ID copying is only allowed if the source object's access level is set to 'protected'.