-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trim object metadata keys for PUT and MPU
This brings the V2's behavior in line with 1.x
- Loading branch information
Showing
4 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "Amazon S3", | ||
"contributor": "", | ||
"description": "Automatically trim object metadata keys of whitespace for `PutObject` and `CreateMultipartUpload`." | ||
} |
74 changes: 74 additions & 0 deletions
74
.../java/software/amazon/awssdk/services/s3/internal/handlers/ObjectMetadataInterceptor.java
This file contains 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,74 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.services.s3.internal.handlers; | ||
|
||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.core.SdkRequest; | ||
import software.amazon.awssdk.core.interceptor.Context; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; | ||
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute; | ||
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
import software.amazon.awssdk.utils.StringUtils; | ||
|
||
/** | ||
* Interceptor that trims object metadata keys of any leading or trailing whitespace for {@code PutObject} and {@code | ||
* CreateMultipartUpload}. This behavior is intended to provide the same functionality as in 1.x. | ||
*/ | ||
@SdkInternalApi | ||
public final class ObjectMetadataInterceptor implements ExecutionInterceptor { | ||
@Override | ||
public SdkRequest modifyRequest(Context.ModifyRequest context, ExecutionAttributes executionAttributes) { | ||
SdkRequest request = context.request(); | ||
|
||
switch (executionAttributes.getAttribute(SdkExecutionAttribute.OPERATION_NAME)) { | ||
case "PutObject": | ||
return trimMetadataNames((PutObjectRequest) request); | ||
case "CreateMultipartUpload": | ||
return trimMetadataNames((CreateMultipartUploadRequest) request); | ||
default: | ||
return request; | ||
} | ||
} | ||
|
||
private PutObjectRequest trimMetadataNames(PutObjectRequest putObjectRequest) { | ||
if (!putObjectRequest.hasMetadata()) { | ||
return putObjectRequest; | ||
} | ||
|
||
return putObjectRequest.toBuilder() | ||
.metadata(trimKeys(putObjectRequest.metadata())) | ||
.build(); | ||
} | ||
|
||
private CreateMultipartUploadRequest trimMetadataNames(CreateMultipartUploadRequest createMultipartUploadRequest) { | ||
if (!createMultipartUploadRequest.hasMetadata()) { | ||
return createMultipartUploadRequest; | ||
} | ||
|
||
return createMultipartUploadRequest.toBuilder() | ||
.metadata(trimKeys(createMultipartUploadRequest.metadata())) | ||
.build(); | ||
} | ||
|
||
private Map<String, String> trimKeys(Map<String, String> map) { | ||
return map.entrySet().stream() | ||
.collect(Collectors.toMap(e -> StringUtils.trim(e.getKey()), Map.Entry::getValue)); | ||
} | ||
} |
This file contains 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
124 changes: 124 additions & 0 deletions
124
...a/software/amazon/awssdk/services/s3/internal/handlers/ObjectMetadataInterceptorTest.java
This file contains 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,124 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.services.s3.internal.handlers; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import org.junit.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import software.amazon.awssdk.core.SdkRequest; | ||
import software.amazon.awssdk.core.interceptor.Context; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute; | ||
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
||
public class ObjectMetadataInterceptorTest { | ||
private static final ObjectMetadataInterceptor INTERCEPTOR = new ObjectMetadataInterceptor(); | ||
|
||
|
||
|
||
public static List<TestCase> testCases() { | ||
return asList( | ||
tc(asList("a", "b", "c"), asList("a", "b", "c")), | ||
tc(asList(" a ", "b", "c"), asList("a", "b", "c")), | ||
tc(asList(" a", "\tb", "\tc"), asList("a", "b", "c")), | ||
tc(asList("a\n", "\tb", "\tc\r\n"), asList("a", "b", "c")) | ||
|
||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("testCases") | ||
public void modifyRequest_putObject_metadataKeysAreTrimmed(TestCase tc) { | ||
Map<String, String> metadata = tc.inputKeys.stream() | ||
.collect(Collectors.toMap(k -> k, k -> "value")); | ||
|
||
Context.ModifyHttpRequest ctx = mock(Context.ModifyHttpRequest.class); | ||
|
||
PutObjectRequest put = PutObjectRequest.builder() | ||
.metadata(metadata) | ||
.build(); | ||
|
||
when(ctx.request()).thenReturn(put); | ||
|
||
ExecutionAttributes attrs = new ExecutionAttributes(); | ||
attrs.putAttribute(SdkExecutionAttribute.OPERATION_NAME, "PutObject"); | ||
|
||
PutObjectRequest modified = (PutObjectRequest) INTERCEPTOR.modifyRequest(ctx, attrs); | ||
|
||
assertThat(modified.metadata().keySet()).containsExactlyElementsOf(tc.expectedKeys); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("testCases") | ||
public void modifyRequest_creatMultipartUpload_metadataKeysAreTrimmed(TestCase tc) { | ||
Map<String, String> metadata = tc.inputKeys.stream() | ||
.collect(Collectors.toMap(k -> k, k -> "value")); | ||
|
||
Context.ModifyHttpRequest ctx = mock(Context.ModifyHttpRequest.class); | ||
|
||
CreateMultipartUploadRequest mpu = CreateMultipartUploadRequest.builder() | ||
.metadata(metadata) | ||
.build(); | ||
|
||
when(ctx.request()).thenReturn(mpu); | ||
|
||
ExecutionAttributes attrs = new ExecutionAttributes(); | ||
attrs.putAttribute(SdkExecutionAttribute.OPERATION_NAME, "CreateMultipartUpload"); | ||
|
||
CreateMultipartUploadRequest modified = (CreateMultipartUploadRequest) INTERCEPTOR.modifyRequest(ctx, attrs); | ||
|
||
assertThat(modified.metadata().keySet()).containsExactlyElementsOf(tc.expectedKeys); | ||
} | ||
|
||
@Test | ||
public void modifyRequest_unknownOperation_ignores() { | ||
Context.ModifyHttpRequest ctx = mock(Context.ModifyHttpRequest.class); | ||
|
||
GetObjectRequest get = GetObjectRequest.builder().build(); | ||
|
||
when(ctx.request()).thenReturn(get); | ||
|
||
ExecutionAttributes attrs = new ExecutionAttributes(); | ||
attrs.putAttribute(SdkExecutionAttribute.OPERATION_NAME, "GetObject"); | ||
|
||
SdkRequest sdkRequest = INTERCEPTOR.modifyRequest(ctx, attrs); | ||
|
||
assertThat(sdkRequest).isEqualTo(get); | ||
} | ||
|
||
private static TestCase tc(List<String> input, List<String> expected) { | ||
return new TestCase(input, expected); | ||
} | ||
private static class TestCase { | ||
private List<String> inputKeys; | ||
private List<String> expectedKeys; | ||
|
||
public TestCase(List<String> inputKeys, List<String> expectedKeys) { | ||
this.inputKeys = inputKeys; | ||
this.expectedKeys = expectedKeys; | ||
} | ||
} | ||
} |