Skip to content
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

feat(storage): multi bucket get properties api #5577

Merged
merged 23 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
13d6723
updating storage_outputs class for multi-bucket support
Sep 17, 2024
c5c0438
seperated bucket out into its own class instead of using a map
Sep 17, 2024
f22dd48
made bucket_output class a proper output class like the others in amp…
Sep 18, 2024
cc6c55b
Update auth.dart
ekjotmultani Sep 18, 2024
b6f8719
added doc comments and changed name of bucket class
Sep 18, 2024
66ad860
Merge branch 'storagewereoutput-update' of https://github.com/aws-amp…
Sep 18, 2024
3fc0ce9
Delete devtools_options.yaml
ekjotmultani Sep 18, 2024
9114206
added trailing commas to pass ci test
Sep 18, 2024
a17b602
Merge branch 'storage-output-update' of https://github.com/aws-amplif…
Sep 18, 2024
e7906c7
ran dart format on two failing files
Sep 18, 2024
6055a80
feat(core): add storage bucket type (#5478)
NikaHsn Sep 20, 2024
24089ae
Merge pull request #5476 from aws-amplify/storage-output-update
ekjotmultani Sep 23, 2024
d01d0df
updated resource.ts and backend.ts for multiple buckets in our infra-…
Sep 24, 2024
47c1371
updated resource.ts and backend.ts for multiple buckets in our infra-…
Sep 24, 2024
0d4e492
Merge pull request #5492 from aws-amplify/multi-bucket-infra-update
ekjotmultani Sep 24, 2024
d68bb03
feat(storage): update s3 storage service to support multiple s3 clien…
NikaHsn Sep 25, 2024
3eafd51
feat(storage): update uploadData API to accept optional storage bucke…
NikaHsn Oct 14, 2024
1becb6f
added bucket option to getProperties api
ekjotmultani Oct 18, 2024
3d717dd
added integration tests
ekjotmultani Oct 19, 2024
3a13b3d
removed the use of specific s3 bucket names and regions, moved testin…
ekjotmultani Oct 21, 2024
58ead62
added unauthiorized path test
ekjotmultani Oct 21, 2024
7de725c
Update packages/amplify_core/lib/src/types/storage/get_properties_opt…
ekjotmultani Oct 21, 2024
3bea1dc
Merge branch 'multi-bucket' into feat/multi-bucket-get-properties-api
ekjotmultani Oct 23, 2024
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,
];
}
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
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