Skip to content

Commit cc399bb

Browse files
committed
delete objects initial implementation
1 parent 91c2933 commit cc399bb

File tree

15 files changed

+307
-0
lines changed

15 files changed

+307
-0
lines changed

packages/storage/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export {
99
getProperties,
1010
copy,
1111
getUrl,
12+
removeObjects,
1213
} from './providers/s3';
1314

1415
export {
@@ -18,6 +19,7 @@ export {
1819
DownloadDataWithPathInput,
1920
RemoveInput,
2021
RemoveWithPathInput,
22+
RemoveObjectsInput,
2123
ListAllInput,
2224
ListAllWithPathInput,
2325
ListPaginateInput,
@@ -37,6 +39,7 @@ export {
3739
DownloadDataWithPathOutput,
3840
RemoveOutput,
3941
RemoveWithPathOutput,
42+
RemoveObjectsOutput,
4043
ListAllOutput,
4144
ListAllWithPathOutput,
4245
ListPaginateOutput,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { Amplify } from '@aws-amplify/core';
5+
6+
import { removeObjects as removeObjectsInternal } from '../../providers/s3/apis/internal/removeObjects';
7+
import { RemoveObjectsInput } from '../types/inputs';
8+
import { RemoveObjectsOutput } from '../types/outputs';
9+
10+
/**
11+
* @internal
12+
*/
13+
export const removeObjects = (
14+
input: RemoveObjectsInput,
15+
): Promise<RemoveObjectsOutput> =>
16+
removeObjectsInternal(Amplify, input as any) as Promise<RemoveObjectsOutput>;

packages/storage/src/internals/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export {
1818
ListAllInput,
1919
ListPaginateInput,
2020
RemoveInput,
21+
RemoveObjectsInput,
2122
UploadDataInput,
2223
DownloadDataInput,
2324
} from './types/inputs';
@@ -27,6 +28,7 @@ export {
2728
GetPropertiesOutput,
2829
GetUrlOutput,
2930
RemoveOutput,
31+
RemoveObjectsOutput,
3032
UploadDataOutput,
3133
DownloadDataOutput,
3234
ListOutput,
@@ -39,6 +41,7 @@ export { list } from './apis/list';
3941
export { getProperties } from './apis/getProperties';
4042
export { getUrl } from './apis/getUrl';
4143
export { remove } from './apis/remove';
44+
export { removeObjects } from './apis/removeObjects';
4245
export { uploadData } from './apis/uploadData';
4346
export { downloadData } from './apis/downloadData';
4447
export { copy } from './apis/copy';

packages/storage/src/internals/types/inputs.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
GetUrlWithPathInput,
1414
ListAllWithPathInput,
1515
ListPaginateWithPathInput,
16+
RemoveObjectsInput as RemoveObjectsWithPathInput,
1617
RemoveWithPathInput,
1718
UploadDataWithPathInput,
1819
} from '../../providers/s3';
@@ -79,6 +80,14 @@ export type RemoveInput = ExtendInputWithAdvancedOptions<
7980
AdvancedOptions
8081
>;
8182

83+
/**
84+
* @internal
85+
*/
86+
export type RemoveObjectsInput = ExtendInputWithAdvancedOptions<
87+
RemoveObjectsWithPathInput,
88+
AdvancedOptions
89+
>;
90+
8291
/**
8392
* @internal
8493
*/

packages/storage/src/internals/types/outputs.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
GetUrlWithPathOutput,
99
ListAllWithPathOutput,
1010
ListPaginateWithPathOutput,
11+
RemoveObjectsOutput as RemoveObjectsWithPathOutput,
1112
RemoveWithPathOutput,
1213
UploadDataWithPathOutput,
1314
} from '../../providers/s3/types';
@@ -44,6 +45,11 @@ export type GetUrlOutput = GetUrlWithPathOutput;
4445
*/
4546
export type RemoveOutput = RemoveWithPathOutput;
4647

48+
/**
49+
* @internal
50+
*/
51+
export type RemoveObjectsOutput = RemoveObjectsWithPathOutput;
52+
4753
/**
4854
* @internal
4955
*/

packages/storage/src/providers/s3/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
export { uploadData } from './uploadData';
55
export { downloadData } from './downloadData';
66
export { remove } from './remove';
7+
export { removeObjects } from './removeObjects';
78
export { list } from './list';
89
export { getProperties } from './getProperties';
910
export { copy } from './copy';
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { AmplifyClassV6 } from '@aws-amplify/core';
5+
import { StorageAction } from '@aws-amplify/core/internals/utils';
6+
7+
import { RemoveObjectsInput, RemoveObjectsOutput } from '../../types';
8+
import {
9+
calculateContentMd5,
10+
resolveS3ConfigAndInput,
11+
validateBucketOwnerID,
12+
} from '../../utils';
13+
import { deleteObjects } from '../../utils/client/s3data';
14+
import { getStorageUserAgentValue } from '../../utils/userAgent';
15+
import { logger } from '../../../../utils';
16+
17+
export const removeObjects = async (
18+
amplify: AmplifyClassV6,
19+
input: RemoveObjectsInput,
20+
): Promise<RemoveObjectsOutput> => {
21+
// @ts-expect-error input type
22+
const { s3Config, bucket } = await resolveS3ConfigAndInput(amplify, input);
23+
24+
validateBucketOwnerID(input.options?.expectedBucketOwner);
25+
26+
if (!input.paths || input.paths.length === 0) {
27+
throw new Error('At least one path must be provided');
28+
}
29+
30+
if (input.paths.length > 1000) {
31+
throw new Error('Cannot delete more than 1000 objects in a single request');
32+
}
33+
34+
logger.debug(
35+
`removing ${input.paths.length} objects from bucket "${bucket}"`,
36+
);
37+
38+
// Build XML body for MD5 calculation
39+
const objects = input.paths
40+
.map(path => `<Object><Key>${path}</Key></Object>`)
41+
.join('');
42+
const xmlBody = `<?xml version="1.0" encoding="UTF-8"?>
43+
<Delete xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
44+
<Quiet>false</Quiet>
45+
${objects}
46+
</Delete>`;
47+
48+
// Calculate Content-MD5 (required for DeleteObjects)
49+
const contentMd5 = await calculateContentMd5(xmlBody);
50+
51+
const result = await deleteObjects(
52+
{
53+
...s3Config,
54+
userAgentValue: getStorageUserAgentValue(StorageAction.Remove),
55+
},
56+
{
57+
Bucket: bucket,
58+
Delete: {
59+
Objects: input.paths.map(path => ({ Key: path })),
60+
Quiet: false,
61+
},
62+
ExpectedBucketOwner: input.options?.expectedBucketOwner,
63+
ContentMD5: contentMd5,
64+
},
65+
);
66+
67+
return {
68+
deleted: result.Deleted?.map(item => ({ path: item.Key! })) || [],
69+
errors:
70+
result.Errors?.map(error => ({
71+
path: error.Key!,
72+
code: error.Code!,
73+
message: error.Message!,
74+
})) || [],
75+
};
76+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { Amplify } from '@aws-amplify/core';
5+
6+
import { RemoveObjectsInput, RemoveObjectsOutput } from '../types';
7+
8+
import { removeObjects as removeObjectsInternal } from './internal/removeObjects';
9+
10+
/**
11+
* Remove multiple files from your S3 bucket in a single request.
12+
* @param input - The `RemoveObjectsInput` object containing paths to remove.
13+
* @return Output containing the removed object paths and any errors.
14+
* @throws service: `S3Exception` - S3 service errors thrown while removing objects.
15+
* @throws validation: `StorageValidationErrorCode` - Validation errors thrown
16+
* when there are no paths or paths are empty.
17+
*/
18+
export function removeObjects(
19+
input: RemoveObjectsInput,
20+
): Promise<RemoveObjectsOutput> {
21+
return removeObjectsInternal(Amplify, input);
22+
}

packages/storage/src/providers/s3/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export {
99
getProperties,
1010
copy,
1111
getUrl,
12+
removeObjects,
1213
} from './apis';
1314

1415
export {
@@ -18,6 +19,7 @@ export {
1819
DownloadDataWithPathInput,
1920
RemoveInput,
2021
RemoveWithPathInput,
22+
RemoveObjectsInput,
2123
ListAllInput,
2224
ListAllWithPathInput,
2325
ListPaginateInput,
@@ -37,6 +39,7 @@ export {
3739
DownloadDataWithPathOutput,
3840
RemoveOutput,
3941
RemoveWithPathOutput,
42+
RemoveObjectsOutput,
4043
ListAllOutput,
4144
ListAllWithPathOutput,
4245
ListPaginateOutput,

packages/storage/src/providers/s3/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {
2525
DownloadDataWithPathOutput,
2626
RemoveOutput,
2727
RemoveWithPathOutput,
28+
RemoveObjectsOutput,
2829
ListAllOutput,
2930
ListAllWithPathOutput,
3031
ListPaginateOutput,
@@ -47,6 +48,7 @@ export {
4748
GetUrlWithPathInput,
4849
RemoveWithPathInput,
4950
RemoveInput,
51+
RemoveObjectsInput,
5052
DownloadDataInput,
5153
DownloadDataWithPathInput,
5254
UploadDataInput,

0 commit comments

Comments
 (0)