Uploading files to AWS S3 By Java SpringBoot

This is a demo of uploading files to AWS S3 using Java SpringBoot.

First, we need to add the dependency of AWS S3:

Then add these configs in application.yml:

The accessKey, secretKey and bucket is nesscessary which can be found in aws’ admin dashboard.

Let’s create a class named AwsS3Componment to execute uploading operation:

Since my service is using endpoint, I add this line in AmazonS3Client config:

Without this config, we will receive exception like this:

The exception message is a little inaccurate. The exception is led by lack of endpoint config, but it remind me of invalid accessKey. It’s really obscure.

Well, next time when we get a exception about AccessKey, we can consider whether it is related to Endpoint.

If you don’t need set config of Endpoint, you must add the Region config. Then the full config is:

Now we have a instance of AmazonS3Client, let’s add the code of uploading files:

The code above uses a File instance to upload files. However, cause of file system permissions, sometimes we need to upload files through InputStream :



Leave a Reply