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

最佳答案

我会这样:

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();

10-06 12:31