Skip to content

[Hackathon] Add method to S3Client: deleteBucketAndAllContents #3070

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

Conversation

Bennett-Lynch
Copy link
Contributor

No description provided.

@Bennett-Lynch Bennett-Lynch requested a review from cenedhryn March 2, 2022 06:34
@Bennett-Lynch Bennett-Lynch requested a review from a team as a code owner March 2, 2022 06:34
}

s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucketName).build());
s3.deleteBucketAndAllContents(bucketName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Red code is the best code!

createBucket(BUCKET);
s3.putBucketVersioning(r -> r
.bucket(BUCKET)
.versioningConfiguration(v -> v.status(BucketVersioningStatus.ENABLED)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a test that sets up a more complicated bucket where objects have different versions?

Copy link
Contributor Author

@Bennett-Lynch Bennett-Lynch Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I could rewrite the same first ~50 key names. Would that be sufficient?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

*/
@SdkExtensionMethod
default void deleteBucketAndAllContents(String bucket) {
new DeleteBucketAndAllContents((S3Client) this).deleteBucketAndAllContents(bucket);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delegation has again proven to be more flexible. Good choice using a separate class.

import software.amazon.awssdk.utils.Validate;

@SdkInternalApi
public class DeleteBucketAndAllContents implements S3ClientSdkExtension {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spirit of a hackathon I'm biasing more towards the integ tests for coverage right now. Would like to defer unit tests for now just since they're time consuming, but will absolutely require them before a potential public release.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough!

* S3Client#listObjectVersions(ListObjectVersionsRequest)} APIs to list a bucket's content, buffer keys that are eligible for
* deletion into batches of 1000, and delete them in bulk with the {@link S3Client#deleteObjects(DeleteObjectsRequest)} API.
* <p>
* While this method is optimized to use batch APIs for both listing and deleting, it may not be suitable for buckets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may also be useful to direct people to TransferManager for their more complicated needs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, but I don't think we currently plan to support "delete directory" in TM, but it makes sense to support it there.

private void deleteObjectStream(String bucket, Stream<ObjectIdentifier> stream) {
Iterator<List<ObjectIdentifier>> batchIterator = batch(stream.iterator(), MAX_DELETE_OBJECTS_SIZE);
batchIterator.forEachRemaining(objects -> {
log.debug(() -> String.format("Deleting %s objects: %s", objects.size(), objects));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line look like in the log, specifically objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It logs the key names, which may not be appropriate. I was considering removing the logs altogether for that reason.

Here's an example from CopySourceIntegrationTest:

Deleting S3 bucket: copy-source-integ-test-src-lyncbenn-2036
10:18:10.213 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Emptying bucket: copy-source-integ-test-src-lyncbenn-2036
10:18:10.398 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Deleting 5 objects: [ObjectIdentifier(Key=%20), ObjectIdentifier(Key=key/with/slashes), ObjectIdentifier(Key=simpleKey), ObjectIdentifier(Key=specialChars/ +!#$&'()*,:;=?@"), ObjectIdentifier(Key=🪣)]
10:18:10.943 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Deleting bucket: copy-source-integ-test-src-lyncbenn-2036
Deleting S3 bucket: copy-source-integ-test-versioned-src-lyncbenn-1687
10:18:11.235 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Emptying bucket: copy-source-integ-test-versioned-src-lyncbenn-1687
10:18:11.335 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Deleting 5 objects: [ObjectIdentifier(Key=%20), ObjectIdentifier(Key=key/with/slashes), ObjectIdentifier(Key=simpleKey), ObjectIdentifier(Key=specialChars/ +!#$&'()*,:;=?@"), ObjectIdentifier(Key=🪣)]
10:18:11.909 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Deleting 20 objects: [ObjectIdentifier(Key=%20, VersionId=V.sVzY.75kBdHEqJM39by_Z5sTBHZ7fQ), ObjectIdentifier(Key=%20, VersionId=sWggSroHE_WHRIOSZaDKZ2brqRu7yZhO), ObjectIdentifier(Key=%20, VersionId=fhPc80IYqCW9CjP.STRJNF2B1vlj4wHB), ObjectIdentifier(Key=key/with/slashes, VersionId=Gav4gvn8A5Ip.Ofmu8w0qmDyZWeFOI_T), ObjectIdentifier(Key=key/with/slashes, VersionId=7AhHansYt7WA5ON1ZEJ0EE0QREBE4.Pk), ObjectIdentifier(Key=key/with/slashes, VersionId=NsKZD8LYIBXs_YECP54aNSJ0QDgFgbYm), ObjectIdentifier(Key=simpleKey, VersionId=H5nEdAB_uTfl7ZA05WG0875hlfpoRn.y), ObjectIdentifier(Key=simpleKey, VersionId=doUGPN_k40B.sh8mGSGooGFn4TKvW1XE), ObjectIdentifier(Key=simpleKey, VersionId=3isxbpv5bC4u0SWKP_32YZ_h7d7VW_Y4), ObjectIdentifier(Key=specialChars/ +!#$&'()*,:;=?@", VersionId=dOYfzJl.xJe.ipWuuilM1M3jtLSYU7Dz), ObjectIdentifier(Key=specialChars/ +!#$&'()*,:;=?@", VersionId=duQuW7pjfUxRim5080eXlAZ83C8CfbRk), ObjectIdentifier(Key=specialChars/ +!#$&'()*,:;=?@", VersionId=VjBGda03yhKoVHCkz6c6DyiqXk3XAZH3), ObjectIdentifier(Key=🪣, VersionId=2IDkpsK3YOGXjvPefVkEOVk0aj7_clYf), ObjectIdentifier(Key=🪣, VersionId=IA2lPaG1Kf57vtCv2ebcVA_h12aHVdLC), ObjectIdentifier(Key=🪣, VersionId=RcMPfGgo0ejdLy_mlDXiHP5w1B9nsR_d), ObjectIdentifier(Key=%20, VersionId=Q25OFLzsmtzUIiU7cIDEZ2AWBH1IgQSJ), ObjectIdentifier(Key=key/with/slashes, VersionId=4bUxcv.SPPW0WwkejxKU9X2PnAz1XkRO), ObjectIdentifier(Key=simpleKey, VersionId=kUHpkEd3zuL6k2kkXfSe5QYTHlGPaKnE), ObjectIdentifier(Key=specialChars/ +!#$&'()*,:;=?@", VersionId=xof9bJbMxbR7WhXsQ8wGFHfn.1rWuioL), ObjectIdentifier(Key=🪣, VersionId=ZfRQ8UITh3_qflMbXHOCV9nRKP9AKcMK)]
10:18:12.300 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Deleting bucket: copy-source-integ-test-versioned-src-lyncbenn-1687
Deleting S3 bucket: copy-source-integ-test-dest-lyncbenn-7871
10:18:12.830 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Emptying bucket: copy-source-integ-test-dest-lyncbenn-7871
10:18:12.915 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Deleting 5 objects: [ObjectIdentifier(Key=%20), ObjectIdentifier(Key=key/with/slashes), ObjectIdentifier(Key=simpleKey), ObjectIdentifier(Key=specialChars/ +!#$&'()*,:;=?@"), ObjectIdentifier(Key=🪣)]
10:18:13.653 [main] DEBUG software.amazon.awssdk.services.s3.internal.extensions.DeleteBucketAndAllContents - Deleting bucket: copy-source-integ-test-dest-lyncbenn-7871

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, agreed to remove user info.

@Bennett-Lynch Bennett-Lynch merged commit 9aeaa9c into aws:hackathon-2022-extension-methods Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants