Last updated: March 13, 2025

<span aria-hidden="true" id="import-files-from-amazon-s3-in-java"></span>

# Import files from Amazon S3 in Java

![Kevin van Zonneveld](/assets/images/teammates/avatar-kvz-4.jpg?dpl=dpl_5zDg5SaYi7KaFpi2w3C4HPYbgapj)

#### Kevin van Zonneveld

Co-founder · Amsterdam, The Netherlands · Show bio

[](https://x.com/kvz)[](https://github.com/kvz)

Integrating Amazon S3 with Java applications is a common requirement for developers working with cloud storage. AWS SDK v2 provides a robust and efficient way to handle file importing. In this DevTip, we'll walk through setting up AWS SDK v2, securely configuring credentials, importing files from S3, handling exceptions, and optimizing performance.

<span aria-hidden="true" id="introduction-to-amazon-s3-and-java"></span>

## Introduction to Amazon S3 and Java

Amazon S3 (Simple Storage Service) is a scalable cloud storage solution for storing and retrieving data. Java developers often interact with S3 to import files. AWS SDK v2 offers a streamlined API for these operations.

<span aria-hidden="true" id="setting-up-aws-sdk-v2"></span>

## Setting up AWS SDK v2

Add the AWS SDK v2 dependencies to your project. For Maven, add to `pom.xml`:

```xml
<dependency>
  <groupId>software.amazon.awssdk</groupId>
  <artifactId>s3</artifactId>
  <version>2.30.38</version>
</dependency>

```

For Gradle, add to `build.gradle`:

```groovy
implementation 'software.amazon.awssdk:s3:2.30.38'

```

<span aria-hidden="true" id="configuring-aws-credentials"></span>

## Configuring AWS credentials

AWS SDK v2 supports multiple ways to configure credentials. The recommended approach is using environment variables or AWS credentials files.

Using environment variables:

```bash
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key

```

Or, store credentials in `~/.aws/credentials`:

```ini
[default]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key

```

<span aria-hidden="true" id="configuring-the-aws-region"></span>

## Configuring the AWS region

Specifying the correct AWS region is crucial. Here's how to configure it:

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

// Configure the S3 client with a specific region
Region region = Region.US_EAST_1; // Choose your region
S3Client s3 = S3Client.builder()
    .region(region)
    .build();

```

<span aria-hidden="true" id="importing-files-from-s3"></span>

## Importing files from S3

Here's an example of importing a file from Amazon S3:

```java
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;

import java.nio.file.Paths;

public class S3FileImporter {

  public static void main(String[] args) {
    String bucketName = "your-bucket-name";
    String key = "path/to/your/file.txt";
    String downloadPath = "downloaded-file.txt";

    Region region = Region.US_EAST_1; // Choose your region

    try (S3Client s3 = S3Client.builder()
        .region(region)
        .build()) {

      GetObjectRequest request = GetObjectRequest.builder()
          .bucket(bucketName)
          .key(key)
          .build();

      s3.getObject(request, ResponseTransformer.toFile(Paths.get(downloadPath)));
      System.out.println("File downloaded successfully to " + downloadPath);
    } catch (S3Exception e) {
      System.err.println("S3 error: " + e.awsErrorDetails().errorMessage());
    } catch (Exception e) {
      System.err.println("Unexpected error: " + e.getMessage());
    }
  }
}

```

<span aria-hidden="true" id="handling-exceptions"></span>

## Handling exceptions

When working with S3, handle specific exceptions:

```java
try {
  s3.getObject(request, ResponseTransformer.toFile(Paths.get(downloadPath)));
  System.out.println("File downloaded successfully.");
} catch (software.amazon.awssdk.services.s3.model.NoSuchKeyException e) {
  System.err.println("File not found: " + e.awsErrorDetails().errorMessage());
} catch (software.amazon.awssdk.services.s3.model.NoSuchBucketException e) {
  System.err.println("Bucket not found: " + e.awsErrorDetails().errorMessage());
} catch (software.amazon.awssdk.services.s3.model.S3Exception e) {
  System.err.println("S3 error: " + e.awsErrorDetails().errorMessage());
} catch (Exception e) {
  System.err.println("Unexpected error: " + e.getMessage());
}

```

<span aria-hidden="true" id="s3-client-types"></span>

## S3 client types

AWS SDK for Java v2 offers different client types:

<span aria-hidden="true" id="synchronous-client-s3client"></span>

### Synchronous client (S3Client)

The standard client for most applications. Operations block until completed.

<span aria-hidden="true" id="asynchronous-client-s3asyncclient"></span>

### Asynchronous client (S3AsyncClient)

Ideal for non-blocking operations, returning `CompletableFuture` objects:

```java
import software.amazon.awssdk.services.s3.S3AsyncClient;
import java.util.concurrent.CompletableFuture;

S3AsyncClient asyncClient = S3AsyncClient.create();
CompletableFuture<GetObjectResponse> futureResponse =
    asyncClient.getObject(request, AsyncResponseTransformer.toFile(Paths.get(downloadPath)));

futureResponse.whenComplete((response, error) -> {
    if (error != null) {
        System.err.println("Error: " + error.getMessage());
    } else {
        System.out.println("File downloaded successfully.");
    }
});

```

<span aria-hidden="true" id="enhanced-performance-client-s3crtasyncclient"></span>

### Enhanced performance client (S3CrtAsyncClient)

For high-throughput, using the AWS Common Runtime (CRT):

```java
import software.amazon.awssdk.services.s3.S3CrtAsyncClient;

S3CrtAsyncClient crtClient = S3CrtAsyncClient.builder()
    .region(Region.US_EAST_1)
    .build();

```

<span aria-hidden="true" id="optimizing-performance"></span>

## Optimizing performance

For large files, consider these techniques:

<span aria-hidden="true" id="streaming-to-disk"></span>

### Streaming to disk

```java
s3.getObject(request, ResponseTransformer.toFile(Paths.get("large-file.txt")));

```

This streams the file to disk, minimizing memory usage.

<span aria-hidden="true" id="multipart-downloads"></span>

### Multipart downloads

For files larger than a few hundred MB, use multipart downloads:

```java
import software.amazon.awssdk.transfer.s3.S3TransferManager;
import software.amazon.awssdk.transfer.s3.model.DownloadFileRequest;

S3TransferManager transferManager = S3TransferManager.builder()
    .s3Client(s3AsyncClient)
    .build();

DownloadFileRequest downloadFileRequest = DownloadFileRequest.builder()
    .getObjectRequest(request)
    .destination(Paths.get("very-large-file.mp4"))
    .build();

transferManager.downloadFile(downloadFileRequest)
    .completionFuture()
    .join();

```

<span aria-hidden="true" id="iam-permissions"></span>

## Iam permissions

Ensure your IAM user or role has the minimum required permissions:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:ListBucket"],
      "Resource": ["arn:aws:s3:::your-bucket-name/*", "arn:aws:s3:::your-bucket-name"]
    }
  ]
}

```

<span aria-hidden="true" id="best-practices"></span>

## Best practices

* Secure your AWS credentials.
* Use IAM roles with minimal permissions.
* Stream large files to disk.
* Implement robust error handling.
* Choose the appropriate S3 client type.
* Use the S3TransferManager for large files.
* Set appropriate timeouts.

Transloadit offers a managed solution for importing files, including from S3. Check out our[S3 Import Robot](/docs/robots/s3-import.md) and [Java SDK](/docs/sdks/java-sdk.md).

\#java#aws-sdk#amazon-s3#file-importing-service

### 👩‍💻 Join 20k+ developers

Sign up for our [monthly newsletter](/newsletters.md) to receive direct links to 3 exclusive tech — and 2 product updates. No less, no more.

Your email:

Get access

## File uploading and encoding. Made simple.

Transloadit streamlines file handling for developers, trusted by brands like Coursera and The New York Times. We’re known for a reliable API, top-notch support, and a strong commitment to open source, with projects like [Uppy⁠](https://uppy.io) and [Tus⁠](https://tus.io) setting standards in file processing.

[Sign up](/c/)[Book a Demo](https://survey.typeform.com/to/kRg47Xi5)

No credit card needed · 5 GB included in the free plan

Cancel anytime
