Amazon S3 AWS上传进度监听器

时间:2018-08-16 09:27:12

标签: java amazon-web-services amazon-s3 aws-sdk

有人知道如何在Amazon S3的分段上传中查看上传进度(百分比)吗?

3 个答案:

答案 0 :(得分:2)

我会这样:

MultipleFileUpload transfer = transferManager.uploadDirectory(mybucket, null, new File(localSourceDataFilesPath), false);

// blocks the thread until the upload is completed
showTransferProgress(transfer);

然后在 showTransferProgress 中,我将使用睡眠创建一个阻止上传的文件,并每隔X秒进行一次数学运算:

private void showTransferProgress(MultipleFileUpload xfer) {
        while (!xfer.isDone()) {
            // some logic to wait so you don't do the math every second like a Thread.sleep

            TransferProgress progress = xfer.getProgress();
            long bytesTransferred = progress.getBytesTransferred();
            long total = progress.getTotalBytesToTransfer();
            Double percentDone = progress.getPercentTransferred();
            LOG.debug("S3 xml upload progress...{}%", percentDone.intValue());
            LOG.debug("{} bytes transferred to S3 out of {}", bytesTransferred, total);
        }

        // print the final state of the transfer.
        TransferState xferState = xfer.getState();
        LOG.debug("Final transfer state: " + xferState);
    }

这是您要查找的行:

Double percentDone = progress.getPercentTransferred();

答案 1 :(得分:0)

这可行,但可能是一种更好的方法

TransferManager tm = TransferManagerBuilder
                .standard()
                .withS3Client(s3Client)
                .build();

        PutObjectRequest request = new PutObjectRequest(bucketName, file.getName(), file);

        ProgressListener progressListener = new ProgressListener() {
            @Override
            public void progressChanged(com.amazonaws.event.ProgressEvent progressEvent) {
                bytesUploaded += progressEvent.getBytesTransferred();// add counter
                if (bytesUploaded > byteTrigger) {
                    if ((bytesUploaded + sizeRatio) < fileSize) {
                        byteTrigger = bytesUploaded + sizeRatio ;
                    } else {
                        byteTrigger = bytesUploaded + (sizeRatio / 6);// increase precision approaching the end
                    }
                    String percent = new DecimalFormat("###.##").format(bytesUploaded * 100.0 / fileSize);
                    log.info("Uploaded: " + FileUtils.byteCountToDisplaySize(bytesUploaded) + " - " + percent + "%");
                }
            }
        };

        request.setGeneralProgressListener(progressListener);
        Upload upload = tm.upload(request);

        log.info("starting upload");

        upload.waitForUploadResult();

        log.info("Upload completed");

答案 2 :(得分:0)

大家好,这是我的最终版本

private void awsHoldUntilCompletedAndShowTransferProgress(Upload upload) throws InterruptedException, AmazonClientException {
        TransferProgress tProgress = upload.getProgress();
        long totalSize = tProgress.getTotalBytesToTransfer();
        long bPrevious = 0;
        int timerSec = Math.toIntExact(Math.round(Math.sqrt(totalSize / 1024 / 1024) / 4));// calculate based on file size
        if (timerSec > 60) {// no longer than 60 second per loop
            timerSec = 60;
        }
        while (!upload.isDone()) {
            long bTransferred = tProgress.getBytesTransferred();
            String strMbps = Double.valueOf((((bTransferred - bPrevious) / timerSec) / 1024) / 1024).toString() + " MBps";
            String strTransfered = bTransferred + " bytes";
            if (bTransferred > 1024) {
                strTransfered = Double.valueOf((bTransferred / 1024) / 1024).toString() + " MB";
            }
            log.info("Upload progress: "
                    + strTransfered
                    + " / "
                    + FileUtils.byteCountToDisplaySize(totalSize) + " - "
                    + Math.round(tProgress.getPercentTransferred()) + "% "
                    + strMbps);

            bPrevious = bTransferred;
            TimeUnit.SECONDS.sleep(timerSec);
        }

        Transfer.TransferState transferState = upload.getState();
        log.info("Final transfer state: " + transferState);

        if (transferState == Transfer.TransferState.Failed) {
            throw upload.waitForException();
        }
}

这是我从

调用上面的代码的地方
..stuff...

TransferManager tm = TransferManagerBuilder
                        .standard()
                        .withS3Client(s3Client)                    
                        .build();

LocalDateTime uploadStartedAt = LocalDateTime.now();
log.info("Starting to upload " + FileUtils.byteCountToDisplaySize(fileSize));

Upload up = tm.upload(bucketName, file.getName(), file);

awsHoldUntilCompletedAndShowTransferProgress(up);

log.info("Time consumed: " + DurationFormatUtils.formatDuration(Duration.between(uploadStartedAt, LocalDateTime.now()).toMillis(), "dd HH:mm:ss"));