Skip to content
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

Migration Tool: PutObjectRequest File/InputStream transform #5942

Merged
merged 4 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package foo.bar;

import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
Expand All @@ -25,9 +26,13 @@
import software.amazon.awssdk.transfer.s3.model.DownloadFileRequest;
import software.amazon.awssdk.transfer.s3.model.FileDownload;
import software.amazon.awssdk.transfer.s3.model.UploadFileRequest;
import software.amazon.awssdk.transfer.s3.model.UploadRequest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.time.Duration;
import java.util.concurrent.Executors;

public class TransferManagerS3 {

Expand Down Expand Up @@ -56,6 +61,20 @@ void download(S3TransferManager tm, String bucket, String key) {

void upload(S3TransferManager tm, String bucket, String key) {
tm.uploadFile(UploadFileRequest.builder().putObjectRequest(PutObjectRequest.builder().bucket(bucket).key(key).build()).source(file).build());

File file = new File("file1.txt");
PutObjectRequest requestWithFile = PutObjectRequest.builder().bucket(bucket).key(key)
.build();
tm.uploadFile(UploadFileRequest.builder().putObjectRequest(requestWithFile).source(file).build());

InputStream inputStream = new ByteArrayInputStream(("HelloWorld").getBytes());
PutObjectRequest requestWithInputStream = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location")
.build();
tm.upload(UploadRequest.builder().putObjectRequest(requestWithInputStream).requestBody(AsyncRequestBody.fromInputStream(inputStream, -1L, Executors.newSingleThreadExecutor())).build());

PutObjectRequest requestWithoutPayload = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location")
.build();
tm.upload(UploadRequest.builder().putObjectRequest(requestWithoutPayload).requestBody(AsyncRequestBody.empty()).build());
}

void copy(S3TransferManager tm, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.Copy;
import com.amazonaws.services.s3.transfer.Download;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

public class TransferManagerS3 {

Expand All @@ -48,6 +51,18 @@ void download(TransferManager tm, String bucket, String key) {

void upload(TransferManager tm, String bucket, String key) {
tm.upload(bucket, key, file);

File file = new File("file1.txt");
PutObjectRequest requestWithFile = new PutObjectRequest(bucket, key, file);
tm.upload(requestWithFile);

InputStream inputStream = new ByteArrayInputStream(("HelloWorld").getBytes());
PutObjectRequest requestWithInputStream = new PutObjectRequest(bucket, key, "location");
requestWithInputStream.setInputStream(inputStream);
tm.upload(requestWithInputStream);

PutObjectRequest requestWithoutPayload = new PutObjectRequest(bucket, key, "location");
tm.upload(requestWithoutPayload);
}

void copy(TransferManager tm, String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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 foo.bar;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.RequestPayer;

public class S3_Streaming {

S3Client s3 = S3Client.create();

void getObject(String bucket, String key) throws Exception {
ResponseInputStream<GetObjectResponse> s3Object = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key)
.build());
s3Object.close();
}

void putObject_bucketKeyContent(String bucket, String key, String content) {
s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key)
.build(), RequestBody.fromString(content));
}

void putObject_bucketKeyFile(String bucket, String key, File file) {
s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key)
.build(), RequestBody.fromFile(file));
}

/**
* Mixed ordering to ensure the files are assigned correctly
*/
void putObject_requestPojoWithFile(String bucket, String key) {
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
File file3 = new File("file3.txt");
File file4 = new File("file4.txt");

PutObjectRequest request1 = PutObjectRequest.builder().bucket(bucket).key(key)
.build();

PutObjectRequest request2 = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location")
.build();

s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location")
.build(), RequestBody.fromFile(file3));
s3.putObject(request2, RequestBody.fromFile(file2));
s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location")
.build(), RequestBody.fromFile(file4));
s3.putObject(request1, RequestBody.fromFile(file1));
}

void putObject_requestPojoWithInputStream(String bucket, String key) {
InputStream inputStream1 = new ByteArrayInputStream(("HelloWorld").getBytes());
InputStream inputStream2 = new ByteArrayInputStream(("HolaWorld").getBytes());

PutObjectRequest request1 = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location")
.build();
s3.putObject(request1, RequestBody.fromContentProvider(() -> inputStream1, "binary/octet-stream"));

s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location")
.build(), RequestBody.fromContentProvider(() -> inputStream2, "binary/octet-stream"));
}

void putObject_requestPojoWithoutPayload(String bucket, String key) {
PutObjectRequest request = PutObjectRequest.builder().bucket(bucket).key(key).websiteRedirectLocation("location")
.build();
s3.putObject(request, RequestBody.empty());
}


void putObjectSetters() {
PutObjectRequest putObjectRequest =
PutObjectRequest.builder().bucket("bucket").key("key").websiteRedirectLocation("location")
.bucket("bucketName")
.acl(ObjectCannedACL.AWS_EXEC_READ)
.build();
}

void putObjectRequesterPaysSetter() {
PutObjectRequest requestWithTrue = PutObjectRequest.builder().bucket("bucket").key("key").websiteRedirectLocation("location").requestPayer(RequestPayer.REQUESTER)
.build();

PutObjectRequest requestWithFalse =PutObjectRequest.builder().bucket("bucket").key("key").websiteRedirectLocation("location")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 foo.bar;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

public class S3_Streaming {

AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();

void getObject(String bucket, String key) throws Exception {
S3Object s3Object = s3.getObject(bucket, key);
s3Object.getObjectContent().close();
}

void putObject_bucketKeyContent(String bucket, String key, String content) {
s3.putObject(bucket, key, content);
}

void putObject_bucketKeyFile(String bucket, String key, File file) {
s3.putObject(bucket, key, file);
}

/**
* Mixed ordering to ensure the files are assigned correctly
*/
void putObject_requestPojoWithFile(String bucket, String key) {
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
File file3 = new File("file3.txt");
File file4 = new File("file4.txt");

PutObjectRequest request1 = new PutObjectRequest(bucket, key, file1);

PutObjectRequest request2 = new PutObjectRequest(bucket, key, "location");
request2.setFile(file2);

s3.putObject(new PutObjectRequest(bucket, key, "location").withFile(file3));
s3.putObject(request2);
s3.putObject(new PutObjectRequest(bucket, key, "location").withFile(file4));
s3.putObject(request1);
}

void putObject_requestPojoWithInputStream(String bucket, String key) {
InputStream inputStream1 = new ByteArrayInputStream(("HelloWorld").getBytes());
InputStream inputStream2 = new ByteArrayInputStream(("HolaWorld").getBytes());

PutObjectRequest request1 = new PutObjectRequest(bucket, key, "location");
request1.setInputStream(inputStream1);
s3.putObject(request1);

s3.putObject(new PutObjectRequest(bucket, key, "location").withInputStream(inputStream2));
}

void putObject_requestPojoWithoutPayload(String bucket, String key) {
PutObjectRequest request = new PutObjectRequest(bucket, key, "location");
s3.putObject(request);
}


void putObjectSetters() {
PutObjectRequest putObjectRequest =
new PutObjectRequest("bucket", "key", "location")
.withBucketName("bucketName")
.withCannedAcl(CannedAccessControlList.AwsExecRead);
}

void putObjectRequesterPaysSetter() {
PutObjectRequest requestWithTrue = new PutObjectRequest("bucket", "key", "location").withRequesterPays(true);

PutObjectRequest requestWithFalse = new PutObjectRequest("bucket", "key", "location").withRequesterPays(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.awssdk.v2migration;

import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Collections;
Expand Down Expand Up @@ -78,7 +80,7 @@ private static class Visitor extends JavaVisitor<ExecutionContext> {
private final List<String> fluentNames;

Visitor(String clzz, List<JavaType> parameterTypes, List<String> fluentNames) {
this.clzz = TypeUtils.asFullyQualified(JavaType.buildType(clzz));
this.clzz = fullyQualified(clzz);
this.parameterTypes = parameterTypes;
this.fluentNames = fluentNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.awssdk.v2migration;

import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -345,10 +347,8 @@ private JContainer<Expression> httpClientBuilderInvoke(Map<String, Expression> h
Class httpClientClassName = httpClientClassNamePair.left();
Class httpClientBuilderClassName = httpClientClassNamePair.right();

JavaType.FullyQualified httpClientType =
TypeUtils.asFullyQualified(JavaType.buildType(httpClientClassName.getCanonicalName()));
JavaType.FullyQualified httpClientBuilderType =
TypeUtils.asFullyQualified(JavaType.buildType(httpClientBuilderClassName.getCanonicalName()));
JavaType.FullyQualified httpClientType = fullyQualified(httpClientClassName.getCanonicalName());
JavaType.FullyQualified httpClientBuilderType = fullyQualified(httpClientBuilderClassName.getCanonicalName());

JavaType.Method httpClientBuilderMethodType = new JavaType.Method(
null,
Expand Down
Loading
Loading