Content Management in Amazon S3 using JAVA

 

Amazon S3 (Simple Storage Service) is a storage service provided by AWS (Amazon Web Services). We can use S3 to store and retrieve any amount of data on the web world-wide. To know more about this service, you can refer to the official documentation of AWS S3 from here. This tutorial will show you, how to manage your content in S3 using JAVA. The below java program demonstrates how to make basic requests to Amazon S3 using the AWS SDK for Java.

To continue with this tutorial, you must have AWS secret access key and access key id. These keys will be used to make a connection with AWS in your code. Keys can be generated for AWS users.

Before moving further to this tutorial, we need to download AWS Java SDK. You can download it from here. This zip file itself included all the required JARs.

Firstly create a S3Tutorial class with a main method.

Amazon S3 client >> Make an Amazon S3 client with your AWS secret access keyaccess key id and your S3 bucket’s region.

Bucket name >> Choose a bucket name which must be unique globally on S3. A bucket is simply a container to which you are going to interact with.You can optionally specify a location (region) for your bucket if you want to keep your data closer to your applications or users.

Creating bucket >> Now create a bucket in your Amazon S3 storage.

Listing buckets >> List all the buckets available in your S3 account.

Key >> Choose a key for the object which you are uploading. Your file will be saved on S3 with this name.

Uploading object >> Upload an object (in this tutorial, it’s an image) to your bucket. You can easily upload a file to S3. You can also specify your own metadata when uploading to S3, which allows you to set a variety of options like content-type and content-encoding, plus additional metadata specific to your applications.

Get your uploaded object >> When you download an object, you get all of the object’s metadata and a stream from which to read the contents. It’s important to read the contents of the stream as quickly as possibly since the data is streamed directly from Amazon S3 and your network connection will remain open until you read all the data or close the input stream.

‘GetObjectRequest’ also supports several other options, including conditional downloading of objects based on modification times, ETags and selectively downloading a range of an object.

List all the objects in your bucket by prefix >> There are many options for listing the objects in your bucket. Keep in mind that buckets with many objects might truncate their results when listing their objects, so be sure to check if the returned object listing is truncated, and use the ‘AmazonS3.listNextBatchOfObjects(…)’ operation to retrieve additional results.

Delete an object >> Unless versioning has been turned on for your bucket, there is no way to undelete an object, so use caution when deleting objects.

Delete a bucket >> A bucket must be completely empty before it can be deleted, so remember to delete all the objects from your buckets before you try to delete them.

So, these were the basic operations of Amazon S3 using AWS Java SDK. Enjoy reading !! 🙂

Leave a Reply