|
35 | 35 | import software.amazon.awssdk.services.s3.S3Client; |
36 | 36 | import software.amazon.awssdk.services.s3.S3Configuration; |
37 | 37 | import software.amazon.awssdk.services.s3.model.CopyObjectResponse; |
| 38 | +import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; |
38 | 39 | import software.amazon.awssdk.services.s3.model.DeleteObjectResponse; |
39 | 40 | import software.amazon.awssdk.services.s3.model.DeleteObjectsResponse; |
40 | 41 | import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 42 | +import software.amazon.awssdk.services.s3.model.HeadObjectResponse; |
41 | 43 | import software.amazon.awssdk.services.s3.model.ObjectIdentifier; |
| 44 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
42 | 45 | import software.amazon.awssdk.services.s3.model.PutObjectResponse; |
43 | 46 | import software.amazon.awssdk.services.s3.model.S3Exception; |
| 47 | +import software.amazon.awssdk.services.s3.model.StorageClass; |
44 | 48 | import software.amazon.awssdk.services.s3.multipart.MultipartConfiguration; |
| 49 | +import software.amazon.encryption.s3.internal.ConvertSDKRequests; |
45 | 50 | import software.amazon.encryption.s3.internal.InstructionFileConfig; |
46 | 51 | import software.amazon.encryption.s3.materials.KmsKeyring; |
47 | 52 | import software.amazon.encryption.s3.utils.BoundedInputStream; |
|
66 | 71 |
|
67 | 72 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
68 | 73 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 74 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
69 | 75 | import static org.junit.jupiter.api.Assertions.assertThrows; |
70 | 76 | import static org.junit.jupiter.api.Assertions.assertTrue; |
71 | 77 | import static org.junit.jupiter.api.Assertions.fail; |
@@ -829,6 +835,111 @@ public void testAsyncInstructionFileConfig() { |
829 | 835 | s3ClientDisabledInstructionFile.close(); |
830 | 836 | s3Client.close(); |
831 | 837 | } |
| 838 | + @Test |
| 839 | + public void testAsyncInstructionFileConfigMultipart() { |
| 840 | + final String objectKey = appendTestSuffix("test-multipart-async-instruction-file-config"); |
| 841 | + final String input = "SimpleTestOfV3EncryptionClient"; |
| 842 | + |
| 843 | + AwsCredentialsProvider credentials = DefaultCredentialsProvider.create(); |
| 844 | + S3Client wrappedClient = S3Client.create(); |
| 845 | + S3AsyncClient v3Client = S3AsyncEncryptionClient.builder() |
| 846 | + .instructionFileConfig(InstructionFileConfig.builder() |
| 847 | + .instructionFileClient(wrappedClient) |
| 848 | + .enableInstructionFilePutObject(true) |
| 849 | + .build()) |
| 850 | + .kmsKeyId(KMS_KEY_ID) |
| 851 | + .enableMultipartPutObject(true) |
| 852 | + .credentialsProvider(credentials) |
| 853 | + .build(); |
| 854 | + PutObjectRequest request = PutObjectRequest.builder() |
| 855 | + .bucket(BUCKET) |
| 856 | + .key(objectKey) |
| 857 | + .build(); |
| 858 | + CompletableFuture<PutObjectResponse> putObjectResponse = v3Client.putObject(request, AsyncRequestBody.fromString(input)); |
| 859 | + putObjectResponse.join(); |
| 860 | + |
| 861 | + assertNotNull(putObjectResponse); |
| 862 | + |
| 863 | + ResponseBytes<GetObjectResponse> instructionFileResponse = wrappedClient.getObjectAsBytes(builder -> builder |
| 864 | + .bucket(BUCKET) |
| 865 | + .key(objectKey + ".instruction") |
| 866 | + .build()); |
| 867 | + assertNotNull(instructionFileResponse); |
| 868 | + assertTrue(instructionFileResponse.response().metadata().containsKey("x-amz-crypto-instr-file")); |
| 869 | + |
| 870 | + CompletableFuture<ResponseBytes<GetObjectResponse>> futureGetObj = v3Client.getObject(builder -> builder |
| 871 | + .bucket(BUCKET) |
| 872 | + .key(objectKey) |
| 873 | + .build(), AsyncResponseTransformer.toBytes()); |
| 874 | + ResponseBytes<GetObjectResponse> getResponse = futureGetObj.join(); |
| 875 | + assertNotNull(getResponse); |
| 876 | + assertEquals(input, getResponse.asUtf8String()); |
| 877 | + |
| 878 | + deleteObject(BUCKET, objectKey, v3Client); |
| 879 | + |
| 880 | + v3Client.close(); |
| 881 | + } |
| 882 | + @Test |
| 883 | + public void testAsyncInstructionFileConfigMultipartWithOptions() { |
| 884 | + final String objectKey = appendTestSuffix("test-multipart-async-instruction-file-config-options"); |
| 885 | + final String input = "SimpleTestOfV3EncryptionClient"; |
| 886 | + |
| 887 | + AwsCredentialsProvider credentials = DefaultCredentialsProvider.create(); |
| 888 | + S3Client wrappedClient = S3Client.create(); |
| 889 | + S3AsyncClient v3Client = S3AsyncEncryptionClient.builder() |
| 890 | + .instructionFileConfig(InstructionFileConfig.builder() |
| 891 | + .instructionFileClient(wrappedClient) |
| 892 | + .enableInstructionFilePutObject(true) |
| 893 | + .build()) |
| 894 | + .kmsKeyId(KMS_KEY_ID) |
| 895 | + .enableMultipartPutObject(true) |
| 896 | + .credentialsProvider(credentials) |
| 897 | + .build(); |
| 898 | + CreateMultipartUploadRequest multipartUploadRequest = CreateMultipartUploadRequest.builder() |
| 899 | + .bucket(BUCKET) |
| 900 | + .key(objectKey) |
| 901 | + .storageClass(StorageClass.STANDARD_IA) |
| 902 | + .build(); |
| 903 | + assertNotNull(multipartUploadRequest); |
| 904 | + |
| 905 | + PutObjectRequest putObjectRequest = ConvertSDKRequests.convertRequest(multipartUploadRequest); |
| 906 | + |
| 907 | + assertNotNull(putObjectRequest); |
| 908 | + assertEquals(putObjectRequest.storageClassAsString(), multipartUploadRequest.storageClassAsString()); |
| 909 | + |
| 910 | + CompletableFuture<PutObjectResponse> putObjectResponse = v3Client.putObject(putObjectRequest, AsyncRequestBody.fromString(input)); |
| 911 | + putObjectResponse.join(); |
| 912 | + |
| 913 | + assertNotNull(putObjectResponse); |
| 914 | + |
| 915 | + ResponseBytes<GetObjectResponse> instructionFileResponse = wrappedClient.getObjectAsBytes(builder -> builder |
| 916 | + .bucket(BUCKET) |
| 917 | + .key(objectKey + ".instruction") |
| 918 | + .build()); |
| 919 | + assertNotNull(instructionFileResponse); |
| 920 | + Map<String, String> metadata = instructionFileResponse.response().metadata(); |
| 921 | + assertTrue(metadata.containsKey("x-amz-crypto-instr-file")); |
| 922 | + |
| 923 | + HeadObjectResponse instructionHeadResponse = wrappedClient.headObject(builder -> builder |
| 924 | + .bucket(BUCKET) |
| 925 | + .key(objectKey + ".instruction") |
| 926 | + .build()); |
| 927 | + assertNotNull(instructionHeadResponse.storageClass()); |
| 928 | + assertEquals(instructionHeadResponse.storageClassAsString(), StorageClass.STANDARD_IA.toString()); |
| 929 | + CompletableFuture<ResponseBytes<GetObjectResponse>> futureGetObj = v3Client.getObject(builder -> builder |
| 930 | + .bucket(BUCKET) |
| 931 | + .key(objectKey) |
| 932 | + .build(), AsyncResponseTransformer.toBytes()); |
| 933 | + ResponseBytes<GetObjectResponse> getResponse = futureGetObj.join(); |
| 934 | + assertNotNull(getResponse); |
| 935 | + assertEquals(input, getResponse.asUtf8String()); |
| 936 | + |
| 937 | + assertEquals(getResponse.response().storageClassAsString(), StorageClass.STANDARD_IA.toString()); |
| 938 | + |
| 939 | + deleteObject(BUCKET, objectKey, v3Client); |
| 940 | + v3Client.close(); |
| 941 | + |
| 942 | + } |
832 | 943 |
|
833 | 944 | @Test |
834 | 945 | public void wrappedClientMultipartUploadThrowsException() throws IOException { |
|
0 commit comments