Chris
Chris

Reputation: 3657

Amazon S3 java sdk - download progress

Trying to find how to output a clean count of 0 - 100% while downloading a file from amazon. There are plenty of examples of how to do this for uploading but they don't seem to directly translate to downloads.

At the moment I'm doing the following

    TransferManager transferManager = new TransferManager(s3Client);
    Download download = transferManager.download(s3Request, downloadedFile);

    while (!download.isDone()) {
        LOGGER.info("Downloaded >> " + download.getProgress().getPercentTransferred());
    }

Which does work, but it spams the console with the same value many many times (assume as it's threaded).

I know I can also do something like:

    ProgressListener listener = progressEvent -> LOGGER.info("Bytes transfer >> " + progressEvent.getBytesTransferred());
    
    GetObjectRequest s3Request = new GetObjectRequest("swordfish-database", snapshot.getKey());
    TransferManager transferManager = new TransferManager(s3Client);
    Download download = transferManager.download(s3Request, downloadedFile);
    
    download.addProgressListener(listener);
    download.waitForCompletion();

Which also works but, I loose the ease of using download.getProgress().getPercentTransferred(). Is this the more proper way of doing this?

Ultimately I want to be getting an int to use for a progress bar.

Upvotes: 4

Views: 2573

Answers (1)

Paweł Chorążyk
Paweł Chorążyk

Reputation: 3643

Well if you reserve a separate thread for progress updating then I don't see any problems with the first approach - you could just add something like TimeUnit.SECONDS.sleep(1) to update the progress every second instead of looping all the time. On the other hand in the second approach you only have to divide getBytesTransferred() by getBytes() to get percentage which also doesn't seem too difficult :-)

Upvotes: 2

Related Questions