How To Implement And Use Simplest Storage Service- S3 In Java Web Development

04/28/2015 11:54

Aegis Java web development team encountered some queries related to Java S3 – Simplest storage service. They wonder what this service is about and why to use it and to answer their wondering; we are posting this article and explaining the S3 concept and what advantages you can avail while using it. We are also answering via this post to those who queried about the way to use Amazon S3. Read further to know the answers to your queries.

What is S3?

S3 expands to Simple Storage Service. S3 is the basest of all services that serve the purpose of why the cloud invent in the first place. Cloud was invented to separate the content from, the device and storing it in central trusted servers thus enabling the users to access their data from anywhere and everywhere from any device. Taking this base concept very seriously, AWS came up with S3, a tool that allows storing any amount of data on Amazon data servers and enables access to those data from any place, anywhere. S3 is cost-effective, in a way that there is no setup fee or a fixed cost associated with it. You only pay for what you use is the motto of Cloud in general, and it applies to S3 as well. S3 provides a variety of means to store and get data from its servers. It can be either via the UI, or the Command Line Interface or via programming API's. Being Java driven and having also worked on Java S3 API, we shall be discussing the Java approach to S3 in this article.

Why S3?

The answer to this lies in the initial motive behind the origin of the Cloud. Consider the case wherein people are working on their personal computers in some corner of the world. The data on that pc is just limited to that pic. If he wants to migrate the data between different devices or access it from some other part of the world, he has to insert cables into the computer and device and perform a physical transfer, or go over the remote desktop connection to the host pc and access the data or even use emails which is an inefficient method because emails only allow a certain amount of data to transfer. To achieve this motive is to use S3. Apart from solving the issue of personalized data, it also solves the issue of sizes because one can store an unlimited amount of data onto S3.

AWS and S3

Amazon Web Services (AWS) is a Cloud Service, and it is one of the largest Cloud providers amongst all those who are available. AWS comes with a bunch of services, all of them catering to the cloud model, which allows any application to use the powerful advantages that the Cloud model suggests. Starting from sharing of the processing power, to the database, to the security system, to the sharing of computer hardware, it is one-stop-shop for any cloud requiring an application. One of the most famous and widely used services is that of S3: Simple Storage Service.

The operations on S3 can either be performed using the AWS UI, AWS CLI (Command Line Interface), or the AWS SDK. The SDK comes for a variety of languages enabling a wide variety of developers to code the applications performing operations on the service. We shall be discussing the Java approach to the system because the Java team in the company has had a long experience developing the applications using S3 perform data transfer. The pre-requisites for operating in Java would be a JAVA SDK and Maven installed on the machine.

Some of the advantages of using S3 are as follows:

  • Durable – S3 has a durable infrastructure to ensure the survival of the data. The data in S3 replicated across multiple facilities and multiple devices in each facility.    
  • Low Cost – TThe tariff structure of using Amazon S3 is quite low. The principle is simple: Pay for what you use without any upfront costs of the fixed fee.
  • Available – The service is designed to provide over 99.99% availability of objects in a given year. Also, one can choose a specific region to reduce bandwidth costs and latency.
  • Secure – Either it is storing of data or retrieval of it, S3 performs all these transactions over SSL. Also, data is automatically encrypted once it uploaded. Also, bucket permissions can be configured to prevent unauthorized access to the data.
  • Scalable – Stop worrying about the storage of the data and the volume of data. Just keep storing as much data as you want and scale up and down as needed.
    Send Event Notifications – S3 provides a mechanism to send notifications when data uploaded to S3. These events can prove useful to perform some automatic transactions once the data uploaded.
  • High Performance – Multi-part upload is supported by the service, therefore providing a higher performance when a large file upload. One can choose the region physically nearest to its country to reduce latency.
    Integrated – Amazon S3 is integrated with a wide variety of other AWS Services making it easy to build solutions that use a range of other AWS services.
  • Easy to use – The ease of use comes with the fact that S3 can be used either directly from the UI, CLI or the programming SDK's.

How to use Amazon S3?

To use AWS S3, or as a matter of fact, any of AWS Services, one download the AWS SDK to use its features. In the case of Maven usage, the following snippets of code are used to download the SDK. If Maven is not present, one can manually download the aws-java-SDK from the Internet.

The versions can be changed to suit the needs.

Amazon S3 Architecture

Before, proceeding further it would be nice to understand some technical jargon associated with S3.

1. Buckets – Buckets are used to organize the different objects that store in S3. Just like a file system is used to provide a hierarchical structure in an OS, buckets are used to provide a hierarchical structure in S3. Every object that store in S3 is kept inside a bucket. For eg, if an object named 'demo.txt' stored inside a bucket named 'trial', the URL for the object would be https://trial.s3.amazonaws.com/demo.txt

2. Objects – Objects are the base identity that store in S3. An object consists of object data, and metadata of which the data is invisible to S3 and only the metadata is available to the service.

3. Key –  Keys are the actual identity of an object within a bucket. Objects are identified in a bucket by 'bucket + key + version' name.

S3 API:

Following are the most important S3 API calls that are used very frequently and are the ones that we have been using in our projects.

1. CreateBucket: This is the API call that is used to create a bucket in an AWS account in a particular region. 

 

One has to specify the region when creating the bucket. AWS, with all of its services, is distributed in many different regions around the globe. The region is specified as follows:

 

The endpoint can be one of the many values as defined below:

2. PutObjectThis is the API call that is used to put an object in an S3 bucket. 

 

S3Client is a variable that has initialized as an establishment to AWS S3. Here 'temp' is File containing the contents to upload.

3. GetObject: This is the API call used to fetch an object from S3 bucket

 

4. DeleteObject: This is the API call used to delete an object from an S3 bucket

 

5. DoesBucketExist: This is the API call used to check whether an object exists in an S3 bucket or not. 

 

6. DeleteBucket: TThis is the API call used to delete a bucket from S3

 

The catch here is that all the objects of a bucket have to delete before, the bucket itself can delete. Also, only the owner of the bucket can delete it, irrespective of the bucket's access control policy.

7. CopyObject: This is the API call used to copy an object from one bucket to another.

8. ListBuckets: This is the API call used to list all the buckets in S3

 

9. ListObjects: This is the API call used to list all the object in a given S3 bucket.

 

All the information and data used in this article is been shared with respect to the queries made by our readers. Aegis Java web development team has replied to relevant queries and also welcomes readers and critics to create new questions to answer.

Reference Links:

https://docs.aws.amazon.com/AmazonS3/latest/API/Welcome.html