DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Content Management in Amazon S3 Using Java

Content Management in Amazon S3 Using Java

This tutorial will show you how to manage your content in S3 using Java and how to make basic requests using the AWS SDK.

Rahul Gupta user avatar by
Rahul Gupta
·
Jun. 22, 18 · Tutorial
Like (1)
Save
Tweet
Share
13.80K Views

Join the DZone community and get the full member experience.

Join For Free

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. To know more about this service, you can refer to the official AWS S3 documentation 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 an 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 the AWS Java SDK. You can download it from here. This zip file itself is included in all the required JARs.

Firstly, create an 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 globally unique on S3. A bucket is simply a container with which you are going to interact. 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 possible 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!

AWS Java (programming language) Object (computer science) Content management system

Published at DZone with permission of Rahul Gupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Quickly Build an Audio Editor With UI
  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • Core Machine Learning Metrics
  • Why You Should Automate Code Reviews

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: