How to setup AWS SDK for Java for Amazon S3 Development
- Details
- Written by Nam Ha Minh
- Last Updated on 03 January 2022   |   Print Email
In this post, I will guide you how to setup a Java Maven project to use Amazon Web Services (AWS) Software Development Kit (SDK) for Amazon S3 (Simple Storage Service) development.
To follow this guide, you must have an AWS account and an IAM admin user. Here are the steps for installing AWS Java SDK for your project:
- Generate AWS programmatic credentials, i.e. Access Key ID and Secret Access Key
- Store AWS configuration via system environment variable, Java system properties or config file
- Declare dependency for AWS Java SDK and Amazon S3
Let’s go through each step in details.
1. Generate AWS Access Key ID and Secret Access Key
Follow this guide to create AWS access keys. Then copy the values of Access key ID and Secret access key, which will be used in the next step.
2. Store AWS Configuration
There are 3 ways to store AWS configuration (Region, Access key ID and Secret Access Key) on development computer: in user/system properties, in config file or in Java system properties. And my preferred way is using system properties, so I will show you how.
On Windows, open the Environment Variables dialog, and add 3 new system variables as shown below:
Note that the name of 3 variables are AWS_REGION, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. You can get the AWS region from S3 service in AWS management console.
3. Declare Dependency for AWS Java SDK and Amazon S3
Now, in the pom.xml file of your Java Maven project, put the following XML code:
<project ...> <dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>bom</artifactId> <version>2.15.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> </dependency> </dependencies> </project>
The dependencyManagement section imports a Bill of Materials (bom) for AWS SDK for Java. Then the dependency section specifies the dependency needed for Amazon S3 development only.
That’s done. Now you’re all set for writing code that uses Amazon S3 API from AWS Java SDK.
To see the steps in action, I recommend you to watch this video:
Related Tutorials:
- How to Generate AWS Access Key ID and Secret Access Key
- AWS Java SDK S3 List Buckets Example
- AWS Java SDK S3 List Objects Examples
- AWS Java SDK S3 Create Bucket Examples
- AWS Java SDK S3 Create Folder Examples
- Upload File to S3 using AWS Jav SDK - Java Console Program
- Upload File to S3 using AWS Java SDK - Java Servlet JSP Web App
- Spring Boot File Upload to Amazon S3 Example
- AWS Java SDK Download File from S3 Example
- AWS Java SDK S3 Delete Objects Examples
- AWS Java SDK S3 Delete Buckets Examples