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 delimiter support #13480

Merged
merged 9 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@
"name": "[Storage] list (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ list }",
"limit": "14.94 kB"
"limit": "15.04 kB"
},
{
"name": "[Storage] remove (S3)",
Expand Down
106 changes: 106 additions & 0 deletions packages/storage/__tests__/providers/s3/apis/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,110 @@ describe('list API', () => {
}
});
});

describe('with delimiter', () => {
const mockedContents = [
{
Key: 'photos/',
...listObjectClientBaseResultItem,
},
{
Key: 'photos/2023.png',
...listObjectClientBaseResultItem,
},
{
Key: 'photos/2024.png',
...listObjectClientBaseResultItem,
},
];
const mockedCommonPrefixes = [
{ Prefix: 'photos/2023/' },
{ Prefix: 'photos/2024/' },
{ Prefix: 'photos/2025/' },
];

const mockedPath = 'photos/';

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

it('should return subpaths when maxiumDepth is passed in the request', async () => {
israx marked this conversation as resolved.
Show resolved Hide resolved
mockListObject.mockResolvedValueOnce({
Contents: mockedContents,
CommonPrefixes: mockedCommonPrefixes,
});
const { items, subpaths } = await list({
path: mockedPath,
options: {
delimiter: '/',
},
});
expect(items).toHaveLength(3);
expect(subpaths).toHaveLength(3);
expect(listObjectsV2).toHaveBeenCalledTimes(1);
await expect(listObjectsV2).toBeLastCalledWithConfigAndInput(
listObjectClientConfig,
{
Bucket: bucket,
MaxKeys: 1000,
Prefix: mockedPath,
Delimiter: '/',
},
);
});

it('should return subpaths when maxiumDepth and listAll are passed in the request', async () => {
mockListObject.mockResolvedValueOnce({
Contents: mockedContents,
CommonPrefixes: mockedCommonPrefixes,
});
const { items, subpaths } = await list({
path: mockedPath,
options: {
delimiter: '/',
listAll: true,
},
});
expect(items).toHaveLength(3);
expect(subpaths).toHaveLength(3);
israx marked this conversation as resolved.
Show resolved Hide resolved
expect(listObjectsV2).toHaveBeenCalledTimes(1);
await expect(listObjectsV2).toBeLastCalledWithConfigAndInput(
listObjectClientConfig,
{
Bucket: bucket,
MaxKeys: 1000,
Prefix: mockedPath,
Delimiter: '/',
},
);
});

it('should return subpaths when maxiumDepth is pageSize are passed in the request', async () => {
mockListObject.mockResolvedValueOnce({
Contents: mockedContents,
CommonPrefixes: mockedCommonPrefixes,
});
israx marked this conversation as resolved.
Show resolved Hide resolved
const { items, subpaths } = await list({
path: mockedPath,
options: {
delimiter: '/',
pageSize: 3,
},
});
expect(items).toHaveLength(3);
expect(subpaths).toHaveLength(3);
expect(listObjectsV2).toHaveBeenCalledTimes(1);
await expect(listObjectsV2).toBeLastCalledWithConfigAndInput(
listObjectClientConfig,
{
Bucket: bucket,
MaxKeys: 3,
Prefix: mockedPath,
Delimiter: '/',
},
);
});
});
});
59 changes: 45 additions & 14 deletions packages/storage/src/providers/s3/apis/internal/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
import { getStorageUserAgentValue } from '../../utils/userAgent';
import { logger } from '../../../../utils';
import { STORAGE_INPUT_PREFIX } from '../../utils/constants';
import { Subpath } from '../../../../types';
import { CommonPrefix } from '../../utils/client/types';

const MAX_PAGE_SIZE = 1000;

Expand Down Expand Up @@ -79,6 +81,7 @@ export const list = async (
Prefix: isInputWithPrefix ? `${generatedPrefix}${objectKey}` : objectKey,
MaxKeys: options?.listAll ? undefined : options?.pageSize,
ContinuationToken: options?.listAll ? undefined : options?.nextToken,
Delimiter: options?.delimiter,
};
logger.debug(`listing items from "${listParams.Prefix}"`);

Expand Down Expand Up @@ -176,23 +179,29 @@ const _listAllWithPath = async ({
listParams,
}: ListInputArgs): Promise<ListAllWithPathOutput> => {
const listResult: ListOutputItemWithPath[] = [];
const subpaths: Subpath[] = [];
let continuationToken = listParams.ContinuationToken;
do {
const { items: pageResults, nextToken: pageNextToken } =
await _listWithPath({
s3Config,
listParams: {
...listParams,
ContinuationToken: continuationToken,
MaxKeys: MAX_PAGE_SIZE,
},
});
const {
items: pageResults,
subpaths: pageSubpaths,
nextToken: pageNextToken,
} = await _listWithPath({
s3Config,
listParams: {
...listParams,
ContinuationToken: continuationToken,
MaxKeys: MAX_PAGE_SIZE,
},
});
listResult.push(...pageResults);
subpaths.push(...(pageSubpaths ?? []));
continuationToken = pageNextToken;
} while (continuationToken);

return {
items: listResult,
...getOptionWithSubpaths(subpaths),
};
};

Expand All @@ -206,27 +215,49 @@ const _listWithPath = async ({
listParamsClone.MaxKeys = MAX_PAGE_SIZE;
}

const response: ListObjectsV2Output = await listObjectsV2(
const {
Contents: contents,
NextContinuationToken: nextContinuationToken,
CommonPrefixes: commonPrefixes,
}: ListObjectsV2Output = (await listObjectsV2(
{
...s3Config,
userAgentValue: getStorageUserAgentValue(StorageAction.List),
},
listParamsClone,
);
)) ?? {};
israx marked this conversation as resolved.
Show resolved Hide resolved

if (!response?.Contents) {
const subpaths = mapCommonPrefixesToSubpaths(commonPrefixes);

if (!contents) {
return {
items: [],
...getOptionWithSubpaths(subpaths),
};
}

return {
items: response.Contents.map(item => ({
items: contents.map(item => ({
path: item.Key!,
eTag: item.ETag,
lastModified: item.LastModified,
size: item.Size,
})),
nextToken: response.NextContinuationToken,
nextToken: nextContinuationToken,
...getOptionWithSubpaths(subpaths),
};
};

function mapCommonPrefixesToSubpaths(
commonPrefixes?: CommonPrefix[],
): Subpath[] | undefined {
const mappedSubpaths = commonPrefixes?.map(({ Prefix }) => ({
path: Prefix,
}));

return mappedSubpaths as Subpath[] | undefined;
}

function getOptionWithSubpaths(subpaths?: Subpath[]) {
israx marked this conversation as resolved.
Show resolved Hide resolved
return subpaths && subpaths.length > 0 ? { subpaths } : {};
}
8 changes: 8 additions & 0 deletions packages/storage/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ export type DownloadTask<Result> = Omit<
>;

export type UploadTask<Result> = TransferTask<Result>;

/**
* Subpath between a root path and a depth
*
*/
export interface Subpath {
path: string;
}
1 change: 1 addition & 0 deletions packages/storage/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
TransferProgressEvent,
TransferTaskState,
UploadTask,
Subpath,
} from './common';
export {
StorageGetPropertiesInputWithKey,
Expand Down
2 changes: 2 additions & 0 deletions packages/storage/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export interface StorageOptions {

export type StorageListAllOptions = StorageOptions & {
listAll: true;
delimiter?: string;
};

export type StorageListPaginateOptions = StorageOptions & {
listAll?: false;
pageSize?: number;
nextToken?: string;
delimiter?: string;
};

export type StorageRemoveOptions = StorageOptions;
6 changes: 6 additions & 0 deletions packages/storage/src/types/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import { ResponseBodyMixin } from '@aws-amplify/core/internals/aws-client-utils';

import { Subpath } from './common';

/**
* Base type for a storage item.
*/
Expand Down Expand Up @@ -70,4 +72,8 @@ export interface StorageListOutput<Item extends StorageItem> {
* List of items returned by the list API.
*/
items: Item[];
/**
* List of subpaths returned by the list API.
israx marked this conversation as resolved.
Show resolved Hide resolved
*/
subpaths?: Subpath[];
israx marked this conversation as resolved.
Show resolved Hide resolved
}
Loading