Skip to content

Commit 6055a80

Browse files
authored
feat(core): add storage bucket type (#5478)
1 parent 51d01c4 commit 6055a80

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

packages/amplify_core/lib/src/types/exception/amplify_exception.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ part 'network_exception.dart';
2121
part 'push/push_notification_exception.dart';
2222
part 'storage/access_denied_exception.dart';
2323
part 'storage/http_status_exception.dart';
24+
part 'storage/invalid_storage_bucket_exception.dart';
2425
part 'storage/local_file_not_found_exception.dart';
2526
part 'storage/not_found_exception.dart';
2627
part 'storage/operation_canceled_exception.dart';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
part of '../amplify_exception.dart';
2+
3+
/// {@template amplify_core.storage.invalid_storage_bucket_exception}
4+
/// Exception thrown when the [StorageBucket] is invalid.
5+
/// {@endtemplate}
6+
class InvalidStorageBucketException extends StorageException {
7+
const InvalidStorageBucketException(
8+
super.message, {
9+
super.recoverySuggestion,
10+
super.underlyingException,
11+
});
12+
13+
@override
14+
String get runtimeTypeName => 'InvalidStorageBucketException';
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// {@template amplify_core.storage.bucket_info}
2+
/// Presents a storage bucket information.
3+
/// {@endtemplate}
4+
class BucketInfo {
5+
/// {@macro amplify_core.storage.bucket_info}
6+
const BucketInfo({required this.bucketName, required this.region});
7+
final String bucketName;
8+
final String region;
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:amplify_core/src/config/amplify_outputs/storage/storage_outputs.dart';
2+
import 'package:amplify_core/src/types/storage/bucket_info.dart';
3+
import 'package:amplify_core/src/types/storage/storage_bucket_from_outputs.dart';
4+
import 'package:meta/meta.dart';
5+
6+
/// Presents a storage bucket.
7+
class StorageBucket {
8+
/// Creates a [StorageBucket] from [BucketInfo].
9+
const StorageBucket.fromBucketInfo(this._info);
10+
11+
/// Creates a [StorageBucket] defined by the [name] in AmplifyOutputs file.
12+
factory StorageBucket.fromOutputs(String name) =>
13+
StorageBucketFromOutputs(name);
14+
15+
final BucketInfo _info;
16+
17+
@internal
18+
BucketInfo resolveBucketInfo(StorageOutputs? storageOutputs) => _info;
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:amplify_core/amplify_core.dart';
2+
import 'package:amplify_core/src/config/amplify_outputs/storage/storage_outputs.dart';
3+
import 'package:meta/meta.dart';
4+
5+
/// {@template amplify_core.storage.storage_bucket_from_outputs}
6+
/// Creates a [StorageBucket] defined by the name in AmplifyOutputs file.
7+
/// {@endtemplate}
8+
@internal
9+
class StorageBucketFromOutputs implements StorageBucket {
10+
/// {@macro amplify_core.storage.storage_bucket_from_outputs}
11+
const StorageBucketFromOutputs(this._name);
12+
13+
final String _name;
14+
15+
@override
16+
BucketInfo resolveBucketInfo(StorageOutputs? storageOutputs) {
17+
assert(
18+
storageOutputs != null,
19+
const InvalidStorageBucketException(
20+
'Amplify Storage is not configured.',
21+
recoverySuggestion:
22+
'Make sure storage exists in the Amplify Outputs file.',
23+
),
24+
);
25+
// TODO(nikahsn): fix after adding buckets to StorageOutputs.
26+
return BucketInfo(
27+
bucketName: _name,
28+
region: storageOutputs!.awsRegion,
29+
);
30+
}
31+
}

packages/amplify_core/lib/src/types/storage/storage_types.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export '../exception/amplify_exception.dart'
1111
StorageOperationCanceledException,
1212
NetworkException,
1313
UnknownException;
14+
export 'bucket_info.dart';
1415
export 'copy_operation.dart';
1516
export 'copy_options.dart';
1617
export 'copy_request.dart';
@@ -44,6 +45,7 @@ export 'remove_operation.dart';
4445
export 'remove_options.dart';
4546
export 'remove_request.dart';
4647
export 'remove_result.dart';
48+
export 'storage_bucket.dart';
4749
export 'storage_item.dart';
4850
export 'storage_path.dart';
4951
export 'transfer_progress.dart';

0 commit comments

Comments
 (0)