How do I download an S3 file only if it has changed?

Viewed 5340

I have a 900 MB file that I'd like to download to disk from S3 if it isn't already in place downloaded. Is there an easy way for me to only download the file if it isn't already in place? I know S3 supports querying MD5 checksum of files, but I'm hoping not to have to build this logic myself.

2 Answers

I have used below code to download S3 files which have timestamp greater than the local folder timestamp. First it's check if any of the files in S3 folder have timestamp greater than the local folder timestamp. If yes then download those files only.

    TransferManager transferManager = TransferManagerBuilder.standard().build();
    AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().build();
            Path location = Paths.get("/data/test/");
            FileTime lastModifiedTime = null;
            try {
                lastModifiedTime = Files.getLastModifiedTime(location, LinkOption.NOFOLLOW_LINKS);
            } catch (IOException e) {
                e.printStackTrace();
            }

Date lastUpdatedTime = new Date(lastModifiedTime.toMillis());        

    ObjectListing listing = amazonS3.listObjects("bucket", "test-folder");
            List<S3ObjectSummary> summaries = listing.getObjectSummaries();
            for (S3ObjectSummary os: summaries) {
                if(os.getLastModified().after(lastUpdatedTime)) {
                    try {
                        String fileName="/data/test/"+os.getKey();
                        Download multipleFileDownload = transferManager.download(bucket, os.getKey(), new File(fileName));                        
                        while (multipleFileDownload.isDone() == false) {
                            Thread.sleep(1000);
                        }
                    }catch(InterruptedException i){
                        LOG.error("Exception Occurred while downloading the file ",i);
                    }
                }
            }
Related