fix 修复 大文件分片断点续传 无法续传问题
This commit is contained in:
+23
-11
@@ -11,7 +11,6 @@ import org.dromara.common.oss.model.GetObjectResult;
|
|||||||
import org.dromara.common.oss.model.HandleAsyncResult;
|
import org.dromara.common.oss.model.HandleAsyncResult;
|
||||||
import org.dromara.common.oss.model.Options;
|
import org.dromara.common.oss.model.Options;
|
||||||
import org.dromara.common.oss.model.PutObjectResult;
|
import org.dromara.common.oss.model.PutObjectResult;
|
||||||
import org.jspecify.annotations.NullMarked;
|
|
||||||
import software.amazon.awssdk.core.ResponseInputStream;
|
import software.amazon.awssdk.core.ResponseInputStream;
|
||||||
import software.amazon.awssdk.core.async.AsyncRequestBody;
|
import software.amazon.awssdk.core.async.AsyncRequestBody;
|
||||||
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
|
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
|
||||||
@@ -426,9 +425,25 @@ public abstract class AbstractOssClientImpl implements OssClient {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public PutObjectResult bucketUpload(String bucket, String key, InputStream in, long contentLength, Options options) {
|
public PutObjectResult bucketUpload(String bucket, String key, InputStream in, long contentLength, Options options) {
|
||||||
options.setLength(contentLength);
|
Path tempFile = null;
|
||||||
AsyncRequestBody body = AsyncRequestBody.fromInputStream(in, contentLength, asyncExecutor);
|
try {
|
||||||
return bucketUpload(bucket, key, body, options);
|
tempFile = Files.createTempFile("ruoyi-oss-upload-", ".tmp");
|
||||||
|
try (OutputStream out = Files.newOutputStream(tempFile)) {
|
||||||
|
in.transferTo(out);
|
||||||
|
}
|
||||||
|
options.setLength(Files.size(tempFile));
|
||||||
|
return bucketUpload(bucket, key, tempFile, options);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw toStorageException(e);
|
||||||
|
} finally {
|
||||||
|
if (tempFile != null) {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(tempFile);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
// 临时文件清理失败不影响上传结果。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -456,11 +471,9 @@ public abstract class AbstractOssClientImpl implements OssClient {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public PutObjectResult bucketUpload(String bucket, String key, byte[] data, Options options) {
|
public PutObjectResult bucketUpload(String bucket, String key, byte[] data, Options options) {
|
||||||
try (ByteArrayInputStream in = new ByteArrayInputStream(data)) {
|
options.setLength((long) data.length);
|
||||||
return bucketUpload(bucket, key, in, data.length, options);
|
AsyncRequestBody body = AsyncRequestBody.fromBytes(data);
|
||||||
} catch (Exception e) {
|
return bucketUpload(bucket, key, body, options);
|
||||||
throw toStorageException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -485,7 +498,6 @@ public abstract class AbstractOssClientImpl implements OssClient {
|
|||||||
* @param options 上传选项
|
* @param options 上传选项
|
||||||
* @return 上传结果
|
* @return 上传结果
|
||||||
*/
|
*/
|
||||||
@NullMarked
|
|
||||||
private PutObjectResult bucketUpload(String bucket, String key, AsyncRequestBody body, Options options) {
|
private PutObjectResult bucketUpload(String bucket, String key, AsyncRequestBody body, Options options) {
|
||||||
// 优先使用body中的内容大小,如果不存在,再获取可选项中的
|
// 优先使用body中的内容大小,如果不存在,再获取可选项中的
|
||||||
Long contentLength = body.contentLength().orElse(options.getLength());
|
Long contentLength = body.contentLength().orElse(options.getLength());
|
||||||
@@ -503,7 +515,7 @@ public abstract class AbstractOssClientImpl implements OssClient {
|
|||||||
.metadata(metadata);
|
.metadata(metadata);
|
||||||
}, transferListeners);
|
}, transferListeners);
|
||||||
if (result.isFailure()) {
|
if (result.isFailure()) {
|
||||||
throw S3StorageException.form(result.error());
|
throw toStorageException(result.error());
|
||||||
}
|
}
|
||||||
Optional<PutObjectResponse> opt = result.getResult();
|
Optional<PutObjectResponse> opt = result.getResult();
|
||||||
if (opt.isEmpty()) {
|
if (opt.isEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user