简体   繁体   English

Amazon S3 java sdk - 下载进度

[英]Amazon S3 java sdk - download progress

Trying to find how to output a clean count of 0 - 100% while downloading a file from amazon. 尝试从亚马逊下载文件时,如何输出干净计数0 - 100%。 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() . 哪个也有效但是,我放松了使用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. 最终我想获得一个用于进度条的int。 Any advice? 有什么建议?

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. 好吧,如果您为进度更新保留一个单独的线程,那么我没有看到第一种方法有任何问题 - 您可以添加类似TimeUnit.SECONDS.sleep(1)来每秒更新进度而不是一直循环。 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 :-) 另一方面,在第二种方法中,你只需将getBytesTransferred()除以getBytes()来获得百分比,这似乎也不太难:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM