Skip to content

Commit

Permalink
Merge branch 'multi-bucket' into feat/multi-bucket-list-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Equartey authored Oct 25, 2024
2 parents 4b3f68d + 81a4c7a commit 885cc98
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 4 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.get_properties_options}
/// Configurable options for `Amplify.Storage.getProperties`.
Expand All @@ -14,20 +14,25 @@ class StorageGetPropertiesOptions
/// {@macro amplify_core.storage.get_properties_options}
const StorageGetPropertiesOptions({
this.pluginOptions,
this.bucket,
});

/// {@macro amplify_core.storage.download_get_properties_plugin_options}
final StorageGetPropertiesPluginOptions? pluginOptions;

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

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

@override
String get runtimeTypeName => 'StorageGetPropertiesOptions';

@override
Map<String, Object?> toJson() => {
'pluginOptions': pluginOptions?.toJson(),
'bucket': bucket,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,119 @@ void main() {
expect(result.storageItem.size, data.length);
});
});
group('multibucket config', () {
final mainBucket =
StorageBucket.fromOutputs('Storage Integ Test main bucket');
final secondaryBucket =
StorageBucket.fromOutputs('Storage Integ Test secondary bucket');
setUpAll(() async {
await configure(amplifyEnvironments['main']!);
addTearDownPath(StoragePath.fromString(path));
await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(data),
path: StoragePath.fromString(path),
options: const StorageUploadDataOptions(metadata: metadata),
bucket: mainBucket,
).result;
await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(data),
path: StoragePath.fromString(path),
options: const StorageUploadDataOptions(metadata: metadata),
bucket: secondaryBucket,
).result;
});

testWidgets('String StoragePath', (_) async {
final result = await Amplify.Storage.getProperties(
path: StoragePath.fromString(path),
options: StorageGetPropertiesOptions(
bucket: mainBucket,
),
).result;
expect(result.storageItem.path, path);
expect(result.storageItem.metadata, metadata);
expect(result.storageItem.eTag, isNotNull);
expect(result.storageItem.size, data.length);

final resultSecondaryBucket = await Amplify.Storage.getProperties(
path: StoragePath.fromString(path),
options: StorageGetPropertiesOptions(
bucket: secondaryBucket,
),
).result;
expect(resultSecondaryBucket.storageItem.path, path);
expect(resultSecondaryBucket.storageItem.metadata, metadata);
expect(resultSecondaryBucket.storageItem.eTag, isNotNull);
expect(resultSecondaryBucket.storageItem.size, data.length);
});

testWidgets('with identity ID', (_) async {
final userIdentityId = await signInNewUser();
final name = 'get-properties-with-identity-id-${uuid()}';
final data = 'with identity ID'.codeUnits;
final expectedResolvedPath = 'private/$userIdentityId/$name';
addTearDownPath(StoragePath.fromString(expectedResolvedPath));
await Amplify.Storage.uploadData(
data: StorageDataPayload.bytes(data),
path: StoragePath.fromString(expectedResolvedPath),
options: const StorageUploadDataOptions(metadata: metadata),
bucket: secondaryBucket,
).result;
final result = await Amplify.Storage.getProperties(
path: StoragePath.fromIdentityId(
((identityId) => 'private/$identityId/$name'),
),
options: StorageGetPropertiesOptions(
bucket: secondaryBucket,
),
).result;
expect(result.storageItem.path, expectedResolvedPath);
expect(result.storageItem.metadata, metadata);
expect(result.storageItem.eTag, isNotNull);
expect(result.storageItem.size, data.length);
});

testWidgets('not existent path', (_) async {
// we expect StorageNotFoundException here since there is no data uploaded to either bucket on this path
await expectLater(
() => Amplify.Storage.getProperties(
path: const StoragePath.fromString('public/not-existent-path'),
options: StorageGetPropertiesOptions(
bucket: mainBucket,
),
).result,
throwsA(isA<StorageNotFoundException>()),
);
await expectLater(
() => Amplify.Storage.getProperties(
path: const StoragePath.fromString('public/not-existent-path'),
options: StorageGetPropertiesOptions(
bucket: secondaryBucket,
),
).result,
throwsA(isA<StorageNotFoundException>()),
);
});
testWidgets('unauthorized path', (_) async {
await expectLater(
() => Amplify.Storage.getProperties(
path: const StoragePath.fromString('unauthorized/path'),
options: StorageGetPropertiesOptions(
bucket: mainBucket,
),
).result,
throwsA(isA<StorageAccessDeniedException>()),
);
await expectLater(
() => Amplify.Storage.getProperties(
path: const StoragePath.fromString('unauthorized/path'),
options: StorageGetPropertiesOptions(
bucket: secondaryBucket,
),
).result,
throwsA(isA<StorageAccessDeniedException>()),
);
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class AmplifyStorageS3Dart extends StoragePluginInterface

final s3Options = StorageGetPropertiesOptions(
pluginOptions: s3PluginOptions,
bucket: options?.bucket,
);

return S3GetPropertiesOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ class StorageS3Service {
required StorageGetPropertiesOptions options,
}) async {
final resolvedPath = await _pathResolver.resolvePath(path: path);

final s3ClientInfo = getS3ClientInfo(storageBucket: options.bucket);
return S3GetPropertiesResult(
storageItem: S3Item.fromHeadObjectOutput(
await headObject(
s3client: _defaultS3Client,
bucket: _storageOutputs.bucketName,
bucket: s3ClientInfo.bucketName,
key: resolvedPath,
),
path: resolvedPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ void main() {
() async {
const testOptions = StorageGetPropertiesOptions(
pluginOptions: S3GetPropertiesPluginOptions(),
bucket: StorageBucket.fromBucketInfo(
BucketInfo(bucketName: 'unit-test-bucket', region: 'us-east-2'),
),
);

when(
Expand Down

0 comments on commit 885cc98

Please sign in to comment.