Copy files
copy
method copies an existing file to a different key and returns a {key: 'destKey'}
object upon success.
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 user10 },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 level4const 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' level15console.log(16 (await list({ prefix: 'copied/', options: { accessLevel: 'private' } })).items17); // [ { ..., 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 current4// authenticated user5
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' } })).items21); // [..., 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}