Skip to content

Commit

Permalink
feat(storage): multi bucket copy
Browse files Browse the repository at this point in the history
  • Loading branch information
tyllark committed Nov 14, 2024
1 parent c7fbd67 commit a4d16d7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
24 changes: 24 additions & 0 deletions packages/amplify_core/lib/src/types/storage/copy_buckets.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:amplify_core/amplify_core.dart';

/// Presents storage buckets for a copy operation.
class CopyBuckets with AWSSerializable<Map<String, Object?>> {
/// Creates a [CopyBuckets] with [source] and [destination] buckets.
const CopyBuckets({
required this.source,
required this.destination,
});

/// Creates a [CopyBuckets] with the same [bucket] for the [source] and [destination].
CopyBuckets.sameBucket(StorageBucket bucket)
: source = bucket,
destination = bucket;

final StorageBucket source;
final StorageBucket destination;

@override
Map<String, Object?> toJson() => {
'source': source.toJson(),
'destination': destination.toJson(),
};
}
11 changes: 9 additions & 2 deletions packages/amplify_core/lib/src/types/storage/copy_options.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:aws_common/aws_common.dart';
import 'package:amplify_core/amplify_core.dart';

/// {@template amplify_core.storage.copy_options}
/// Configurable options for `Amplify.Storage.copy`.
Expand All @@ -12,11 +12,17 @@ class StorageCopyOptions
AWSSerializable<Map<String, Object?>>,
AWSDebuggable {
/// {@macro amplify_core.storage.copy_options}
const StorageCopyOptions({this.pluginOptions});
const StorageCopyOptions({
this.pluginOptions,
this.buckets,
});

/// plugin specific options for `Amplify.Storage.copy`.
final StorageCopyPluginOptions? pluginOptions;

/// Optionally specify which bucket to target
final CopyBuckets? buckets;

@override
List<Object?> get props => [pluginOptions];

Expand All @@ -26,6 +32,7 @@ class StorageCopyOptions
@override
Map<String, Object?> toJson() => {
'pluginOptions': pluginOptions?.toJson(),
'buckets': buckets?.toJson(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export '../exception/amplify_exception.dart'
NetworkException,
UnknownException;
export 'bucket_info.dart';
export 'copy_buckets.dart';
export 'copy_operation.dart';
export 'copy_options.dart';
export 'copy_request.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ class AmplifyStorageS3Dart extends StoragePluginInterface

final s3Options = StorageCopyOptions(
pluginOptions: s3PluginOptions,
buckets: options?.buckets,
);

return S3CopyOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,17 @@ class StorageS3Service {
}) async {
final s3PluginOptions = options.pluginOptions as S3CopyPluginOptions? ??
const S3CopyPluginOptions();
final s3ClientInfoSource = getS3ClientInfo(storageBucket: options.buckets?.source);
final s3ClientInfoDestination = getS3ClientInfo(storageBucket: options.buckets?.destination);

final [sourcePath, destinationPath] = await _pathResolver.resolvePaths(
paths: [source, destination],
);

final copyRequest = s3.CopyObjectRequest.build((builder) {
builder
..bucket = _storageOutputs.bucketName
..copySource = '${_storageOutputs.bucketName}/$sourcePath'
..bucket = s3ClientInfoDestination.bucketName
..copySource = '${s3ClientInfoSource.bucketName}/$sourcePath'
..key = destinationPath
..metadataDirective = s3.MetadataDirective.copy;
});
Expand Down

0 comments on commit a4d16d7

Please sign in to comment.