Page updated Apr 29, 2024

Copy files

The copy API duplicates an existing file to a designated path and returns an object {path: 'destPath'} upon successful completion.

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

To copy a file, you need to provide the existing path in source and the destination path in destination.

1import { copy } from 'aws-amplify/storage';
2
3const copyFile = async () => {
4 try {
5 const response = await copy({
6 source: {
7 path: 'public/album/2024/1.jpg',
8 // Alternatively, path: ({identityId}) => `protected/${identityId}/album/1.jpg`
9 },
10 destination: {
11 path: 'public/shared/2024/1.jpg',
12 // Alternatively, path: ({identityId}) => `protected/${identityId}/shared/1.jpg`
13 },
14 });
15 } catch (error) {
16 console.error('Error', err);
17 }
18};
1import { copy } from 'aws-amplify/storage';
2
3const copyFile = async () => {
4 try {
5 const response = await copy({
6 source: {
7 key: 'srcKey',
8 accessLevel: 'protected' // 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: 'protected' // 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 path to another path within the same File Access Level.

1import { copy, list } from 'aws-amplify/storage';
2
3// Copies 'existing/srcPath' to 'copied/destPath' within 'public/'
4const copied = await copy({
5 source: { path: 'public/existing/srcPath' },
6 destination: { path: 'public/copied/destPath' }
7});
8
9// There should now be a new file 'copied/destPath' in 'public/'
10console.log((await list({ path: 'public/copied/' })).items); // [ { ..., path: 'public/copied/destPath' } ]
11console.log(copied); // { path: 'public/copied/destPath' }
1import { copy, list } from 'aws-amplify/storage';
2
3// Copies 'existing/srcKey' to 'copied/destKey' within 'public/'
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 to or from an access level, you'll need to explicitly provide the access level in path parameter.

1import { copy, list } from 'aws-amplify/storage';
2
3// Copies 'existing/srcPath' to 'copied/destPath' from 'guest' to 'private'
4const copied = await copy({
5 source: {
6 path: 'public/existing/srcPath'
7 },
8 destination: {
9 path: ({identityId}) => `private/${identityId}/copied/destPath`
10 }
11});
12
13// There should now be a new file 'copied/destPath' in 'private/'
14console.log(
15 (await list({ path: 'private/copied/'})).items
16); // [ { ..., path: 'private/XXXXX/copied/destPath' } ]
17console.log(copied); // { path: 'private/XXXXX/copied/destPath' }
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/srcPath' to 'copied/destPath' from 'protected' of another user to 'protected' of the current authenticated user
4
5const copied = await copy({
6 source: {
7 path: `protected/${targetUserId}/existing/srcPath`,
8 },
9 destination: {
10 path: ({identityId}) => `protected/${identityId}/copied/destPath`,
11 }
12});
13
14// There should now be a new file 'copied/destPath'
15console.log(
16 (await list({ path: ({identityId}) => `protected/${identityId}/copied/`})).items
17); // [..., path: 'protected/XXXXX/copied/destPath']
18console.log(copied); // { path: 'protected/XXXXX/copied/destPath' }
1import { copy, list } from 'aws-amplify/storage';
2
3// Copies 'existing/srcKey' to 'copied/destKey' from 'protected' of another user to 'private' of the current authenticated user
4
5const copied = await copy({
6 source: {
7 key: 'existing/srcKey',
8 accessLevel: 'protected',
9 targetIdentityId: 'identityId'
10 },
11 destination: {
12 key: 'copied/destKey',
13 accessLevel: 'private'
14 }
15});
16
17// There should now be a new file with key 'copied/destKey'
18console.log(
19 (await list({ prefix: 'copied/', options: { accessLevel: 'private' } })).items
20); // [..., key: 'copied/destKey']
21console.log(copied); // { key: 'copied/destKey' }

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