Copy files
You can copy an existing file to a different path within the storage bucket using the copy API.
User who initiates a copy operation should have read permission on the copy source file.
Future<void> copy() async { try { final result = await Amplify.Storage.copy( source: const StoragePath.fromString('album/2024/1.jpg'), destination: const StoragePath.fromString('shared/2024/1.jpg'), ).result; safePrint('Copied file: ${result.copiedItem.path}'); } on StorageException catch (e) { safePrint(e); }}
Specify a bucket or copy across buckets / regions
You can also perform a copy
operation to a specific bucket by providing the CopyBuckets
option.
This option is an object that takes two StorageBucket
parameters, which can be constructed by the specified name in the Amplify Backend, or the bucket name and region from the console.
final mainBucket = StorageBucket.fromOutputs( 'mainBucket',);final bucket2 = StorageBucket.fromBucketInfo( BucketInfo( bucketName: 'second-bucket-name-from-console', region: 'us-east-2', ),),try { final result = await Amplify.Storage.copy( source: const StoragePath.fromString('album/2024/1.jpg'), destination: const StoragePath.fromString('shared/2024/1.jpg'), options: StorageCopyOptions( buckets: CopyBuckets( source: bucket1, destination: bucket2, ), ), ).result; safePrint('Copied file: ${result.copiedItem.path}');} on StorageException catch (e) { print('Error: $e');}
copy
options
Option | Type | Description |
---|---|---|
getProperties | boolean | Whether to retrieve properties for the copied object using theAmplify.Storage.getProperties() after the operation completes. When set to true the returned item will contain additional info such as metadata and content type. |
buckets | CopyBuckets | An object that accepts two StorageBucket parameters. To copy to and from the same bucket, use the CopyBuckets.sameBucket(targetBucket) method, where targetBucket is a StorageBucket . Read more at Configure additional storage buckets |
Example of copy
with options:
final result = Amplify.Storage.copy( source: const StoragePath.fromString('album/2024/1.jpg'), destination: const StoragePath.fromString('shared/2024/1.jpg'), options: const StorageCopyOptions( pluginOptions: S3CopyPluginOptions( getProperties: true, ), buckets: CopyBuckets.sameBucket( StorageBucket.fromOutputs('secondBucket'), ), ),);