Skip to content

Commit

Permalink
refactor(storage): add client standard remove api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwin Kumar committed Sep 9, 2024
1 parent 001263d commit f60b1fb
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/storage/__tests__/client/apis/advanced/remove.test.ts
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');
});
});
12 changes: 12 additions & 0 deletions packages/storage/__tests__/client/apis/remove.test.ts
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');
});
});
5 changes: 5 additions & 0 deletions packages/storage/src/client/apis/advanced/index.ts
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';
29 changes: 29 additions & 0 deletions packages/storage/src/client/apis/advanced/remove.ts
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,
},
);
};
4 changes: 4 additions & 0 deletions packages/storage/src/client/apis/advanced/types/index.ts
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';
56 changes: 56 additions & 0 deletions packages/storage/src/client/apis/advanced/types/inputs.ts
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 packages/storage/src/client/apis/advanced/types/options.ts
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;
}>;
2 changes: 2 additions & 0 deletions packages/storage/src/client/apis/advanced/types/outputs.ts
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
5 changes: 5 additions & 0 deletions packages/storage/src/client/apis/index.ts
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';
68 changes: 68 additions & 0 deletions packages/storage/src/client/apis/remove.ts
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,
};
};
6 changes: 6 additions & 0 deletions packages/storage/src/client/apis/types/index.ts
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';
5 changes: 5 additions & 0 deletions packages/storage/src/client/apis/types/inputs.ts
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';
8 changes: 8 additions & 0 deletions packages/storage/src/client/apis/types/outputs.ts
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';
5 changes: 5 additions & 0 deletions packages/storage/src/client/apis/utils/constants.ts
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';
6 changes: 6 additions & 0 deletions packages/storage/src/client/apis/utils/index.ts
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';
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';
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';
1 change: 1 addition & 0 deletions packages/storage/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

export { resolvePrefix } from './resolvePrefix';
export { logger } from './logger';
export { getStorageUserAgentValue } from './userAgent';
5 changes: 5 additions & 0 deletions packages/storage/src/utils/userAgent.ts
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';

0 comments on commit f60b1fb

Please sign in to comment.