上传文件或InputStream,为S3与进度回调回调、进度、上传文件、InputStream

2023-09-11 08:11:08 作者:柠檬味的少女

我们正在上载使用亚马逊的AWS Java库文件,且很难获得上传进度。目前,我们正在调用以下:

We are uploading a file using the Amazon AWS Java Library and are having difficulty obtaining upload progress. We're currently calling the following:

File file = new File(localAsset.getVideoFilePath());
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, localAsset.getFileName(), file);
s3.putObject(putObjectRequest);

如何才能设置一个回调来检查上的文件上传进度?

How can we set a callback to check up on file upload progress?

感谢

推荐答案

我来到这个确切的问题,并写了一个简单的InputStream包装,打印出漂亮的进度条:

I came upon this exact problem and wrote a simple InputStream wrapper that prints out nice progress bars:

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.vfs.FileContent;
import org.apache.commons.vfs.FileSystemException;

public class ProgressInputStream extends InputStream {
    private final long size;
    private long progress, lastUpdate = 0;
    private final InputStream inputStream;
    private final String name;
    private boolean closed = false;

    public ProgressInputStream(String name, InputStream inputStream, long size) {
        this.size = size;
        this.inputStream = inputStream;
        this.name = name;
    }

    public ProgressInputStream(String name, FileContent content)
    throws FileSystemException {
        this.size = content.getSize();
        this.name = name;
        this.inputStream = content.getInputStream();
    }

    @Override
    public void close() throws IOException {
        super.close();
        if (closed) throw new IOException("already closed");
        closed = true;
    }

    @Override
    public int read() throws IOException {
        int count = inputStream.read();
        if (count > 0)
            progress += count;
        lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
        return count;
    }
    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int count = inputStream.read(b, off, len);
        if (count > 0)
            progress += count;
        lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
        return count;
    }

    static long maybeUpdateDisplay(String name, long progress, long lastUpdate, long size) {
        if (Config.isInUnitTests()) return lastUpdate;
        if (size < B_IN_MB/10) return lastUpdate;
        if (progress - lastUpdate > 1024 * 10) {
            lastUpdate = progress;
            int hashes = (int) (((double)progress / (double)size) * 40);
            if (hashes > 40) hashes = 40;
            String bar = StringUtils.repeat("#",
                    hashes);
            bar = StringUtils.rightPad(bar, 40);
            System.out.format("%s [%s] %.2fMB/%.2fMB\r",
                    name, bar, progress / B_IN_MB, size / B_IN_MB);
            System.out.flush();
        }
        return lastUpdate;
    }
}

(这是复制和粘贴的活code,所以你可能需要做一些调整信息获得它在自己的code工作。)

(this is copy-and-pasted from live code, so you might have to do a few fixups to get it to work in your own code.)

然后,就用把事情的InputStream的方式(请确保指定的大小!),它会是一个不错的进度条为您服务。如果你想有一个适当的回调将会是pretty的容易做了。

Then, just use the InputStream way of putting things (make sure to specify the size!) and it will make a nice progress bar for you. If you want a proper callback that'd be pretty easy to do too.