Skip to content

Commit

Permalink
feat(storage): multi bucket upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
Equartey committed Oct 24, 2024
1 parent 81a4c7a commit 7e373fd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
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.upload_file_options}
/// Configurable options for `Amplify.Storage.uploadFile`.
Expand All @@ -15,6 +15,7 @@ class StorageUploadFileOptions
const StorageUploadFileOptions({
this.metadata = const {},
this.pluginOptions,
this.bucket,
});

/// The metadata attached to the object to be uploaded.
Expand All @@ -23,8 +24,11 @@ class StorageUploadFileOptions
/// {@macro amplify_core.storage.upload_file_plugin_options}
final StorageUploadFilePluginOptions? pluginOptions;

/// Optionally specify which bucket to target
final StorageBucket? bucket;

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

@override
String get runtimeTypeName => 'StorageUploadFileOptions';
Expand All @@ -33,6 +37,7 @@ class StorageUploadFileOptions
Map<String, Object?> toJson() => {
'metadata': metadata,
'pluginOptions': pluginOptions?.toJson(),
'bucket': bucket,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:integration_test/integration_test.dart';

import 'utils/configure.dart';
import 'utils/create_file/create_file.dart';
import 'utils/object_exists.dart';
import 'utils/sign_in_new_user.dart';
import 'utils/tear_down.dart';

Expand Down Expand Up @@ -220,6 +221,76 @@ void main() {
});
});

group('multi-bucket', () {
final mainBucket =
StorageBucket.fromOutputs('Storage Integ Test main bucket');
final secondaryBucket = StorageBucket.fromOutputs(
'Storage Integ Test secondary bucket',
);

testWidgets('uploads to multiple buckets', (_) async {
final fileId = uuid();
final path =
StoragePath.fromString('public/multi-bucket-upload-file-$fileId');
const content = 'upload file';
final data = content.codeUnits;
final filePath = await createFile(path: fileId, content: content);
addTearDownMultiBucket(
path,
[mainBucket, secondaryBucket],
);
// main bucket
final mainResult = await Amplify.Storage.uploadFile(
localFile: AWSFile.fromPath(filePath),
path: path,
options: StorageUploadFileOptions(
pluginOptions: const S3UploadFilePluginOptions(
useAccelerateEndpoint: true,
),
bucket: mainBucket,
),
).result;
expect(mainResult.uploadedItem.path, path);

final downloadMainResult = await Amplify.Storage.downloadData(
path: path,
options: StorageDownloadDataOptions(
bucket: mainBucket,
),
).result;
expect(downloadMainResult.bytes, data);

// secondary bucket
final secondaryResult = await Amplify.Storage.uploadFile(
localFile: AWSFile.fromPath(filePath),
path: path,
options: StorageUploadFileOptions(
pluginOptions: const S3UploadFilePluginOptions(
useAccelerateEndpoint: true,
),
bucket: secondaryBucket,
),
).result;
expect(secondaryResult.uploadedItem.path, path);

final downloadSecondaryResult = await Amplify.Storage.downloadData(
path: path,
options: StorageDownloadDataOptions(
bucket: secondaryBucket,
),
).result;
expect(downloadSecondaryResult.bytes, data);

expect(
await objectExistsInBuckets(
path,
[mainBucket, secondaryBucket],
),
true,
);
});
});

group('upload progress', () {
testWidgets('reports progress', (_) async {
final fileId = uuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ class StorageS3Service {
FutureOr<void> Function()? onDone,
FutureOr<void> Function()? onError,
}) {
final s3ClientInfo = getS3ClientInfo(storageBucket: options.bucket);
final s3PluginOptions =
options.pluginOptions as S3UploadFilePluginOptions? ??
const S3UploadFilePluginOptions();
Expand All @@ -373,9 +374,9 @@ class StorageS3Service {
);
final uploadDataTask = S3UploadTask.fromAWSFile(
localFile,
s3Client: _defaultS3Client,
s3ClientConfig: _defaultS3ClientConfig,
bucket: _storageOutputs.bucketName,
s3Client: s3ClientInfo.client,
s3ClientConfig: s3ClientInfo.config,
bucket: s3ClientInfo.bucketName,
awsRegion: _storageOutputs.awsRegion,
path: path,
options: uploadDataOptions,
Expand Down

0 comments on commit 7e373fd

Please sign in to comment.