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): add new internal remove api #13880

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
59 changes: 59 additions & 0 deletions packages/storage/__tests__/internals/apis/remove.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { AmplifyClassV6 } from '@aws-amplify/core';

import { remove as advancedRemove } from '../../../src/internals';
jimblanc marked this conversation as resolved.
Show resolved Hide resolved
import { remove as removeInternal } from '../../../src/providers/s3/apis/internal/remove';

jest.mock('../../../src/providers/s3/apis/internal/remove');
const mockedRemoveInternal = jest.mocked(removeInternal);

describe('remove (internal)', () => {
beforeEach(() => {
mockedRemoveInternal.mockResolvedValue({
path: 'output/path/to/mock/object',
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('should pass advanced option locationCredentialsProvider to internal remove', async () => {
const useAccelerateEndpoint = true;
const bucket = { bucketName: 'bucket', region: 'us-east-1' };
const locationCredentialsProvider = async () => ({
credentials: {
accessKeyId: 'akid',
secretAccessKey: 'secret',
sessionToken: 'token',
expiration: new Date(),
},
});

const result = await advancedRemove({
path: 'input/path/to/mock/object',
options: {
useAccelerateEndpoint,
bucket,
locationCredentialsProvider,
},
});

expect(mockedRemoveInternal).toHaveBeenCalledTimes(1);
expect(mockedRemoveInternal).toHaveBeenCalledWith(
expect.any(AmplifyClassV6),
{
path: 'input/path/to/mock/object',
options: {
useAccelerateEndpoint,
bucket,
locationCredentialsProvider,
},
},
);
expect(result).toEqual({
path: 'output/path/to/mock/object',
});
});
});
30 changes: 30 additions & 0 deletions packages/storage/src/internals/apis/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { Amplify } from '@aws-amplify/core';

import { remove as removeInternal } from '../../providers/s3/apis/internal/remove';
import { RemoveInput } from '../types/inputs';
import { RemoveOutput } from '../types/outputs';

/**
* Remove a file from your S3 bucket.
* @param input - The `RemoveWithPathInput` object.
* @return Output containing the removed object path.
* @throws service: `S3Exception` - S3 service errors thrown while while removing the object.
* @throws validation: `StorageValidationErrorCode` - Validation errors thrown
* when there is no path or path is empty or path has a leading slash.
*
* @internal
*/
export const remove = (input: RemoveInput): Promise<RemoveOutput> =>
removeInternal(Amplify, {
path: input.path,
options: {
useAccelerateEndpoint: input?.options?.useAccelerateEndpoint,
bucket: input?.options?.bucket,
locationCredentialsProvider: input?.options?.locationCredentialsProvider,
},
// Type casting is necessary because `removeInternal` supports both Gen1 and Gen2 signatures, but here
// given in input can only be Gen2 signature, the return can only ben Gen2 signature.
}) as Promise<RemoveOutput>;
3 changes: 3 additions & 0 deletions packages/storage/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ export {
ListCallerAccessGrantsInput,
GetPropertiesInput,
CopyInput,
RemoveInput,
} from './types/inputs';
export {
GetDataAccessOutput,
ListCallerAccessGrantsOutput,
GetPropertiesOutput,
RemoveOutput,
} from './types/outputs';

export { getDataAccess } from './apis/getDataAccess';
export { listCallerAccessGrants } from './apis/listCallerAccessGrants';
export { getProperties } from './apis/getProperties';
export { remove } from './apis/remove';

/*
CredentialsStore exports
Expand Down
11 changes: 11 additions & 0 deletions packages/storage/src/internals/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {
CopyWithPathInput,
GetPropertiesWithPathInput,
RemoveWithPathInput,
} from '../../providers/s3';

import { CredentialsProvider, ListLocationsInput } from './credentials';
Expand Down Expand Up @@ -37,6 +38,16 @@ export interface GetDataAccessInput {
scope: string;
}

/**
* @internal
*/
export type RemoveInput = ExtendInputWithAdvancedOptions<
RemoveWithPathInput,
{
locationCredentialsProvider?: CredentialsProvider;
ashwinkumar6 marked this conversation as resolved.
Show resolved Hide resolved
}
>;

/**
* @internal
*/
Expand Down
10 changes: 9 additions & 1 deletion packages/storage/src/internals/types/outputs.ts
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 { GetPropertiesWithPathOutput } from '../../providers/s3/types';
import {
GetPropertiesWithPathOutput,
RemoveWithPathOutput,
} from '../../providers/s3/types';

import { ListLocationsOutput, LocationCredentials } from './credentials';

Expand All @@ -19,3 +22,8 @@ export type GetDataAccessOutput = LocationCredentials;
* @internal
*/
export type GetPropertiesOutput = GetPropertiesWithPathOutput;

/**
* @internal
*/
export type RemoveOutput = RemoveWithPathOutput;
11 changes: 4 additions & 7 deletions packages/storage/src/providers/s3/apis/internal/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
import { AmplifyClassV6 } from '@aws-amplify/core';
import { StorageAction } from '@aws-amplify/core/internals/utils';

import {
RemoveInput,
RemoveOutput,
RemoveWithPathInput,
RemoveWithPathOutput,
} from '../../types';
import { RemoveInput, RemoveOutput, RemoveWithPathOutput } from '../../types';
import {
resolveS3ConfigAndInput,
validateStorageOperationInput,
Expand All @@ -18,10 +13,12 @@ import { deleteObject } from '../../utils/client/s3data';
import { getStorageUserAgentValue } from '../../utils/userAgent';
import { logger } from '../../../../utils';
import { STORAGE_INPUT_KEY } from '../../utils/constants';
// TODO: Remove this interface when we move to public advanced APIs.
import { RemoveInput as RemoveWithPathInputWithAdvancedOptions } from '../../../../internals';

export const remove = async (
amplify: AmplifyClassV6,
input: RemoveInput | RemoveWithPathInput,
input: RemoveInput | RemoveWithPathInputWithAdvancedOptions,
): Promise<RemoveOutput | RemoveWithPathOutput> => {
const { s3Config, keyPrefix, bucket, identityId } =
await resolveS3ConfigAndInput(amplify, input);
Expand Down
Loading