How to download the files from AWS S3 into local system using java

       HOW TO DOWNLOAD THE FILES FROM AWS S3 INTO LOCAL SYSTEM USING JAVA

  • First, create the object of AWSCredentials and pass the aws_access_key_id and aws_secret_access_key as parameters.
  • Next, create the s3client object for connecting to the aws s3 bucket.
  • For creating a connection we can pass the AWSCredentials object as a parameter.
  • Next, create the TransferManager object using s3client, TransferManager provides asynchronous management for uploads and downloads between your application.
  • Using the s3client.listObjects get all list of object in aws s3 bucket.
  • Using Iteratoriterate the al objects. using the S3ObjectSummary object to get all information of the object(file name.lastmodified date).
  • Finally, close the connection of TransferManager object otherwise it's running continuously.
  • Using the shutdownNow(); the method to close the connection.
Downloading files process from aws s3

import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.ListObjectsRequest; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectSummary; import com.amazonaws.services.s3.transfer.TransferManager; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import java.util.Iterator; import java.util.List; /** * * @author Janardhan Randhi * Date : Feb 4 2019 * Description : This class Downlaod the files from AWS S3 bucket into local system. * */ public class AWSFiledownload { public AWSFiledownload() {} public static void main(String[] args) throws AmazonClientException, InterruptedException, FileNotFoundException, IOException { //create a client connection based on credentials AWSCredentials credentials = new BasicAWSCredentials("***************key", "*****************pwd"); AmazonS3 s3client = new AmazonS3Client(credentials); // create bucket - name must be unique for all S3 users also small letter required String bucketName = "BucketName"; String key = "test_folder/"; //folder name long startTime = System.currentTimeMillis(); TransferManager xfer_mgr = new TransferManager(s3client); try { ObjectListing objectListing = s3client.listObjects(new ListObjectsRequest() .withBucketName(bucketName) .withPrefix(key)); Iterator localIterator = objectListing.getObjectSummaries().iterator(); if (localIterator.hasNext()) { S3ObjectSummary objectSummary = (S3ObjectSummary)localIterator.next(); System.out.println(objectSummary.getKey()); S3Object object = s3client.getObject(new GetObjectRequest(bucketName, objectSummary.getKey())); InputStream reader = new BufferedInputStream(object.getObjectContent()); File file = new File("C:\\Users\\Administrator\\Desktop\\AWS_Download\\" + objectSummary.getKey()); OutputStream writer = new BufferedOutputStream(new FileOutputStream(file)); int read = -1; while ((read = reader.read()) != -1) { writer.write(read); } writer.flush(); writer.close(); reader.close(); s3client.deleteObject(bucketName, objectSummary.getKey()); } } catch (AmazonServiceException e) { System.err.println(e.getMessage()); } finally { xfer_mgr.shutdownNow(); } System.out.println("All File are downloaded successfully from " + key + " Directory and Deleted in " + key + "Directory"); long endTime = System.currentTimeMillis(); System.out.println("Time taken to download a multiple files : " + (endTime - startTime) + " milliseconds"); } }

SUBSCRIBE TO OUR NEWSLETTER

I’m the Founder of quickdevops.com. I am a Professional Blogger, Application developer, YouTuber. I’ve been blogging since 2015.I spend a lot of time learning new techniques and actively help other people learn web development through a variety of help groups and writing web development tutorials for my website and blog about advancements in web design and development.Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie or planning a trip to someplace I've never been before.

30 Responses to "How to download the files from AWS S3 into local system using java "

  1. I appreciate several from the Information which has been composed, and especially the remarks posted I will visit once more.  Hotspot Shield Elite APK

    ReplyDelete
    Replies
    1. Please replace with ES File Explorer mod apk app. This is an application that helps you to store files that you have downloaded on your phone.

      Delete
  2. Wts the use of transfer manager object here. You are just opening and closing it

    ReplyDelete
  3. For example, you should give exact and current data while enrolling yourself at the site. file transfer

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. http://www.mamafamille.ca/

    ReplyDelete
  6. Web hosting is simply described as a place that manages the websites of individuals and organizations. When web hosting is discussed, it is referred to as a company that offers a space on a server (computer) where they can host your website files and provide Internet connectivity to allow other computers to gain access to the files on your site. Is it clear? https://onohosting.com/

    ReplyDelete
  7. This is a choice more satisfactory for medium-sized organizations that will have more die hard commitment. It utilizes Virtualization innovation to furnish you with committed assets on a worker with numerous clients. https://onohosting.com/

    ReplyDelete
  8. Take a look at these five things before you purchase web hosting services for your small business. Don't be the one to get burned! Learn about the things that make a web hosting company stand above the rest! https://hostinglelo.in/

    ReplyDelete
  9. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work. https://youstable.com

    ReplyDelete
  10. These are called your app experience steps

    ReplyDelete
  11. If you are looking for any kind of software so you must this website get into pc

    ReplyDelete