AWS S3 Processing With JetS3t

DZone 's Guide to

AWS S3 Processing With JetS3t

The JetS3t library, known as Jet Set, is a nifty Java toolkit for S3, CloudFront, and Google Storage. Let's bring our Java to bear to manage our objects.

· Cloud Zone ·
Free Resource

In this post, we will see how we can use the Amazon AWS S3 to store objects. We will be using the JetS3t library to process our objects. The JetS3t library, also known as Jet Set, is widely used for processing the AWS S3 i.e. Amazon Web Services, Simple Storage Service.

Before working on AWS S3, you need to have a valid account on AWS with an S3 key and an S3 password. You can register here free of cost. You can also read Amazon Web Services in Action (MANNING) also for more details about AWS.

Here we will be using four APIs:

  1. Upload a document

  2. Download a document

  3. Listing of documents

  4. Delete a document

We will upload images of the user on S3 and then process them as specified above.


To upload an image to S3, we will be using the putObject()  method of the S3Service class.

public S3Object uploadImage(MultipartFile mpf, String albumId, String userId) throws S3ServiceException, IOException {
    InputStream stream = mpf.getInputStream();
    if (s3Bucket == null) {
        s3Bucket = s3Service.getBucket(S3_BUCKET);

    // replacing space with underscore so that file name will not create problem in Linux while downloading
    String fileName = mpf.getOriginalFilename().replaceAll(" ", "_");
    String filePath = userId + "/" + albumId + "/" + fileName;

    S3Object s3Object = new S3Object(filePath);
    S3Object s3Obj = s3Service.putObject(s3Bucket, s3Object);
    return s3Obj;

At line 4, we get the s3bucket object for our processing.

Lines 11-15 are the main code that will upload the image to the s3 bucket.

We have created a path at line 9, which is passed on line 11. After that, we had passed the input stream of the file to the s3Object .

At line 15, we uploaded the image to s3.


To download images, we will be using the getObject() method of the S3Service class.

public InputStream downloadImage(String albumId, String userId, String imageName) throws ServiceException {
    String file = userId + "/" + albumId + "/" + imageName;
    S3Object objectComplete = s3Service.getObject(S3_BUCKET, file);
    return objectComplete.getDataInputStream();

Here, we simply download the image from the s3. First, we will create the path we need to download the image from, and then we get the object from there. See line 3. After that, we return the Input Stream of the image.


To list images, we will be using  listObjects()  method of S3Service  class.

public Map < String, Object > listImages(String albumId, String userId)
throws S3ServiceException {
    Map < String, Object > finalMap = new LinkedHashMap < > ();
    List < Map < String, String >> lstDocs = new LinkedList < > ();
    long count;

    String prefix = userId + "/" + albumId + "/";
    S3Object[] filteredObjects = s3Service.listObjects(S3_BUCKET, prefix, null);

    count = Arrays.stream(filteredObjects).filter(s3 - > s3.getContentLength() > 1).count();
    Arrays.stream(filteredObjects).filter(s3 - > s3.getContentLength() > 1).forEach(s3 - > {
        Map < String,
        String > map = new HashMap < > ();
        String key = s3.getKey();
        map.put("name", key.substring(key.lastIndexOf("/") + 1, key.length()));
    finalMap.put("totalCount", Long.toString(count));
    finalMap.put("items", lstDocs.stream().sorted((m1, m2) - > m2.get(DATETIME).compareTo(m1.get(DATETIME)))
    return finalMap;

Here, we're listing all our images via a particular path. The main focus in the above method is line 8. We pass the bucket name with the path. Everything else done in this method is to pack and format the output.


To delete images, we will be using the deleteObjects() method of the S3Service class. We can also delete multiple images using  thedeleteMultipleObjects()  method.

public void deleteImage(String albumId, String userId, String imageName)
throws ServiceException {
    imageName = imageName != null && !imageName.isEmpty() ? imageName : "";
    String file = userId + "/" + albumId + "/" + imageName;
    s3Service.deleteObject(S3_BUCKET, file);

As the name implies, it will delete the image from s3. Here, we are creating the full path of the image and then passing it to deleteObject() of the s3Service.

A full example is available on the GitHub. Simply download and explore it.


aws s3 ,cloud ,jets3t ,object storage ,tutorial

Published at DZone with permission of Vinod Kumar Kashyap . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}