Uploading file into S3 from Spring Boot microservice

There is an ongoing and never ending journey we had started long time ago: how to build a microservice with Spring Boot and how to run it in Elastic Beanstalk. All the articles that have been published till now are available here.

Today we’ll continue this series with another subject: how to upload file into S3 sent via an API, as multipart. This topic is a subject that arose as a result of your messages.

The full implementation of this episode is available here. We updated the README file to explain how to run it, but in order to do that, you have to do 2 things:

  • Create an S3 bucket and put the file name in the configuration file (resources/properties/config-<>.properties)
  • Create an IAM sub-account that has permissions to write in that S3 bucket.

Run the service as it’s described in the README file and on localhost:8080, you’ll get a section like this one:

Select a file from your disk, press Try it out, and then check the logs for an entry like:

2018-31-13 19:55:26.471  INFO 86574 --- [nio-8080-exec-1] c.c.d.datastore.FileDataStoreWithAWSS3   : File foo.jpeg added

Of course, there are many limitation that could be added, like file mime type or so on, but this example is just to demo how to use MultipartFile from Spring.

If you want to run this code in Beanstalk, maybe you wonder how you are going to set the system properties with credentials. But do you remember that we have a post on that? See here for more details.

A good practice we are trying to promote without pretending we invented the wheel: any configuration that is not sensitive (bucket name) can be put in a config file, but anyone that could be stolen, need to sent as environment variables, even it’s not the best option. The flaw here is that if someone reaches to ssh on you host, it can get your credentials.

For any question you might have, we are here and hopefully we can help you!

Happy cloud computing!