0

我正在通过传输管理器将我的应用程序对象上传到 s3。它工作正常,除了极少数情况下,300 KB 的对象大小需要 15 分钟。我只是在探索引入重试逻辑,以便在 30 秒后中止当前上传并重新提交。下面是我的代码

private static UploadResult put(
            final AmazonS3 amazonS3,
            final Supplier<PutObjectRequest> requestSupplier) throws Exception {
        TransferManager tx = TransferManagerBuilder
                .standard()
                .withS3Client(amazonS3)
                .build();
        try {
            Upload myUpload = tx.upload(requestSupplier.get());

            return myUpload.waitForUploadResult();
        } finally {
            tx.shutdownNow(false);
        }
    }

我们能否有一些东西可以让我在 30 秒后让这个上传请求超时,以便我可以重试。waitForUploadResult 方法是一个阻塞调用,所以不能对它做任何事情,只能等待它完成。

提前致谢!

4

1 回答 1

0

I couldn't find a way to timeout the upload request so I submitted it in an executor so waiting part runs in separate thread. Below is my code

private static UploadResult put(
            final AmazonS3 amazonS3,
            final Supplier<PutObjectRequest> requestSupplier) throws Exception {
        TransferManager tx = TransferManagerBuilder
                .standard()
                .withS3Client(amazonS3)
                .build();
        ExecutorService ex = Executors.newSingleThreadExecutor();
        Upload myUpload = tx.upload(requestSupplier.get());
        try {
            Future<UploadResult> f = ex.submit(() -> myUpload.waitForUploadResult());
            return f.get(2, TimeUnit.MINUTES);
        } catch (TimeoutException te) {
            myUpload.abort();
            throw new RuntimeException("S3 save operation timeout after 2 minutes. Aborted.", te);
        } finally {
            ex.shutdownNow();
            tx.shutdownNow(false);
        }
    }
于 2018-12-05T06:44:20.197 回答