Skip to content

Commit 08f45df

Browse files
authored
Add support for S3Object getObjectMetadata transform (#6630)
* Add support for S3Object getObjectMetadata transform * Fix whitespace in tests
1 parent 5da5c4e commit 08f45df

File tree

8 files changed

+52
-20
lines changed

8 files changed

+52
-20
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2 Migration Tool",
4+
"contributor": "",
5+
"description": "Add support for S3Object getObjectMetadata transform"
6+
}

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/S3Streaming.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void getObject(String bucket, String key, File file) throws Exception {
4343
.build());
4444
s3Object.close();
4545

46+
String cacheControl = s3Object.response().cacheControl();
47+
4648
GetObjectResponse objectMetadata = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key)
4749
.build(), file.toPath());
4850
}

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/after/src/main/java/foo/bar/SdkClientsDependencyFactory.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ public static ClientOverrideConfiguration customClientConfiguration() {
4848

4949
public static SqsClient sqsClientWithAllSettings() {
5050
return SqsClient.builder()
51-
.region(Region.of("us-west-2"))
52-
.httpClientBuilder(ApacheHttpClient.builder()
53-
.connectionMaxIdleTime(Duration.ofMillis(1006))
54-
.tcpKeepAlive(true)
55-
.socketTimeout(Duration.ofMillis(1004))
56-
.connectionTimeToLive(Duration.ofMillis(1005))
57-
.connectionTimeout(Duration.ofMillis(1003))
58-
.maxConnections(1002))
59-
.overrideConfiguration(customClientConfiguration())
51+
.region(Region.of("us-west-2"))
52+
.httpClientBuilder(ApacheHttpClient.builder()
53+
.connectionMaxIdleTime(Duration.ofMillis(1006))
54+
.tcpKeepAlive(true)
55+
.socketTimeout(Duration.ofMillis(1004))
56+
.connectionTimeToLive(Duration.ofMillis(1005))
57+
.connectionTimeout(Duration.ofMillis(1003))
58+
.maxConnections(1002))
59+
.overrideConfiguration(customClientConfiguration())
6060
.credentialsProvider(CredentialsDependencyFactory.defaultCredentialsProviderChain())
61-
.build();
61+
.build();
6262
}
6363

6464
public static SqsClientBuilder sqsClientBuilder() {
@@ -90,6 +90,6 @@ public static SqsAsyncClient sqsAsyncClientWithAllSettings() {
9090
.httpClientBuilder(NettyNioAsyncHttpClient.builder()
9191
.connectionTimeout(Duration.ofMillis(2004)))
9292
.overrideConfiguration(clientConfiguration)
93-
.build();
93+
.build();
9494
}
9595
}

test/v2-migration-tests/src/test/resources/software/amazon/awssdk/v2migrationtests/maven/before/src/main/java/foo/bar/S3Streaming.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ void getObject(String bucket, String key, File file) throws Exception {
4040
S3Object s3Object = s3.getObject(bucket, key);
4141
s3Object.getObjectContent().close();
4242

43+
String cacheControl = s3Object.getObjectMetadata().getCacheControl();
44+
4345
ObjectMetadata objectMetadata = s3.getObject(new GetObjectRequest(bucket, key), file);
4446
}
4547

v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3PojoToV2.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext executionContext) {
7373
params.get(0), params.get(1), params.get(2), params.get(3));
7474
}
7575
if (type.isAssignableFrom(OBJECT_TAGGING) && newClass.getArguments().size() == 1) {
76+
addV2S3ModelImport("Tagging");
7677
String v2Builder = "Tagging.builder().tagSet(#{any()}).build();";
7778
return JavaTemplate.builder(v2Builder)
7879
.build().apply(getCursor(), newClass.getCoordinates().replace(),
7980
newClass.getArguments().get(0));
8081
}
8182
if (type.isAssignableFrom(GET_OBJECT_TAGGING_RESULT) && newClass.getArguments().size() == 1) {
83+
addV2S3ModelImport("GetObjectTaggingResponse");
8284
String v2Builder = "GetObjectTaggingResponse.builder().tagSet(#{any()}).build();";
8385
return JavaTemplate.builder(v2Builder)
8486
.build().apply(getCursor(), newClass.getCoordinates().replace(),

v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3PutObjectRequestToV2.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.getArgumentName;
2525
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.getSelectName;
2626
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.inputStreamBufferingWarningComment;
27+
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isObjectMetadataGetter;
2728
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isObjectMetadataSetter;
2829
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isPayloadSetter;
2930
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isPutObjectRequestBuilderSetter;
@@ -95,6 +96,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
9596
if (isObjectMetadataSetter(method)) {
9697
return saveMetadataValueAndRemoveStatement(method);
9798
}
99+
if (isObjectMetadataGetter(method)) {
100+
if (method.getSelect() != null) {
101+
return method.getSelect().withPrefix(method.getPrefix());
102+
}
103+
}
98104

99105
if (isPutObjectRequestBuilderSetter(method)) {
100106
if (isRequestMetadataSetter(method)) {

v2-migration/src/main/java/software/amazon/awssdk/v2migration/internal/utils/S3TransformUtils.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ public static boolean isObjectMetadataSetter(J.MethodInvocation method) {
207207
return isSetterForClassType(method, V2_S3_MODEL_PKG + "HeadObjectResponse");
208208
}
209209

210+
public static boolean isObjectMetadataGetter(J.MethodInvocation method) {
211+
if (!"objectMetadata".equals(method.getSimpleName()) || hasArguments(method)) {
212+
return false;
213+
}
214+
215+
Expression select = method.getSelect();
216+
if (!(select instanceof J.MethodInvocation)) {
217+
return false;
218+
}
219+
220+
J.MethodInvocation receiverMethod = (J.MethodInvocation) select;
221+
return "response".equals(receiverMethod.getSimpleName());
222+
}
223+
210224
/** Field set during POJO instantiation, e.g.,
211225
* PutObjectRequest request = new PutObjectRequest("bucket" "key", "redirectLocation").withFile(file);
212226
*/
@@ -236,7 +250,7 @@ public static boolean isSetterForClassType(J.MethodInvocation method, String fqc
236250
}
237251

238252
public static boolean hasArguments(J.MethodInvocation method) {
239-
return !method.getArguments().isEmpty();
253+
return method.getArguments().stream().anyMatch(arg -> !(arg instanceof J.Empty));
240254
}
241255

242256
public static boolean isPayloadSetter(J.MethodInvocation method) {

v2-migration/src/test/java/software/amazon/awssdk/v2migration/ChangeConfigTypesTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void configurationWithHttpSettings_localVariable_shouldRemoveAndSetOnSdkClient()
164164
+ " .connectionTimeout(Duration.ofMillis(1000))\n"
165165
+ " .maxConnections(1000))\n"
166166
+ " .overrideConfiguration(clientConfiguration)\n"
167-
+ " .build();\n"
167+
+ " .build();\n"
168168
+ " }\n"
169169
+ "}"
170170
)
@@ -218,7 +218,7 @@ void configurationWithHttpSettings_memberVariable_shouldRemoveAndSetOnSdkClient(
218218
+ " .connectionTimeout(Duration.ofMillis(3000))\n"
219219
+ " .maxConnections(2000))\n"
220220
+ " .overrideConfiguration(clientConfiguration)\n"
221-
+ " .build();\n"
221+
+ " .build();\n"
222222
+ " }\n"
223223
+ "}"
224224
)
@@ -273,7 +273,7 @@ void configurationWithHttpSettings_usedByMultipleSdkClients_shouldRemoveAndSetOn
273273
+ " .connectionTimeout(Duration.ofMillis(3000))\n"
274274
+ " .maxConnections(2000))\n"
275275
+ " .overrideConfiguration(CONFIGURATION)\n"
276-
+ " .build();\n"
276+
+ " .build();\n"
277277
+ "\n"
278278
+ " public void test() {\n"
279279
+ " SqsClient sqs = SqsClient.builder()\n"
@@ -285,7 +285,7 @@ void configurationWithHttpSettings_usedByMultipleSdkClients_shouldRemoveAndSetOn
285285
+ " .connectionTimeout(Duration.ofMillis(3000))\n"
286286
+ " .maxConnections(2000))\n"
287287
+ " .overrideConfiguration(CONFIGURATION)\n"
288-
+ " .build();\n"
288+
+ " .build();\n"
289289
+ " }\n"
290290
+ "}"
291291
)
@@ -371,7 +371,7 @@ void configurationWithHttpSettings_methodReference_shouldRemoveAndSetOnSdkClient
371371
+ " .tcpKeepAlive(true)\n"
372372
+ " .maxConnections(1000))\n"
373373
+ " .overrideConfiguration(configuration())\n"
374-
+ " .build();\n"
374+
+ " .build();\n"
375375
+ " }\n"
376376
+ "}"
377377
)
@@ -425,7 +425,7 @@ void configurationWithHttpSettings_asyncSdkClient_shouldRemoveAndSetOnSdkClient(
425425
+ " .connectionTimeToLive(Duration.ofMillis(1000))\n"
426426
+ " .connectionTimeout(Duration.ofMillis(1000)))\n"
427427
+ " .overrideConfiguration(clientConfiguration)\n"
428-
+ " .build();\n"
428+
+ " .build();\n"
429429
+ "\n"
430430
+ "\n"
431431
+ " }\n"
@@ -482,13 +482,13 @@ void multipleSdkClients_shouldSetHttpClientCorrectly() {
482482
+ " .socketTimeout(Duration.ofMillis(1000))\n"
483483
+ " .maxConnections(100))\n"
484484
+ " .overrideConfiguration(clientConfiguration1)\n"
485-
+ " .build();\n"
485+
+ " .build();\n"
486486
+ "\n"
487487
+ " SqsClient sqs2 = SqsClient.builder()\n"
488488
+ " .httpClientBuilder(ApacheHttpClient.builder()\n"
489489
+ " .connectionTimeout(Duration.ofMillis(2000)))\n"
490490
+ " .overrideConfiguration(clientConfiguration2)\n"
491-
+ " .build();\n"
491+
+ " .build();\n"
492492
+ " }\n"
493493
+ "}"
494494
)

0 commit comments

Comments
 (0)