Quick revision
The short answer
We create one S3Client, for example S3Client.builder().region(Region.EU_WEST_1).build(), and one S3Presigner as singleton beans, because clients are thread-safe and hold connection pools. Uploads use putObject with a PutObjectRequest and RequestBody.fromFile(path), downloads use getObject with ResponseTransformer.toFile(target) or a ResponseInputStream that we must close, and a presigned URL comes from presignGetObject with a signatureDuration of 10 minutes. Credentials come from the DefaultCredentialsProvider chain and the region from the default region provider chain unless we set it explicitly; new code uses SDK 2.x, since 1.x has reached end of support.
What to remember
In this answer, we saw how a Spring Boot service uses the AWS SDK for Java 2.x with S3. We create one thread-safe S3Client and one S3Presigner as singleton beans, upload with putObject and RequestBody.fromFile, download with ResponseTransformer.toFile or a ResponseInputStream closed in try-with-resources, and create a 10-minute presigned URL with presignGetObject. Credentials come from the DefaultCredentialsProvider chain and the region from the default region chain unless set explicitly, and new code uses 2.x because 1.x has reached end of support.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We create one S3Client, for example S3Client.builder().region(Region.EU_WEST_1).build(), and one S3Presigner as singleton beans, because clients are thread-safe and hold connection pools. Uploads use putObject with a PutObjectRequest and RequestBody.fromFile(path), downloads use getObject with ResponseTransformer.toFile(target) or a ResponseInputStream that we must close, and a presigned URL comes from presignGetObject with a signatureDuration of 10 minutes. Credentials come from the DefaultCredentialsProvider chain and the region from the default region provider chain unless we set it explicitly; new code uses SDK 2.x, since 1.x has reached end of support.
Why this question is asked
Storing files in S3 is one of the most common things a Spring Boot service does on AWS, and small mistakes here cause real problems: a client built per request exhausts sockets, an unclosed stream leaks connections, and hard-coded keys end up in git. Interviewers ask this to see whether we know the SDK's main classes and, more importantly, how it should be wired and where it gets its credentials. In this answer, we will create and share the client, upload and download a file, create a presigned URL, see how credentials and the region are found, and note the move from SDK 1.x to 2.x.
1Creating and Sharing the Client
The AWS SDK for Java 2.x, in the software.amazon.awssdk packages, gives each service a client class. S3Client is the synchronous S3 client; S3AsyncClient is the non-blocking one. Building a client is expensive: it creates an HTTP client with a connection pool, loads configuration and resolves credentials. Once built, it is thread-safe. So the right pattern is one client per application, shared by all threads, created as a Spring bean and closed when the application stops.
Notice that there are no access keys in the code. Leaving credentials to the default provider chain is what lets the same code run on a laptop, in CI and in production with different credentials and no secrets in the repository.
2Uploading and Downloading a File
Each S3 operation takes a request object built with a builder, naming the bucket and the key, the object's name within the bucket. For uploads, a RequestBody supplies the content; RequestBody.fromFile streams from disk instead of loading the file into memory.
The streaming form of getObject returns a ResponseInputStream that holds an HTTP connection until it is fully read or closed. That is why it sits in a try-with-resources block: forgetting to close it leaks a connection from the client's pool, and after enough leaks every S3 call times out waiting for a connection.
3Creating a Presigned URL
A presigned URL lets someone without AWS credentials, such as a browser, download one object for a limited time. The service signs the request with its own credentials, and S3 checks the signature and the expiry when the URL is used. Signing happens locally; there is no call to S3.
5 messages in order. Play them, or step through them one at a time.
The file never passes through the service, and S3 enforces the expiry.
But there is a catch: if the service signs with temporary credentials, as it does with an IAM role, the URL also stops working when those credentials expire, even if that is before the 10 minutes are up. With role credentials that are refreshed regularly this rarely matters for 10 minutes, but it explains URLs that die early. The S3Presigner is also a closeable resource, so it is a singleton bean too.
4How the SDK Finds Credentials and the Region
When we do not pass credentials, the client uses DefaultCredentialsProvider, which tries a chain of sources in order and uses the first that works. It checks Java system properties, then environment variables such as AWS_ACCESS_KEY_ID, then a web identity token, which is how pods on EKS get role credentials through IAM Roles for Service Accounts, then the shared credentials and config files in ~/.aws with the active profile, then container credentials on ECS, and finally the EC2 instance metadata service. The exact order can change between SDK versions, so check your version's documentation.
The region works the same way through DefaultAwsRegionProviderChain: the aws.region system property, the AWS_REGION environment variable, the profile's config file, and the instance metadata. Setting the region explicitly in the builder, as we did, skips the chain and makes the behaviour obvious; leaving it out lets the same image run in several regions.
- Java system properties
aws.accessKeyId,aws.secretAccessKey - Environment variables
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY - Web identity tokenEKS IAM Roles for Service Accounts
- Profile files~/.aws/credentials and ~/.aws/config
- Container credentialsECS task role endpoint
- Instance metadataEC2 instance profile
6 layers, from the top down. Step through them one at a time.
The first source that returns credentials wins; check your SDK version for the exact order.
5SDK 1.x Versus 2.x
The older AWS SDK for Java 1.x, in the com.amazonaws packages, used AmazonS3ClientBuilder and classes such as S3Object. AWS announced its end of support, and it reached end of support at the end of 2025, so it no longer receives fixes; check AWS announcements for the exact dates. New code should use 2.x, which has immutable request objects with builders, pluggable HTTP clients, non-blocking async clients, and better startup. Both versions can live in one application during a migration because their packages differ, which lets teams move one call site at a time.
6Follow-up questions
What does Spring Cloud AWS add on top of the SDK?
Spring Cloud AWS auto-configures the SDK clients as beans, including S3Client, from spring.cloud.aws.* properties for region and credentials, so we do not write the configuration class ourselves. It adds S3Template, a higher-level helper for uploading and downloading objects and creating signed URLs, and it lets us use S3 objects as Spring Resources with s3:// locations. Beyond S3, it integrates SQS listeners, SNS, and loading configuration from Parameter Store and Secrets Manager through spring.config.import. Underneath, it still uses the SDK 2.x clients, so everything about sharing clients and closing streams still applies. Check its compatibility table for your Spring Boot version.
Why not build a new S3Client in each request?
Each client creates its own HTTP connection pool and threads and resolves credentials, so building one per request is slow, wastes sockets and memory, and can exhaust file descriptors under load. Unless each client is also closed, it leaks those resources. A single shared client reuses connections across requests, which is also much faster because TLS handshakes are reused.
How do you test code that uses S3Client?
For unit tests, we can mock the S3Client interface. For integration tests, Testcontainers with LocalStack or another S3-compatible server gives a real endpoint, and we point the client at it with endpointOverride and static test credentials, and with path-style access if the server needs it.
7Conclusion
In this answer, we saw how a Spring Boot service uses the AWS SDK for Java 2.x with S3. We create one thread-safe S3Client and one S3Presigner as singleton beans, upload with putObject and RequestBody.fromFile, download with ResponseTransformer.toFile or a ResponseInputStream closed in try-with-resources, and create a 10-minute presigned URL with presignGetObject. Credentials come from the DefaultCredentialsProvider chain and the region from the default region chain unless set explicitly, and new code uses 2.x because 1.x has reached end of support.
Open with
I would create one S3Client as a singleton Spring bean, for example S3Client.builder().region(Region.EU_WEST_1).build(), and one S3Presigner the same way. Clients are thread-safe and hold an HTTP connection pool, so building one per request wastes connections and threads; as beans, Spring also closes them on shutdown.
Then, if they want more
To upload, I call putObject with a PutObjectRequest naming the bucket, key and content type, and RequestBody.fromFile so the file streams from disk. To download, I can use getObject with ResponseTransformer.toFile, or the version that returns a ResponseInputStream, and then I always use try-with-resources, because that stream holds a pooled connection until it is fully read or closed.
For a presigned URL, I build a GetObjectPresignRequest with signatureDuration of ten minutes and the get request, and call presignGetObject. Signing happens locally, and the URL also stops working if the credentials that signed it expire earlier. I do not put credentials in code.
The client uses DefaultCredentialsProvider, which tries system properties, environment variables, a web identity token like EKS service account roles, profile files, ECS container credentials and EC2 instance metadata, and the region comes from the default region chain unless I set it, which I usually do explicitly.
And new code should be on SDK 2.x, the software.amazon.awssdk packages, because 1.x has reached end of support.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 2
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- AWS documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Using the AWS SDK for Java 2.x, show how a Spring Boot service uploads a file to S3...": https://scrivio-interview-library.netlify.app/spring-boot/aws/65da5775f4/