Skip to content

Commit

Permalink
feat(storage): add remaining copy changes for internals (#13889)
Browse files Browse the repository at this point in the history
* chore: add remaining copy changes for internals

* chore; explicitly call internal API with params

* chore: copy return type

* chore: feedback changes
  • Loading branch information
Samaritan1011001 authored Oct 9, 2024
1 parent 02cb08a commit 0bca04a
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 15 deletions.
53 changes: 53 additions & 0 deletions packages/storage/__tests__/internals/apis/copy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { AmplifyClassV6 } from '@aws-amplify/core';

import { copy as advancedCopy } from '../../../src/internals';
import { copy as copyInternal } from '../../../src/providers/s3/apis/internal/copy';

jest.mock('../../../src/providers/s3/apis/internal/copy');
const mockedCopyInternal = jest.mocked(copyInternal);

describe('copy (internals)', () => {
beforeEach(() => {
jest.clearAllMocks();
mockedCopyInternal.mockResolvedValue({
path: 'output/path/to/mock/object',
});
});

it('should pass advanced option locationCredentialsProvider to internal list', async () => {
const locationCredentialsProvider = async () => ({
credentials: {
accessKeyId: 'akid',
secretAccessKey: 'secret',
sessionToken: 'token',
expiration: new Date(),
},
});
const copyInputWithAdvancedOptions = {
source: {
path: 'path/to/object',
bucket: 'bucket',
eTag: 'eTag',
notModifiedSince: new Date(),
},
destination: {
path: 'path/to/object',
bucket: 'bucket',
},
options: {
locationCredentialsProvider,
},
};
const result = await advancedCopy(copyInputWithAdvancedOptions);
expect(mockedCopyInternal).toHaveBeenCalledTimes(1);
expect(mockedCopyInternal).toHaveBeenCalledWith(
expect.any(AmplifyClassV6),
copyInputWithAdvancedOptions,
);
expect(result).toEqual({
path: 'output/path/to/mock/object',
});
});
});
3 changes: 1 addition & 2 deletions packages/storage/__tests__/internals/apis/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AmplifyClassV6 } from '@aws-amplify/core';

import { list as advancedList } from '../../../src/internals';
import { list as listInternal } from '../../../src/providers/s3/apis/internal/list';
import { ListAllWithPathOutput } from '../../../src';

jest.mock('../../../src/providers/s3/apis/internal/list');
const mockedListInternal = jest.mocked(listInternal);
Expand All @@ -14,7 +13,7 @@ describe('list (internals)', () => {
jest.clearAllMocks();
mockedListInternal.mockResolvedValue({
items: [],
} as ListAllWithPathOutput);
});
});

it('should pass advanced option locationCredentialsProvider to internal list', async () => {
Expand Down
31 changes: 31 additions & 0 deletions packages/storage/src/internals/apis/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

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

import { copy as copyInternal } from '../../providers/s3/apis/internal/copy';
import { CopyInput } from '../types/inputs';
import { CopyOutput } from '../types/outputs';

/**
* @internal
*/
export const copy = (input: CopyInput) =>
copyInternal(Amplify, {
source: {
path: input.source.path,
bucket: input.source.bucket,
eTag: input.source.eTag,
notModifiedSince: input.source.notModifiedSince,
},
destination: {
path: input.destination.path,
bucket: input.destination.bucket,
},
options: {
// Advanced options
locationCredentialsProvider: input.options?.locationCredentialsProvider,
},
// Type casting is necessary because `copyInternal` 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<CopyOutput>;
29 changes: 21 additions & 8 deletions packages/storage/src/internals/apis/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@
import { Amplify } from '@aws-amplify/core';

import { list as listInternal } from '../../providers/s3/apis/internal/list';
import { ListInputWithPath } from '../types/inputs';
import { ListInput } from '../types/inputs';
import { ListPaginateWithPathInput } from '../../providers/s3';
import { ListOutput } from '../types/outputs';

/**
* @internal
* List all or paginate files from S3 for a given `path`.
* @param input - The `ListWithPathInputAndAdvancedOptions` object.
* @returns A list of all objects with path and metadata
* @throws service: `S3Exception` - S3 service errors thrown when checking for existence of bucket
* @throws validation: `StorageValidationErrorCode` - thrown when there are issues with credentials
*/
export function list(input?: ListInputWithPath) {
return listInternal(Amplify, input ?? {});
export function list(input: ListInput): Promise<ListOutput> {
return listInternal(Amplify, {
path: input.path,
options: {
bucket: input.options?.bucket,
subpathStrategy: input.options?.subpathStrategy,
useAccelerateEndpoint: input.options?.useAccelerateEndpoint,
listAll: input.options?.listAll,

// Pagination options
nextToken: (input as ListPaginateWithPathInput).options?.nextToken,
pageSize: (input as ListPaginateWithPathInput).options?.pageSize,
// Advanced options
locationCredentialsProvider: input.options?.locationCredentialsProvider,
},
// Type casting is necessary because `listInternal` supports both Gen1 and Gen2 signatures, but here
// given in input can only be Gen2 signature, the return can only ben Gen2 signature.
} as ListInput) as Promise<ListOutput>;
}
5 changes: 4 additions & 1 deletion packages/storage/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export {
GetPropertiesInput,
GetUrlInput,
CopyInput,
ListInputWithPath,
ListInput,
RemoveInput,
DownloadDataInput,
} from './types/inputs';
Expand All @@ -25,6 +25,8 @@ export {
GetUrlOutput,
RemoveOutput,
DownloadDataOutput,
ListOutput,
CopyOutput,
} from './types/outputs';

export { getDataAccess } from './apis/getDataAccess';
Expand All @@ -34,6 +36,7 @@ export { getProperties } from './apis/getProperties';
export { getUrl } from './apis/getUrl';
export { remove } from './apis/remove';
export { downloadData } from './apis/downloadData';
export { copy } from './apis/copy';

/*
CredentialsStore exports
Expand Down
2 changes: 1 addition & 1 deletion packages/storage/src/internals/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface GetDataAccessInput {
/**
* @internal
*/
export type ListInputWithPath = ExtendInputWithAdvancedOptions<
export type ListInput = ExtendInputWithAdvancedOptions<
ListAllWithPathInput | ListPaginateWithPathInput,
{
locationCredentialsProvider?: CredentialsProvider;
Expand Down
17 changes: 15 additions & 2 deletions packages/storage/src/internals/types/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// SPDX-License-Identifier: Apache-2.0

import {
CopyWithPathOutput,
DownloadDataWithPathOutput,
GetPropertiesWithPathOutput,
GetUrlWithPathOutput,
ListAllWithPathOutput,
ListPaginateWithPathOutput,
RemoveWithPathOutput,
} from '../../providers/s3/types';

Expand All @@ -13,7 +16,12 @@ import { ListLocationsOutput, LocationCredentials } from './credentials';
/**
* @internal
*/
export type ListCallerAccessGrantsOutput = ListLocationsOutput;
export type CopyOutput = CopyWithPathOutput;

/**
* @internal
*/
export type DownloadDataOutput = DownloadDataWithPathOutput;

/**
* @internal
Expand All @@ -38,4 +46,9 @@ export type RemoveOutput = RemoveWithPathOutput;
/**
* @internal
*/
export type DownloadDataOutput = DownloadDataWithPathOutput;
export type ListOutput = ListAllWithPathOutput | ListPaginateWithPathOutput;

/**
* @internal
*/
export type ListCallerAccessGrantsOutput = ListLocationsOutput;
2 changes: 1 addition & 1 deletion packages/storage/src/providers/s3/apis/internal/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { CommonPrefix } from '../../utils/client/s3data/types';
import { IntegrityError } from '../../../../errors/IntegrityError';
import { ListAllInput, ListPaginateInput } from '../../types/inputs';
// TODO: Remove this interface when we move to public advanced APIs.
import { ListInputWithPath as ListWithPathInputAndAdvancedOptions } from '../../../../internals/types/inputs';
import { ListInput as ListWithPathInputAndAdvancedOptions } from '../../../../internals/types/inputs';

const MAX_PAGE_SIZE = 1000;

Expand Down

0 comments on commit 0bca04a

Please sign in to comment.