Skip to content

Commit

Permalink
feat(core): add storage bucket type
Browse files Browse the repository at this point in the history
  • Loading branch information
Nika Hassani committed Sep 17, 2024
1 parent 51d01c4 commit 3ef6f7e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ part 'network_exception.dart';
part 'push/push_notification_exception.dart';
part 'storage/access_denied_exception.dart';
part 'storage/http_status_exception.dart';
part 'storage/invalid_storage_bucket_exception.dart';
part 'storage/local_file_not_found_exception.dart';
part 'storage/not_found_exception.dart';
part 'storage/operation_canceled_exception.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
part of '../amplify_exception.dart';

/// {@template amplify_core.storage.invalid_storage_bucket_exception}
/// Exception thrown when the [StorageBucket] is invalid.
/// {@endtemplate}
class InvalidStorageBucketException extends StorageException {
const InvalidStorageBucketException(
super.message, {
super.recoverySuggestion,
super.underlyingException,
});

@override
String get runtimeTypeName => 'InvalidStorageBucketException';
}
9 changes: 9 additions & 0 deletions packages/amplify_core/lib/src/types/storage/bucket_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// {@template amplify_core.storage.bucket_info}
/// Presents a storage bucket information.
/// {@endtemplate}
class BucketInfo {
/// {@macro amplify_core.storage.bucket_info}
const BucketInfo({required this.bucketName, required this.region});
final String bucketName;
final String region;
}
19 changes: 19 additions & 0 deletions packages/amplify_core/lib/src/types/storage/storage_bucket.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:amplify_core/src/config/amplify_outputs/storage/storage_outputs.dart';
import 'package:amplify_core/src/types/storage/bucket_info.dart';
import 'package:amplify_core/src/types/storage/storage_bucket_from_outputs.dart';
import 'package:meta/meta.dart';

/// Presents a storage bucket.
class StorageBucket {
/// Creates a [StorageBucket] from [BucketInfo].
const StorageBucket.fromBucketInfo(this._info);

/// Creates a [StorageBucket] defined by the [name] in AmplifyOutputs file.
factory StorageBucket.fromOutputs(String name) =>
StorageBucketFromOutputs(name);

final BucketInfo _info;

@internal
BucketInfo resolveBucketInfo(StorageOutputs? storageOutputs) => _info;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_core/src/config/amplify_outputs/storage/storage_outputs.dart';
import 'package:meta/meta.dart';

/// {@template amplify_core.storage.storage_bucket_from_outputs}
/// Creates a [StorageBucket] defined by the name in AmplifyOutputs file.
/// {@endtemplate}
@internal
class StorageBucketFromOutputs implements StorageBucket {
/// {@macro amplify_core.storage.storage_bucket_from_outputs}
const StorageBucketFromOutputs(this._name);

final String _name;

@override
BucketInfo resolveBucketInfo(StorageOutputs? storageOutputs) {
assert(
storageOutputs != null,
const InvalidStorageBucketException(
'Amplify Storage is not configured.',
recoverySuggestion:
'Make sure storage exists in the Amplify Outputs file.',
),
);
// TODO(nikahsn): fix after adding buckets to StorageOutputs.
return BucketInfo(
bucketName: _name,
region: storageOutputs!.awsRegion,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export '../exception/amplify_exception.dart'
StorageOperationCanceledException,
NetworkException,
UnknownException;
export 'bucket_info.dart';
export 'copy_operation.dart';
export 'copy_options.dart';
export 'copy_request.dart';
Expand Down Expand Up @@ -44,6 +45,7 @@ export 'remove_operation.dart';
export 'remove_options.dart';
export 'remove_request.dart';
export 'remove_result.dart';
export 'storage_bucket.dart';
export 'storage_item.dart';
export 'storage_path.dart';
export 'transfer_progress.dart';
Expand Down

0 comments on commit 3ef6f7e

Please sign in to comment.