Skip to content

Commit

Permalink
feat(storage): sync multibucket to storagebrowser (#13635)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP authored Jul 24, 2024
2 parents ce9e88c + 4f9f9d8 commit d89cc27
Show file tree
Hide file tree
Showing 23 changed files with 1,282 additions and 93 deletions.
56 changes: 28 additions & 28 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,31 +293,31 @@
"name": "[Analytics] record (Pinpoint)",
"path": "./dist/esm/analytics/index.mjs",
"import": "{ record }",
"limit": "17.14 kB"
"limit": "17.23 kB"
},
{
"name": "[Analytics] record (Kinesis)",
"path": "./dist/esm/analytics/kinesis/index.mjs",
"import": "{ record }",
"limit": "48.56 kB"
"limit": "48.65 kB"
},
{
"name": "[Analytics] record (Kinesis Firehose)",
"path": "./dist/esm/analytics/kinesis-firehose/index.mjs",
"import": "{ record }",
"limit": "45.71 kB"
"limit": "45.81 kB"
},
{
"name": "[Analytics] record (Personalize)",
"path": "./dist/esm/analytics/personalize/index.mjs",
"import": "{ record }",
"limit": "49.53 kB"
"limit": "49.63 kB"
},
{
"name": "[Analytics] identifyUser (Pinpoint)",
"path": "./dist/esm/analytics/index.mjs",
"import": "{ identifyUser }",
"limit": "15.64 kB"
"limit": "15.73 kB"
},
{
"name": "[Analytics] enable",
Expand All @@ -335,7 +335,7 @@
"name": "[API] generateClient (AppSync)",
"path": "./dist/esm/api/index.mjs",
"import": "{ generateClient }",
"limit": "40.09 kB"
"limit": "40.19 kB"
},
{
"name": "[API] REST API handlers",
Expand All @@ -353,13 +353,13 @@
"name": "[Auth] resetPassword (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ resetPassword }",
"limit": "12.48 kB"
"limit": "12.58 kB"
},
{
"name": "[Auth] confirmResetPassword (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ confirmResetPassword }",
"limit": "12.42 kB"
"limit": "12.52 kB"
},
{
"name": "[Auth] signIn (Cognito)",
Expand All @@ -371,7 +371,7 @@
"name": "[Auth] resendSignUpCode (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ resendSignUpCode }",
"limit": "12.44 kB"
"limit": "12.53 kB"
},
{
"name": "[Auth] confirmSignUp (Cognito)",
Expand All @@ -383,31 +383,31 @@
"name": "[Auth] confirmSignIn (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ confirmSignIn }",
"limit": "28.32 kB"
"limit": "28.42 kB"
},
{
"name": "[Auth] updateMFAPreference (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ updateMFAPreference }",
"limit": "11.78 kB"
"limit": "11.87 kB"
},
{
"name": "[Auth] fetchMFAPreference (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ fetchMFAPreference }",
"limit": "11.81 kB"
"limit": "11.91 kB"
},
{
"name": "[Auth] verifyTOTPSetup (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ verifyTOTPSetup }",
"limit": "12.7 kB"
"limit": "12.75 kB"
},
{
"name": "[Auth] updatePassword (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ updatePassword }",
"limit": "12.67 kB"
"limit": "12.76 kB"
},
{
"name": "[Auth] setUpTOTP (Cognito)",
Expand All @@ -419,85 +419,85 @@
"name": "[Auth] updateUserAttributes (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ updateUserAttributes }",
"limit": "11.87 kB"
"limit": "11.99 kB"
},
{
"name": "[Auth] getCurrentUser (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ getCurrentUser }",
"limit": "7.75 kB"
"limit": "7.85 kB"
},
{
"name": "[Auth] confirmUserAttribute (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ confirmUserAttribute }",
"limit": "12.64 kB"
"limit": "12.75 kB"
},
{
"name": "[Auth] signInWithRedirect (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ signInWithRedirect }",
"limit": "21.10 kB"
"limit": "21.19 kB"
},
{
"name": "[Auth] fetchUserAttributes (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ fetchUserAttributes }",
"limit": "11.72 kB"
"limit": "11.82 kB"
},
{
"name": "[Auth] Basic Auth Flow (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ signIn, signOut, fetchAuthSession, confirmSignIn }",
"limit": "30.11 kB"
"limit": "30.20 kB"
},
{
"name": "[Auth] OAuth Auth Flow (Cognito)",
"path": "./dist/esm/auth/index.mjs",
"import": "{ signInWithRedirect, signOut, fetchAuthSession }",
"limit": "21.52 kB"
"limit": "21.62 kB"
},
{
"name": "[Storage] copy (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ copy }",
"limit": "14.9 kB"
"limit": "15.24 kB"
},
{
"name": "[Storage] downloadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ downloadData }",
"limit": "15.49 kB"
"limit": "15.76 kB"
},
{
"name": "[Storage] getProperties (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ getProperties }",
"limit": "14.77 kB"
"limit": "15.03 kB"
},
{
"name": "[Storage] getUrl (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ getUrl }",
"limit": "15.87 kB"
"limit": "16.09 kB"
},
{
"name": "[Storage] list (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ list }",
"limit": "15.39 kB"
"limit": "15.65 kB"
},
{
"name": "[Storage] remove (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ remove }",
"limit": "14.63 kB"
"limit": "14.88 kB"
},
{
"name": "[Storage] uploadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ uploadData }",
"limit": "19.94 kB"
"limit": "20.18 kB"
}
]
}
65 changes: 65 additions & 0 deletions packages/core/__tests__/parseAmplifyOutputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,71 @@ describe('parseAmplifyOutputs tests', () => {
},
});
});
it('should parse storage multi bucket', () => {
const amplifyOutputs: AmplifyOutputs = {
version: '1',
storage: {
aws_region: 'us-west-2',
bucket_name: 'storage-bucket-test',
buckets: [
{
name: 'default-bucket',
bucket_name: 'storage-bucket-test',
aws_region: 'us-west-2',
},
{
name: 'bucket-2',
bucket_name: 'storage-bucket-test-2',
aws_region: 'us-west-2',
},
],
},
};

const result = parseAmplifyOutputs(amplifyOutputs);

expect(result).toEqual({
Storage: {
S3: {
bucket: 'storage-bucket-test',
region: 'us-west-2',
buckets: {
'bucket-2': {
bucketName: 'storage-bucket-test-2',
region: 'us-west-2',
},
'default-bucket': {
bucketName: 'storage-bucket-test',
region: 'us-west-2',
},
},
},
},
});
});
it('should throw for storage multi bucket parsing with same friendly name', () => {
const amplifyOutputs: AmplifyOutputs = {
version: '1',
storage: {
aws_region: 'us-west-2',
bucket_name: 'storage-bucket-test',
buckets: [
{
name: 'default-bucket',
bucket_name: 'storage-bucket-test',
aws_region: 'us-west-2',
},
{
name: 'default-bucket',
bucket_name: 'storage-bucket-test-2',
aws_region: 'us-west-2',
},
],
},
};

expect(() => parseAmplifyOutputs(amplifyOutputs)).toThrow();
});
});

describe('analytics tests', () => {
Expand Down
28 changes: 26 additions & 2 deletions packages/core/src/parseAmplifyOutputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* This is because JSON schema contains keys with snake_case */
/* eslint-disable camelcase */

/* Does not like exahaustive checks */
/* Does not like exhaustive checks */
/* eslint-disable no-case-declarations */

import {
Expand All @@ -25,11 +25,13 @@ import {
AmplifyOutputsDataProperties,
AmplifyOutputsGeoProperties,
AmplifyOutputsNotificationsProperties,
AmplifyOutputsStorageBucketProperties,
AmplifyOutputsStorageProperties,
} from './singleton/AmplifyOutputs/types';
import {
AnalyticsConfig,
AuthConfig,
BucketInfo,
GeoConfig,
LegacyConfig,
ResourcesConfig,
Expand All @@ -56,12 +58,13 @@ function parseStorage(
return undefined;
}

const { bucket_name, aws_region } = amplifyOutputsStorageProperties;
const { bucket_name, aws_region, buckets } = amplifyOutputsStorageProperties;

return {
S3: {
bucket: bucket_name,
region: aws_region,
buckets: buckets && createBucketInfoMap(buckets),
},
};
}
Expand Down Expand Up @@ -333,3 +336,24 @@ function getMfaStatus(

return 'off';
}

function createBucketInfoMap(
buckets: AmplifyOutputsStorageBucketProperties[],
): Record<string, BucketInfo> {
const mappedBuckets: Record<string, BucketInfo> = {};

buckets.forEach(({ name, bucket_name: bucketName, aws_region: region }) => {
if (name in mappedBuckets) {
throw new Error(
`Duplicate friendly name found: ${name}. Name must be unique.`,
);
}

mappedBuckets[name] = {
bucketName,
region,
};
});

return mappedBuckets;
}
12 changes: 12 additions & 0 deletions packages/core/src/singleton/AmplifyOutputs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,21 @@ export interface AmplifyOutputsAuthProperties {
mfa_methods?: string[];
}

export interface AmplifyOutputsStorageBucketProperties {
/** Friendly bucket name provided in Amplify Outputs */
name: string;
/** Actual S3 bucket name given */
bucket_name: string;
/** Region for the bucket */
aws_region: string;
}
export interface AmplifyOutputsStorageProperties {
/** Default region for Storage */
aws_region: string;
/** Default bucket for Storage */
bucket_name: string;
/** List of buckets for Storage */
buckets?: AmplifyOutputsStorageBucketProperties[];
}

export interface AmplifyOutputsGeoProperties {
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/singleton/Storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { AtLeastOne } from '../types';
/** @deprecated This may be removed in the next major version. */
export type StorageAccessLevel = 'guest' | 'protected' | 'private';

/** Information on bucket used to store files/objects */
export interface BucketInfo {
/** Actual bucket name */
bucketName: string;
/** Region of the bucket */
region: string;
}
export interface S3ProviderConfig {
S3: {
bucket?: string;
Expand All @@ -16,6 +23,8 @@ export interface S3ProviderConfig {
* @internal
*/
dangerouslyConnectToHttpEndpointForTesting?: string;
/** Map of friendly name for bucket to its information */
buckets?: Record<string, BucketInfo>;
};
}

Expand Down
Loading

0 comments on commit d89cc27

Please sign in to comment.