|
17 | 17 | import static org.assertj.core.api.Assertions.assertThat;
|
18 | 18 | import static org.junit.Assert.assertNotNull;
|
19 | 19 | import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName;
|
| 20 | +import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; |
20 | 21 |
|
| 22 | +import java.io.IOException; |
| 23 | +import java.time.Duration; |
21 | 24 | import java.util.StringJoiner;
|
22 | 25 | import org.junit.AfterClass;
|
23 | 26 | import org.junit.BeforeClass;
|
24 | 27 | import org.junit.Test;
|
25 | 28 | import software.amazon.awssdk.core.sync.RequestBody;
|
| 29 | +import software.amazon.awssdk.http.HttpExecuteRequest; |
| 30 | +import software.amazon.awssdk.http.HttpExecuteResponse; |
| 31 | +import software.amazon.awssdk.http.SdkHttpClient; |
| 32 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 33 | +import software.amazon.awssdk.http.apache.ApacheHttpClient; |
26 | 34 | import software.amazon.awssdk.regions.Region;
|
27 | 35 | import software.amazon.awssdk.services.s3.S3Client;
|
28 | 36 | import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
29 | 37 | import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
| 38 | +import software.amazon.awssdk.services.s3.presigner.S3Presigner; |
30 | 39 | import software.amazon.awssdk.services.sts.StsClient;
|
| 40 | +import software.amazon.awssdk.utils.IoUtils; |
| 41 | +import software.amazon.awssdk.utils.StringInputStream; |
31 | 42 |
|
32 | 43 | public class S3AccessPointsIntegrationTest extends S3ControlIntegrationTestBase {
|
33 | 44 |
|
@@ -108,6 +119,51 @@ public void transfer_Succeeds_UsingAccessPoint_CrossRegion() {
|
108 | 119 | assertThat(objectContent).isEqualTo("helloworld");
|
109 | 120 | }
|
110 | 121 |
|
| 122 | + @Test |
| 123 | + public void uploadAndDownloadWithPresignedUrlWorks() throws IOException { |
| 124 | + String accessPointArn = new StringJoiner(":").add("arn").add("aws").add("s3").add("us-west-2").add(accountId) |
| 125 | + .add("accesspoint").add(AP_NAME).toString(); |
| 126 | + String key = "foo/a0A!-_.*'()&@:,$=+?; \n\\^`<>{}[]#%\"~|山"; |
| 127 | + |
| 128 | + testAccessPointPresigning(accessPointArn, key); |
| 129 | + } |
| 130 | + |
| 131 | + private void testAccessPointPresigning(String accessPointArn, String key) throws IOException { |
| 132 | + String data = "Hello"; |
| 133 | + |
| 134 | + S3Presigner presigner = S3Presigner.builder().region(Region.US_WEST_2).build(); |
| 135 | + |
| 136 | + SdkHttpRequest presignedPut = presigner.presignPutObject(r -> r.signatureDuration(Duration.ofDays(7)) |
| 137 | + .putObjectRequest(por -> por.bucket(accessPointArn) |
| 138 | + .key(key))) |
| 139 | + .httpRequest(); |
| 140 | + |
| 141 | + |
| 142 | + SdkHttpRequest presignedGet = presigner.presignGetObject(r -> r.signatureDuration(Duration.ofDays(7)) |
| 143 | + .getObjectRequest(gor -> gor.bucket(accessPointArn) |
| 144 | + .key(key))) |
| 145 | + .httpRequest(); |
| 146 | + |
| 147 | + try (SdkHttpClient client = ApacheHttpClient.create()) { |
| 148 | + client.prepareRequest(HttpExecuteRequest.builder() |
| 149 | + .request(presignedPut) |
| 150 | + .contentStreamProvider(() -> new StringInputStream(data)) |
| 151 | + .build()) |
| 152 | + .call(); |
| 153 | + |
| 154 | + HttpExecuteResponse getResult = client.prepareRequest(HttpExecuteRequest.builder() |
| 155 | + .request(presignedGet) |
| 156 | + .build()) |
| 157 | + .call(); |
| 158 | + |
| 159 | + String result = getResult.responseBody() |
| 160 | + .map(stream -> invokeSafely(() -> IoUtils.toUtf8String(stream))) |
| 161 | + .orElseThrow(AssertionError::new); |
| 162 | + |
| 163 | + assertThat(result).isEqualTo(data); |
| 164 | + } |
| 165 | + } |
| 166 | + |
111 | 167 | @Test
|
112 | 168 | public void accessPointOperation_nonArns() {
|
113 | 169 | assertNotNull(s3control.listAccessPoints(b -> b.bucket(BUCKET).accountId(accountId).maxResults(1)));
|
|
0 commit comments