Methods to Efficiently Upload Files to Amazon S3 Using Java

Anh Trần Tuấn - Oct 12 - - Dev Community

1. Setting Up AWS SDK for Java

To interact with Amazon S3 in Java, you need to set up the AWS SDK for Java in your project.

1.1 Adding the AWS SDK Dependency

First, you need to add the AWS SDK dependency to your project. If you’re using Maven, include the following in your pom.xml :

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.20.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

1.2 Configuring AWS Credentials

AWS requires credentials to authenticate requests to the S3 service. You can provide credentials using the ~/.aws/credentials file or through environment variables.

Example:

[default]
aws_access_key_id = your_access_key_id
aws_secret_access_key = your_secret_access_key
Enter fullscreen mode Exit fullscreen mode

1.3 Initializing the S3 Client

With the AWS SDK configured, you can initialize an S3 client in your Java application:

import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.regions.Region;

public class S3Uploader {
    private final S3Client s3;

    public S3Uploader() {
        this.s3 = S3Client.builder()
                          .region(Region.US_WEST_2)
                          .build();
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Uploading Files to S3

Once the S3 client is initialized, you can upload files to an S3 bucket.

2.1 Single File Upload

To upload a single file, you can use the putObject method provided by the S3 client. Here’s how you can do it:

import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import java.nio.file.Paths;

public void uploadFile(String bucketName, String keyName, String filePath) {
    PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                                                        .bucket(bucketName)
                                                        .key(keyName)
                                                        .build();
    s3.putObject(putObjectRequest, Paths.get(filePath));
}
Enter fullscreen mode Exit fullscreen mode

Demo Code and Result:

Suppose you have a file test.txt that you want to upload to the my-bucket S3 bucket:

public static void main(String[] args) {
    S3Uploader uploader = new S3Uploader();
    uploader.uploadFile("my-bucket", "test.txt", "/path/to/test.txt");
    System.out.println("File uploaded successfully!");
}
Enter fullscreen mode Exit fullscreen mode

2.2 Multipart Upload for Large Files

For larger files, Amazon S3 provides a multipart upload API. This method allows you to upload large files in smaller, more manageable parts.

import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest;
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest;
import software.amazon.awssdk.services.s3.model.UploadPartRequest;

public void uploadLargeFile(String bucketName, String keyName, String filePath) {
    CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder()
                                                                                           .bucket(bucketName)
                                                                                           .key(keyName)
                                                                                           .build();
    String uploadId = s3.createMultipartUpload(createMultipartUploadRequest).uploadId();

    // Divide the file into parts and upload each part
    // After all parts are uploaded, complete the multipart upload
    CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder()
                                                                                                  .bucket(bucketName)
                                                                                                  .key(keyName)
                                                                                                  .uploadId(uploadId)
                                                                                                  .build();
    s3.completeMultipartUpload(completeMultipartUploadRequest);
}
Enter fullscreen mode Exit fullscreen mode

Here’s how you might use the multipart upload for a large file:

public static void main(String[] args) {
    S3Uploader uploader = new S3Uploader();
    uploader.uploadLargeFile("my-bucket", "large-file.txt", "/path/to/large-file.txt");
    System.out.println("Large file uploaded successfully!");
}
Enter fullscreen mode Exit fullscreen mode

3. Optimizing File Uploads

In production environments, optimizing file uploads can save both time and costs. Here are a few strategies:

3.1 Asynchronous Uploads

Using asynchronous uploads allows your application to remain responsive while the file upload is in progress.

import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.services.s3.S3AsyncClient;

public void uploadFileAsync(String bucketName, String keyName, String filePath) {
    S3AsyncClient asyncClient = S3AsyncClient.builder().region(Region.US_WEST_2).build();
    asyncClient.putObject(
        PutObjectRequest.builder().bucket(bucketName).key(keyName).build(),
        AsyncRequestBody.fromFile(Paths.get(filePath))
    ).thenRun(() -> System.out.println("Asynchronous upload completed!"));
}
Enter fullscreen mode Exit fullscreen mode

3.2 Utilizing TransferManager

AWS SDK provides a higher-level API called TransferManager for handling file transfers, which can simplify the process, especially for multipart uploads.

import software.amazon.awssdk.transfer.s3.S3TransferManager;
import software.amazon.awssdk.transfer.s3.Upload;

public void uploadUsingTransferManager(String bucketName, String keyName, String filePath) {
    S3TransferManager transferManager = S3TransferManager.create();
    Upload upload = transferManager.uploadFile(b -> b.source(Paths.get(filePath))
                                                     .putObjectRequest(r -> r.bucket(bucketName).key(keyName)));
    upload.completionFuture().join();
    System.out.println("File uploaded using TransferManager!");
}
Enter fullscreen mode Exit fullscreen mode

4. Conclusion

Uploading files to Amazon S3 using Java can be accomplished efficiently by following the methods outlined in this guide. Whether you’re dealing with small files or large multipart uploads, the AWS SDK for Java provides robust tools to ensure your uploads are secure, fast, and reliable.

If you have any questions or need further clarification, feel free to leave a comment below!

Read posts more at : Methods to Efficiently Upload Files to Amazon S3 Using Java

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .