S3 Upload: the simplest possible in Java

Recently I’ve been in QConSP 2013, where I met many friends and talked a lot. Talking to Milfont, he told me he thought nobody was doing regular file uploads anymore, since S3 is very convenient and easy to use. In his Ruby projects he uses Paperclip, which allows S3 uploads with simple configurations.

However, in our Amazon projects we see that most applications aren’t using S3 yet, and this got me motivated to write some posts showing the simplest possible S3 uploads in several languages, frameworks and products. This one is the first in the series, and here we’ll show a regular file upload and the equivalent one using S3. For TL;DR version, simply clone the project in github and try it yourselves.

In WebContent folder there are 2 simple htmls, both with forms with a single element: a file upload. disco.html file points to Servlet /UploadDisco, which manipulates input stream the old fashioned way (with o commons-fileupload) e saves to disk, just like below.

In order to use S3, we need to download Java SDK and add it to our dependencies, or useMaven instead. S3 features require some Apache Commons libs.

Besides having Amazon SDK in classpath, we need to add a file AwsCredentials.properties, with your accessKey e secretKey. Both can be found in Security Credentials option, at AWS console, just like below.

The last thing we need is to have an S3 bucket available for the upload. We cloud create a new bucket through the API as well, but the easiest and most common choice is to use an existing bucket.

We have below the Servlet receiving the file upload and sending it straight to S3, without local file system operations.

In this upload, we send the file to a bucket called rivendel-upload-s3, and the define its access policity as Public Read. This way, everyone can download the file from S3 without authentication. With the name of the region and the name of the bucket, it’s possible to create the file access URL, in this format: https://s3-region-name.amazonaws.com/bucket-name/file-name.

Soon we’ll publish similar posts with examples in other languages. I hope this will prove useful to many people :). See ya next!

  • Elson Costa

    Ola amigo, será que você poderia me ajudar, clonei seu projeto e estou tentando fazer o uplouder der uma arquivo mais ele sempre retorna um errro (AmazonS3Exception: Status Code: 301, AWS Service: Amazon S3, AWS Request ID: CA6E8FFAE05AF174, AWS Error Code: PermanentRedirect, AWS Error Message: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint., S3 Extended Request ID: gOR1rJve2EKJzZ8dLoLecgLz6pPzEH23cZ/bUWNgzfxhEaCrRrmbwZR8kde3C9+H).

    Qual será o problema.

    • Antonio Carlos da Graça Mota D

      Parece que você não mudou as credencias do Bucket para o o seu.

      Imagino que ele esteja usando as credenciais de teste, usadas para criar o texto.