-
Notifications
You must be signed in to change notification settings - Fork 17
fix: Support all PutObjectRequest fields #458
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
Merged
Merged
Changes from all 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
144 changes: 144 additions & 0 deletions
144
src/main/java/software/amazon/encryption/s3/internal/ConvertSDKRequests.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,144 @@ | ||
package software.amazon.encryption.s3.internal; | ||
|
||
import software.amazon.awssdk.services.s3.model.ChecksumType; | ||
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
|
||
public class ConvertSDKRequests { | ||
|
||
public static CreateMultipartUploadRequest convert(PutObjectRequest request) { | ||
|
||
final CreateMultipartUploadRequest.Builder output = CreateMultipartUploadRequest.builder(); | ||
request | ||
.toBuilder() | ||
.sdkFields() | ||
.forEach(f -> { | ||
final Object value = f.getValueOrDefault(request); | ||
if (value != null) { | ||
switch (f.memberName()) { | ||
case "ACL": | ||
output.acl((String) value); | ||
break; | ||
case "Bucket": | ||
output.bucket((String) value); | ||
break; | ||
case "BucketKeyEnabled": | ||
output.bucketKeyEnabled((Boolean) value); | ||
break; | ||
case "CacheControl": | ||
output.cacheControl((String) value); | ||
break; | ||
case "ChecksumAlgorithm": | ||
output.checksumAlgorithm((String) value); | ||
break; | ||
case "ChecksumType": | ||
output.checksumType((ChecksumType) value); | ||
case "ContentDisposition": | ||
assert value instanceof String; | ||
output.contentDisposition((String) value); | ||
break; | ||
case "ContentEncoding": | ||
output.contentEncoding((String) value); | ||
break; | ||
case "ContentLanguage": | ||
output.contentLanguage((String) value); | ||
break; | ||
case "ContentType": | ||
output.contentType((String) value); | ||
break; | ||
case "ExpectedBucketOwner": | ||
output.expectedBucketOwner((String) value); | ||
break; | ||
case "Expires": | ||
output.expires((Instant) value); | ||
break; | ||
case "GrantFullControl": | ||
output.grantFullControl((String) value); | ||
break; | ||
case "GrantRead": | ||
output.grantRead((String) value); | ||
break; | ||
case "GrantReadACP": | ||
output.grantReadACP((String) value); | ||
break; | ||
case "GrantWriteACP": | ||
output.grantWriteACP((String) value); | ||
break; | ||
case "Key": | ||
output.key((String) value); | ||
break; | ||
case "Metadata": | ||
// The PutObjectRequest.builder().metadata(value) | ||
// only takes Map<String, String> therefore it should not be possible | ||
// to get here with anything other than a Map<String, String> | ||
// This may be overkill, but this map should be small | ||
// so the performance hit to verify this is worth the correctness. | ||
if (!isStringStringMap(value)) { | ||
throw new IllegalArgumentException("Metadata must be a Map<String, String>"); | ||
} | ||
@SuppressWarnings("unchecked") | ||
Map<String, String> metadata = (Map<String, String>) value; | ||
output.metadata(metadata); | ||
break; | ||
case "ObjectLockLegalHoldStatus": | ||
output.objectLockLegalHoldStatus((String) value); | ||
break; | ||
case "ObjectLockMode": | ||
output.objectLockMode((String) value); | ||
break; | ||
case "ObjectLockRetainUntilDate": | ||
output.objectLockRetainUntilDate((Instant) value); | ||
break; | ||
case "RequestPayer": | ||
output.requestPayer((String) value); | ||
break; | ||
case "ServerSideEncryption": | ||
output.serverSideEncryption((String) value); | ||
break; | ||
case "SSECustomerAlgorithm": | ||
output.sseCustomerAlgorithm((String) value); | ||
break; | ||
case "SSECustomerKey": | ||
output.sseCustomerKey((String) value); | ||
break; | ||
case "SSEKMSEncryptionContext": | ||
output.ssekmsEncryptionContext((String) value); | ||
break; | ||
case "SSEKMSKeyId": | ||
output.ssekmsKeyId((String) value); | ||
break; | ||
case "StorageClass": | ||
output.storageClass((String) value); | ||
break; | ||
case "Tagging": | ||
output.tagging((String) value); | ||
break; | ||
case "WebsiteRedirectLocation": | ||
output.websiteRedirectLocation((String) value); | ||
break; | ||
default: | ||
// Rather than silently dropping the value, | ||
// we loudly signal that we don't know how to handle this field. | ||
throw new IllegalArgumentException("Unknown PutObjectRequest field " + f.locationName() + "."); | ||
} | ||
} | ||
}); | ||
return output | ||
// OverrideConfiguration is not as SDKField but still needs to be supported | ||
.overrideConfiguration(request.overrideConfiguration().orElse(null)) | ||
.build(); | ||
} | ||
|
||
private static boolean isStringStringMap(Object value) { | ||
if (!(value instanceof Map)) { | ||
return false; | ||
} | ||
Map<?, ?> map = (Map<?, ?>) value; | ||
return map.entrySet().stream() | ||
.allMatch(entry -> entry != null | ||
&& ((Map.Entry<?, ?>) entry).getKey() instanceof String | ||
&& ((Map.Entry<?, ?>) entry).getValue() instanceof String); | ||
} | ||
} |
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.