Skip to content

feat/multi bucket list api #5561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions infra-gen2/backends/storage/main/amplify/backend.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
import { defineBackend } from "@aws-amplify/backend";
import * as s3 from "aws-cdk-lib/aws-s3";
import { auth } from "./auth/resource";
import { storage } from "./storage/resource";
import { firstBucket, secondBucket } from "./storage/resource";

/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/
const backend = defineBackend({
auth,
storage,
firstBucket,
secondBucket,
});

// custom storage configurations
const s3Bucket = backend.storage.resources.bucket;
const s3Bucket = backend.firstBucket.resources.bucket;
const cfnBucket = s3Bucket.node.defaultChild as s3.CfnBucket;
const s3SecondaryBucket = backend.secondBucket.resources.bucket;
const cfnSecondaryBucket = s3SecondaryBucket.node.defaultChild as s3.CfnBucket;

cfnBucket.accelerateConfiguration = {
accelerationStatus: "Enabled",
};

cfnSecondaryBucket.accelerateConfiguration = {
accelerationStatus: "Enabled",
};

// required to add the metadata header, which amplify-backend does not support
backend.firstBucket.resources.cfnResources.cfnBucket.corsConfiguration = {
corsRules: [
{
allowedHeaders: ["*"],
allowedMethods: ["GET", "HEAD", "PUT", "POST", "DELETE"],
allowedOrigins: ["*"],
exposedHeaders: [
"x-amz-server-side-encryption",
"x-amz-request-id",
"x-amz-id-2",
"ETag",
"x-amz-meta-description",
],
maxAge: 3000,
},
],
};

// required to add the metadata header, which amplify-backend does not support
backend.storage.resources.cfnResources.cfnBucket.corsConfiguration = {
backend.secondBucket.resources.cfnResources.cfnBucket.corsConfiguration = {
corsRules: [
{
allowedHeaders: ["*"],
Expand Down
22 changes: 20 additions & 2 deletions infra-gen2/backends/storage/main/amplify/storage/resource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { defineStorage } from "@aws-amplify/backend";

export const storage = defineStorage({
name: "Storage Integ Test main",
export const firstBucket = defineStorage({
name: "Storage Integ Test main bucket",
isDefault: true,
access: (allow) => ({
"public/*": [
allow.guest.to(["read", "write", "delete"]),
allow.authenticated.to(["read", "delete", "write"]),
],
"protected/{entity_id}/*": [
allow.authenticated.to(["read"]),
allow.entity("identity").to(["read", "write", "delete"]),
],
"private/{entity_id}/*": [
allow.entity("identity").to(["read", "write", "delete"]),
],
}),
});

export const secondBucket = defineStorage({
name: "Storage Integ Test secondary bucket",
access: (allow) => ({
"public/*": [
allow.guest.to(["read", "write", "delete"]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class StorageCategory extends AmplifyCategory<StoragePluginInterface> {
required StoragePath path,
void Function(StorageTransferProgress)? onProgress,
StorageUploadDataOptions? options,
StorageBucket? bucket,
}) {
return identifyCall(
StorageCategoryMethod.uploadData,
Expand All @@ -152,6 +153,7 @@ class StorageCategory extends AmplifyCategory<StoragePluginInterface> {
data: data,
onProgress: onProgress,
options: options,
bucket: bucket,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';

part 'bucket_outputs.g.dart';

/// {@template amplify_core.amplify_outputs.bucket_outputs}
/// The Amplify Gen 2 outputs for Buckets in the Storage category.
/// {@endtemplate}
@zAmplifyOutputsSerializable
class BucketOutputs
with AWSEquatable<BucketOutputs>, AWSSerializable, AWSDebuggable {
/// {@macro amplify_core.amplify_outputs.bucket_outputs}
const BucketOutputs({
required this.name,
required this.bucketName,
required this.awsRegion,
});

factory BucketOutputs.fromJson(Map<String, Object?> json) =>
_$BucketOutputsFromJson(json);

/// The user friendly name of the bucket
final String name;

/// The Amazon S3 bucket name.
final String bucketName;

/// The AWS region of Amazon S3 resources.
final String awsRegion;

@override
List<Object?> get props => [
name,
bucketName,
awsRegion,
];

@override
String get runtimeTypeName => 'BucketOutputs';

@override
Object? toJson() {
return _$BucketOutputsToJson(this);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_core/src/config/amplify_outputs/storage/bucket_outputs.dart';

part 'storage_outputs.g.dart';

Expand All @@ -12,7 +13,11 @@ part 'storage_outputs.g.dart';
class StorageOutputs
with AWSEquatable<StorageOutputs>, AWSSerializable, AWSDebuggable {
/// {@macro amplify_core.amplify_outputs.storage_outputs}
const StorageOutputs({required this.awsRegion, required this.bucketName});
const StorageOutputs({
required this.awsRegion,
required this.bucketName,
this.buckets,
});

factory StorageOutputs.fromJson(Map<String, Object?> json) =>
_$StorageOutputsFromJson(json);
Expand All @@ -23,8 +28,11 @@ class StorageOutputs
/// The Amazon S3 bucket name.
final String bucketName;

/// The list of buckets if there are multiple buckets for the project
final List<BucketOutputs>? buckets;

@override
List<Object?> get props => [awsRegion, bucketName];
List<Object?> get props => [awsRegion, bucketName, buckets];

@override
String get runtimeTypeName => 'StorageOutputs';
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ abstract class StoragePluginInterface extends AmplifyPluginInterface {
required StorageDataPayload data,
void Function(StorageTransferProgress)? onProgress,
StorageUploadDataOptions? options,
StorageBucket? bucket,
}) {
throw UnimplementedError('uploadData() has not been implemented.');
}
Expand Down
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';
}
17 changes: 17 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,17 @@
import 'package:amplify_core/amplify_core.dart';

/// {@template amplify_core.storage.bucket_info}
/// Presents a storage bucket information.
/// {@endtemplate}
class BucketInfo with AWSEquatable<BucketInfo> {
/// {@macro amplify_core.storage.bucket_info}
const BucketInfo({required this.bucketName, required this.region});
final String bucketName;
final String region;

@override
List<Object?> get props => [
bucketName,
region,
];
}
12 changes: 10 additions & 2 deletions packages/amplify_core/lib/src/types/storage/list_options.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:aws_common/aws_common.dart';
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_core/amplify_core.dart';

/// {@template amplify_core.storage.list_options}
/// Configurable options for `Amplify.Storage.list`.
Expand All @@ -15,6 +18,7 @@ class StorageListOptions
const StorageListOptions({
this.pageSize = 1000,
this.nextToken,
this.bucket,
this.pluginOptions,
});

Expand All @@ -26,9 +30,12 @@ class StorageListOptions

/// {@macro amplify_core.storage.list_plugin_options}
final StorageListPluginOptions? pluginOptions;

/// an optional bucket to specify which bucket to return the list for
final StorageBucket? bucket;

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

@override
String get runtimeTypeName => 'StorageListOptions';
Expand All @@ -37,6 +44,7 @@ class StorageListOptions
Map<String, Object?> toJson() => {
'pageSize': pageSize,
'nextToken': nextToken,
'bucket': bucket,
'pluginOptions': pluginOptions?.toJson(),
};
}
Expand Down
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;
}
Loading
Loading