-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(storage): add client standard remove api
- Loading branch information
Ashwin Kumar
committed
Sep 9, 2024
1 parent
001263d
commit f60b1fb
Showing
19 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
packages/storage/__tests__/client/apis/advanced/remove.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO(ashwinkumar6): add tests for client advanced API utils | ||
describe('remove advanced API', () => { | ||
describe('Happy Cases', () => { | ||
it.todo('Should call foundational remove API once with correct params'); | ||
}); | ||
describe('Error Cases', () => { | ||
it.todo('Should throw if underlying foundational API throws'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO(ashwinkumar6): add tests for client standard API utils | ||
describe('remove standard API', () => { | ||
describe('Happy Cases', () => { | ||
it.todo('Should call advanced remove API once with correct params'); | ||
}); | ||
describe('Error Cases', () => { | ||
it.todo('Should throw if underlying advanced API throws'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// public advanced APIs | ||
export { remove } from './remove'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO(ashwinkumar6): import from foundational API | ||
import { deleteObject } from '../../../providers/s3/utils/client/s3data'; | ||
|
||
import { GetPropertiesInput } from './types'; | ||
|
||
export const remove = async (input: GetPropertiesInput): Promise<void> => { | ||
const { bucket, credentialsProvider, path, options } = input; | ||
const { useAccelerateEndpoint, customEndpoint, customUserAgent } = | ||
options ?? {}; | ||
|
||
// TODO(ashwinkumar6): move service call to foundation | ||
// TODO(ashwinkumar6): 'customHeaders' input isn't wired up | ||
await deleteObject( | ||
{ | ||
credentials: (await credentialsProvider()).credentials, | ||
region: bucket.region, | ||
userAgentValue: customUserAgent, | ||
useAccelerateEndpoint, | ||
customEndpoint, | ||
}, | ||
{ | ||
Bucket: bucket.bucketName, | ||
Key: path, | ||
}, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { GetPropertiesInput } from './inputs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { BucketInfo, CredentialsProvider } from './options'; | ||
|
||
// TODO(aswhinkumar6): finalize on API signature | ||
// Question(aswhinkumar6): should we inherit from standard API interface ? | ||
export interface GetPropertiesInput { | ||
// Question(ashwinkumar6): why not keep bucket and region flat ? | ||
bucket: BucketInfo; | ||
/** | ||
* S3 object key. | ||
*/ | ||
path: string; | ||
/** | ||
* Async function resolved to AWS credentials to | ||
* authorize AWS requests. | ||
*/ | ||
credentialsProvider: CredentialsProvider; | ||
|
||
options?: { | ||
/** | ||
* Whether to use accelerate endpoint. | ||
* @default false | ||
*/ | ||
useAccelerateEndpoint?: boolean; | ||
/** | ||
* [OUT OF SCOPE FOR THIS REVIEW]Additional string to be appended to the user | ||
* agent header. | ||
* | ||
* @internal | ||
*/ | ||
customUserAgent?: string; | ||
/** | ||
* [OUT OF SCOPE FOR THIS REVIEW]Custom S3 base host name. The bucket name may be | ||
* added to subdomain. | ||
* | ||
*/ | ||
customEndpoint?: string; | ||
/** | ||
* [OUT OF SCOPE FOR THIS REVIEW]After the request has been construct but before | ||
* it's authorized, apply changes to the request | ||
* headers. | ||
*/ | ||
customHeaders?(input: { | ||
/** | ||
* Existing headers value of given request. | ||
*/ | ||
existingHeaders: Record<Lowercase<string>, string>; | ||
/** | ||
* S3 operation name. e.g. 'headObject' | ||
*/ | ||
apiName: string; | ||
}): Promise<Record<Lowercase<string>, string>>; | ||
}; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/storage/src/client/apis/advanced/types/options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { AWSCredentials } from '@aws-amplify/core/internals/utils'; | ||
|
||
export interface BucketInfo { | ||
bucketName: string; | ||
region: string; | ||
} | ||
|
||
export type CredentialsProvider = () => Promise<{ | ||
credentials: AWSCredentials; | ||
}>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// public standard APIs | ||
export { remove } from './remove'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Amplify, fetchAuthSession } from '@aws-amplify/core'; | ||
import { StorageAction } from '@aws-amplify/core/internals/utils'; | ||
|
||
import { getStorageUserAgentValue, logger } from '../../utils'; | ||
|
||
import { | ||
RemoveInput, | ||
RemoveOutput, | ||
RemoveWithPathInput, | ||
RemoveWithPathOutput, | ||
} from './types'; | ||
import { | ||
resolveS3ConfigAndInput, | ||
validateStorageOperationInput, | ||
} from './utils'; | ||
import { STORAGE_INPUT_KEY } from './utils/constants'; | ||
import { remove as removeAdvanced } from './advanced/remove'; | ||
|
||
export const remove = async ( | ||
input: RemoveInput | RemoveWithPathInput, | ||
): Promise<RemoveOutput | RemoveWithPathOutput> => { | ||
const { s3Config, keyPrefix, bucket, identityId } = | ||
await resolveS3ConfigAndInput(Amplify, input); | ||
|
||
const { inputType, objectKey } = validateStorageOperationInput( | ||
input, | ||
identityId, | ||
); | ||
|
||
let finalKey; | ||
if (inputType === STORAGE_INPUT_KEY) { | ||
finalKey = `${keyPrefix}${objectKey}`; | ||
logger.debug(`remove "${objectKey}" from "${finalKey}".`); | ||
} else { | ||
finalKey = objectKey; | ||
logger.debug(`removing object in path "${finalKey}"`); | ||
} | ||
|
||
await removeAdvanced({ | ||
path: finalKey, | ||
bucket: { | ||
bucketName: bucket, | ||
region: s3Config.region, | ||
}, | ||
credentialsProvider: async () => { | ||
return { | ||
// QUESTION(ashwinkumar6): do we throw exception if no credentials | ||
// TODO(ashwinkumar6): move to util | ||
credentials: (await fetchAuthSession()).credentials!, | ||
}; | ||
}, | ||
options: { | ||
useAccelerateEndpoint: s3Config?.useAccelerateEndpoint, | ||
customUserAgent: getStorageUserAgentValue(StorageAction.Remove), | ||
}, | ||
}); | ||
|
||
return inputType === STORAGE_INPUT_KEY | ||
? { | ||
key: objectKey, | ||
} | ||
: { | ||
path: objectKey, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { RemoveInput, RemoveWithPathInput } from './inputs'; | ||
|
||
export { RemoveOutput, RemoveWithPathOutput } from './outputs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO(ashwinkumar6): move type here | ||
export { RemoveInput, RemoveWithPathInput } from '../../../providers/s3/types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO(ashwinkumar6): move type here | ||
export { | ||
RemoveOutput, | ||
RemoveWithPathOutput, | ||
} from '../../../providers/s3/types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO move standard API constants to this file | ||
export { STORAGE_INPUT_KEY } from '../../../providers/s3/utils/constants'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Standard API utils | ||
export { validateStorageOperationInput } from './validateStorageOperationInput'; | ||
export { resolveS3ConfigAndInput } from './resolveS3ConfigAndInput'; |
6 changes: 6 additions & 0 deletions
6
packages/storage/src/client/apis/utils/resolveS3ConfigAndInput.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO(ashwinkumar6): move assertion to this file | ||
// TODO(ashwinkumar6): refactor and cleanup this util | ||
export { resolveS3ConfigAndInput } from '../../../providers/s3/utils'; |
5 changes: 5 additions & 0 deletions
5
packages/storage/src/client/apis/utils/validateStorageOperationInput.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO(ashwinkumar6): move assertion to this file | ||
export { validateStorageOperationInput } from '../../../providers/s3/utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO(ashwinkumar6) move to this file | ||
export { getStorageUserAgentValue } from '../providers/s3/utils/userAgent'; |