-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat(s3): add object annotations support #3324
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
Open
rafaelsierra
wants to merge
3
commits into
floci-io:main
Choose a base branch
from
rafaelsierra:feat/s3-object-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
224 changes: 224 additions & 0 deletions
224
compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3AnnotationsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| package com.floci.test; | ||
|
|
||
| import org.junit.jupiter.api.*; | ||
|
|
||
| import software.amazon.awssdk.core.ResponseBytes; | ||
| import software.amazon.awssdk.core.sync.RequestBody; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.AnnotationDirective; | ||
| import software.amazon.awssdk.services.s3.model.AnnotationEntry; | ||
| import software.amazon.awssdk.services.s3.model.BucketVersioningStatus; | ||
| import software.amazon.awssdk.services.s3.model.ChecksumMode; | ||
| import software.amazon.awssdk.services.s3.model.CreateBucketRequest; | ||
| import software.amazon.awssdk.services.s3.model.GetObjectAnnotationResponse; | ||
| import software.amazon.awssdk.services.s3.model.ListObjectAnnotationsRequest; | ||
| import software.amazon.awssdk.services.s3.model.ListObjectAnnotationsResponse; | ||
| import software.amazon.awssdk.services.s3.model.NoSuchAnnotationException; | ||
| import software.amazon.awssdk.services.s3.model.PutBucketVersioningRequest; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectAnnotationRequest; | ||
| import software.amazon.awssdk.services.s3.model.VersioningConfiguration; | ||
|
|
||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| @DisplayName("S3 Object Annotations") | ||
| @TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
| class S3AnnotationsTest { | ||
|
|
||
| private static S3Client s3; | ||
| private static final String BUCKET = "sdk-annotations-bucket"; | ||
| private static final String KEY = "docs/annotated.txt"; | ||
|
|
||
| @BeforeAll | ||
| static void setup() { | ||
| s3 = TestFixtures.s3Client(); | ||
| s3.createBucket(CreateBucketRequest.builder().bucket(BUCKET).build()); | ||
| s3.putObject(r -> r.bucket(BUCKET).key(KEY), RequestBody.fromString("annotated object body")); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void teardown() { | ||
| for (String key : new String[]{"docs/annotated.txt", "docs/annotated-copy.txt", "docs/annotated-copied.txt"}) { | ||
| try { | ||
| s3.deleteObject(r -> r.bucket(BUCKET).key(key)); | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
| try { | ||
| s3.deleteBucket(r -> r.bucket(BUCKET)); | ||
| } catch (Exception ignored) { | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @Order(1) | ||
| void putObjectAnnotation() { | ||
| var response = s3.putObjectAnnotation(PutObjectAnnotationRequest.builder() | ||
| .bucket(BUCKET) | ||
| .key(KEY) | ||
| .annotationName("classification") | ||
| .build(), | ||
| RequestBody.fromString("{\"label\": \"report\"}")); | ||
|
|
||
| assertThat(response.key()).isEqualTo(KEY); | ||
| assertThat(response.annotationName()).isEqualTo("classification"); | ||
| assertThat(response.eTag()).isNotBlank(); | ||
| // CRC64NVME is the AWS default algorithm for annotations without an explicit one. | ||
| // The SDK computes a CRC32 payload checksum client-side by default and expects it echoed. | ||
| assertThat(response.checksumCRC32()).isNotBlank(); | ||
| assertThat(response.checksumTypeAsString()).isEqualTo("FULL_OBJECT"); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(2) | ||
| void getObjectAnnotation() { | ||
| ResponseBytes<GetObjectAnnotationResponse> response = s3.getObjectAnnotationAsBytes( | ||
| r -> r.bucket(BUCKET).key(KEY).annotationName("classification").build()); | ||
|
|
||
| assertThat(response.asUtf8String()).isEqualTo("{\"label\": \"report\"}"); | ||
| assertThat(response.response().eTag()).isNotBlank(); | ||
| assertThat(response.response().lastModified()).isNotNull(); | ||
| assertThat(response.response().contentLength()).isEqualTo(19L); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(3) | ||
| void getObjectAnnotationWithChecksumMode() { | ||
| ResponseBytes<GetObjectAnnotationResponse> response = s3.getObjectAnnotationAsBytes( | ||
| r -> r.bucket(BUCKET).key(KEY) | ||
| .annotationName("classification") | ||
| .checksumMode(ChecksumMode.ENABLED) | ||
| .build()); | ||
|
|
||
| assertThat(response.response().checksumCRC32()).isNotBlank(); | ||
| assertThat(response.response().checksumTypeAsString()).isEqualTo("FULL_OBJECT"); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(4) | ||
| void listObjectAnnotations() { | ||
| s3.putObjectAnnotation(PutObjectAnnotationRequest.builder() | ||
| .bucket(BUCKET).key(KEY).annotationName("summary") | ||
| .build(), | ||
| RequestBody.fromString("summary text")); | ||
|
|
||
| ListObjectAnnotationsResponse response = s3.listObjectAnnotations( | ||
| ListObjectAnnotationsRequest.builder().bucket(BUCKET).key(KEY).build()); | ||
|
|
||
| assertThat(response.bucket()).isEqualTo(BUCKET); | ||
| assertThat(response.key()).isEqualTo(KEY); | ||
| assertThat(response.annotationCount()).isEqualTo(2); | ||
| assertThat(response.annotations()) | ||
| .extracting(AnnotationEntry::annotationName) | ||
| .containsExactly("classification", "summary"); | ||
| assertThat(response.annotations()) | ||
| .filteredOn(a -> "summary".equals(a.annotationName())) | ||
| .allSatisfy(a -> assertThat(a.size()).isEqualTo(12L)); | ||
| assertThat(response.nextContinuationToken()).isNull(); | ||
| assertThat(response.maxAnnotationResults()).isEqualTo(1000); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(5) | ||
| void putObjectAnnotationWithChecksum() { | ||
| var response = s3.putObjectAnnotation(PutObjectAnnotationRequest.builder() | ||
| .bucket(BUCKET).key(KEY).annotationName("hashed") | ||
| .checksumAlgorithm(software.amazon.awssdk.services.s3.model.ChecksumAlgorithm.SHA256) | ||
| .build(), | ||
| RequestBody.fromString("checksummed payload")); | ||
|
|
||
| assertThat(response.checksumSHA256()).isNotBlank(); | ||
| assertThat(response.eTag()).isNotBlank(); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(6) | ||
| void getMissingAnnotationThrows() { | ||
| assertThatThrownBy(() -> s3.getObjectAnnotationAsBytes(r -> r.bucket(BUCKET).key(KEY) | ||
| .annotationName("missing").build())) | ||
| .isInstanceOf(NoSuchAnnotationException.class); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(7) | ||
| void deleteObjectAnnotationIsIdempotent() { | ||
| s3.deleteObjectAnnotation(r -> r.bucket(BUCKET).key(KEY).annotationName("hashed")); | ||
|
|
||
| assertThatThrownBy(() -> s3.getObjectAnnotationAsBytes(r -> r.bucket(BUCKET).key(KEY) | ||
| .annotationName("hashed").build())) | ||
| .isInstanceOf(NoSuchAnnotationException.class); | ||
|
|
||
| // Deleting a nonexistent annotation is not an error. | ||
| assertThatCode(() -> s3.deleteObjectAnnotation(r -> r.bucket(BUCKET).key(KEY) | ||
| .annotationName("hashed"))).doesNotThrowAnyException(); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(8) | ||
| void copyObjectExcludeDirectiveSkipsAnnotations() { | ||
| s3.copyObject(r -> r.sourceBucket(BUCKET).sourceKey(KEY) | ||
| .destinationBucket(BUCKET).destinationKey("docs/annotated-copy.txt") | ||
| .annotationDirective(AnnotationDirective.EXCLUDE)); | ||
|
|
||
| assertThatThrownBy(() -> s3.getObjectAnnotationAsBytes(r -> r.bucket(BUCKET) | ||
| .key("docs/annotated-copy.txt").annotationName("classification").build())) | ||
| .isInstanceOf(NoSuchAnnotationException.class); | ||
| } | ||
|
|
||
| // ========== Versioned bucket behavior ========== | ||
|
|
||
| @Test | ||
| @Order(10) | ||
| void enableVersioning() { | ||
| s3.putBucketVersioning(r -> r.bucket(BUCKET) | ||
| .versioningConfiguration(VersioningConfiguration.builder() | ||
| .status(BucketVersioningStatus.ENABLED) | ||
| .build())); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(11) | ||
| void annotationAttachesToSpecificVersion() { | ||
| s3.putObject(r -> r.bucket(BUCKET).key(KEY), RequestBody.fromString("version one")); | ||
| var v1 = s3.putObjectAnnotation(PutObjectAnnotationRequest.builder() | ||
| .bucket(BUCKET).key(KEY).annotationName("v1-note") | ||
| .build(), | ||
| RequestBody.fromString("on version one")); | ||
| assertThat(v1.objectVersionId()).isNotBlank(); | ||
|
|
||
| s3.putObject(r -> r.bucket(BUCKET).key(KEY), RequestBody.fromString("version two")); | ||
|
|
||
| // The new version has no annotations; the old one's stay reachable by versionId. | ||
| assertThatThrownBy(() -> s3.getObjectAnnotationAsBytes(r -> r.bucket(BUCKET).key(KEY) | ||
| .annotationName("v1-note").build())) | ||
| .isInstanceOf(NoSuchAnnotationException.class); | ||
|
|
||
| ResponseBytes<GetObjectAnnotationResponse> fromV1 = s3.getObjectAnnotationAsBytes( | ||
| r -> r.bucket(BUCKET).key(KEY) | ||
| .annotationName("v1-note") | ||
| .versionId(v1.objectVersionId()) | ||
| .build()); | ||
| assertThat(fromV1.asUtf8String()).isEqualTo("on version one"); | ||
| assertThat(fromV1.response().objectVersionId()).isEqualTo(v1.objectVersionId()); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(12) | ||
| void copyObjectCopiesAnnotationsByDefault() { | ||
| // The latest version (the copy source) must carry an annotation for the copy to take. | ||
| s3.putObjectAnnotation(PutObjectAnnotationRequest.builder() | ||
| .bucket(BUCKET).key(KEY).annotationName("latest-note") | ||
| .build(), | ||
| RequestBody.fromString("copied annotation")); | ||
| s3.copyObject(r -> r.sourceBucket(BUCKET).sourceKey(KEY) | ||
| .destinationBucket(BUCKET).destinationKey("docs/annotated-copied.txt")); | ||
|
|
||
| ResponseBytes<GetObjectAnnotationResponse> copied = s3.getObjectAnnotationAsBytes( | ||
| r -> r.bucket(BUCKET) | ||
| .key("docs/annotated-copied.txt").annotationName("latest-note") | ||
| .versionId(s3.headObject(b -> b.bucket(BUCKET).key("docs/annotated-copied.txt")) | ||
| .versionId()) | ||
| .build()); | ||
| assertThat(copied.asUtf8String()).isEqualTo("copied annotation"); | ||
| s3.deleteObject(r -> r.bucket(BUCKET).key("docs/annotated-copied.txt")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.