Copy files
You can copy an existing file to a different path within the storage bucket using the copy API.
The copy
method duplicates an existing file to a designated path and returns an object {path: 'destPath'}
upon successful completion.
import { copy } from 'aws-amplify/storage';
const copyFile = async () => { try { const response = await copy({ source: { path: 'album/2024/1.jpg', // Alternatively, path: ({identityId}) => `album/{identityId}/1.jpg` }, destination: { path: 'shared/2024/1.jpg', // Alternatively, path: ({identityId}) => `shared/{identityId}/1.jpg` }, }); } catch (error) { console.error('Error', err); }};
Specify a bucket or copy across buckets / regions
You can also perform an copy
operation to a specific bucket by providing the bucket
option. This option can either be a string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console.
import { copy } from 'aws-amplify/storage';
const copyFile = async () => { try { const response = await copy({ source: { path: 'album/2024/1.jpg', // Specify a target bucket using name assigned in Amplify Backend // or bucket name from console and associated region bucket: 'assignedNameInAmplifyBackend' }, destination: { path: 'shared/2024/1.jpg', // Specify a target bucket using name assigned in Amplify Backend // or bucket name from console and associated region bucket: { bucketName: 'generated-second-bucket-name', region: 'us-east-2' } } }); } catch (error) { console.error('Error', err); }};
Copy source
and destination
options
Option | Type | Default | Description |
---|---|---|---|
path | string | ({ identityId }) => string | Required | A string or callback that represents the path in source and destination bucket to copy the object to or from |
bucket | string | { bucketName: string; region: string; } | Default bucket and region from Amplify configuration | A string representing the target bucket's assigned name in Amplify Backend or an object specifying the bucket name and region from the console. Read more at Configure additional storage buckets. |