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.
//'import' statements will be placed here. public class S3Tutorial { public static void main(String[] args) { //the blocks of code given below, will be placed here. } }
Amazon S3 client >> Make an Amazon S3 client with your AWS secret access key, access key id and your S3 bucket’s region.
AWSCredentials awsCredentials = null; try { awsCredentials = new AWSCredentials() { @Override public String getAWSSecretKey() { return "paste_your_aws_secret_access_key_here"; } @Override public String getAWSAccessKeyId() { return "paste_your_aws_access_key_id_here"; } }; } catch (Exception e) { throw new AmazonClientException("can not load your aws credentials, please check your credentials !!", e); } AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) .withRegion("paste_your_aws_s3_region_here") .build(); //my S3 region is EU (London), so i am using 'eu-west-2'.
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.
System.out.println("welcome to Amazon S3 !!"); String bucketName = "s3-tutorial-bucket";
Creating bucket >> Now create a bucket in your Amazon S3 storage.
System.out.println("creating bucket : " + bucketName + "\n"); s3Client.createBucket(bucketName);
Listing buckets >> List all the buckets available in your S3 account.
System.out.println("listing buckets..."); int buckIndex = 1; for (Bucket bucket : s3Client.listBuckets()) { System.out.println(buckIndex + ". " + bucket.getName()); } System.out.println();
Key >> Choose a key for the object which you are uploading. Your file will be saved on S3 with this name.
String key = "S3TutorialKey";
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.
System.out.println("uploading a new object to S3 bucket from a file...\n"); s3Client.putObject(new PutObjectRequest(bucketName, key, new File("D:\\image.jpg")));
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.
System.out.println("downloading an object..."); S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucketName, key)); System.out.println("content-type of your downloaded object : " + object.getObjectMetadata().getContentType()); try (InputStream inputStream = s3Object.getObjectContent();) { Image image = ImageIO.read(inputStream); //use a label to display the image. JFrame frame = new JFrame(); JLabel label = new JLabel(new ImageIcon(image)); frame.getContentPane().add(label, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } catch (Exception ex) { System.out.println("some exception occured !"); }
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.
System.out.println("listing objects..."); int objIndex = 1; ObjectListing objectListing = s3Client.listObjects(new ListObjectsRequest() .withBucketName(bucketName) .withPrefix("S3")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println(objIndex + ". " + objectSummary.getKey() + "\t(size = " + objectSummary.getSize() + ")"); }
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.
System.out.println("deleting an object...\n"); s3Client.deleteObject(bucketName, key);
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.
System.out.println("deleting bucket : " + bucketName + "\n"); s3Client.deleteBucket(bucketName);
So, these were the basic operations of Amazon S3 using AWS Java SDK. Enjoy reading !! 🙂