|
|
@@ -46,12 +46,9 @@ public class OssClient {
|
|
|
this.sk = sk;
|
|
|
}
|
|
|
|
|
|
- public UploadPrepareRet prepareUpload(Path filePath) throws Exception {
|
|
|
+ public UploadPrepareRet prepareUpload(int channelCode, Path filePath) throws Exception {
|
|
|
// 1. 计算 SHA-256 (用于后端秒传校验)
|
|
|
String sha256 = OssSamplingHash.calculateFullHash(filePath);
|
|
|
- System.out.printf("%s 的 sha256sum %s\n", filePath, sha256);
|
|
|
-
|
|
|
- int channelCode = 101;
|
|
|
String filename0 = filePath.getFileName().toString();
|
|
|
long size = filePath.toFile().length();
|
|
|
UploadPrepare uploadPrepare = new UploadPrepare(channelCode, filename0, size, sha256);
|
|
|
@@ -74,11 +71,9 @@ public class OssClient {
|
|
|
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
|
|
|
.build();
|
|
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
- if (response.statusCode() == 200) {
|
|
|
- System.out.println("服务端返回: " + response.body());
|
|
|
- } else {
|
|
|
- System.err.println("上传失败,状态码: " + response.statusCode());
|
|
|
- return null;
|
|
|
+ if (response.statusCode() != 200) {
|
|
|
+ String errorMsg = String.format("上传失败,状态码: %s", response.statusCode());
|
|
|
+ throw new RuntimeException(errorMsg);
|
|
|
}
|
|
|
|
|
|
String body = response.body();
|
|
|
@@ -93,8 +88,6 @@ public class OssClient {
|
|
|
long offset = uploadPrepareRet.getOffset();
|
|
|
int length = uploadPrepareRet.getLength();
|
|
|
String sampleMd5 = OssSamplingHash.calculateSampleMd5(filePath.toString(), offset, length);
|
|
|
- System.out.printf("%s 的抽样 md5 %s\n", filePath, sampleMd5);
|
|
|
-
|
|
|
UploadSample uploadSample = new UploadSample(uploadId, sampleMd5);
|
|
|
return checkSample(uploadSample);
|
|
|
}
|
|
|
@@ -116,21 +109,15 @@ public class OssClient {
|
|
|
.build();
|
|
|
|
|
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
- if (response.statusCode() == 200) {
|
|
|
- System.out.println("服务端返回: " + response.body());
|
|
|
- } else {
|
|
|
- System.err.println("上传失败,状态码: " + response.statusCode());
|
|
|
- return null;
|
|
|
+ if (response.statusCode() != 200) {
|
|
|
+ String errorMsg = String.format("上传失败,状态码: %s", response.statusCode());
|
|
|
+ throw new RuntimeException(errorMsg);
|
|
|
}
|
|
|
|
|
|
String body = response.body();
|
|
|
Type type1 = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
|
|
|
WebResult<UploadFileRet> webResult1 = JsonConverter.jsonToObject(body, type1);
|
|
|
UploadFileRet uploadFileRet = webResult1.getData();
|
|
|
- if (uploadFileRet.isFastUpload()) {
|
|
|
- System.out.println("fast upload success...");
|
|
|
- }
|
|
|
-
|
|
|
return uploadFileRet;
|
|
|
}
|
|
|
|
|
|
@@ -187,7 +174,6 @@ public class OssClient {
|
|
|
*/
|
|
|
public void uploadFile(String uploadUrl, String uploadToken, Path filePath) throws Exception {
|
|
|
// 1. 计算 SHA-256 (用于后端秒传校验)
|
|
|
- System.out.println("正在计算哈希...");
|
|
|
String sha256 = OssSamplingHash.calculateFullHash(filePath);
|
|
|
|
|
|
String contentType = "application/octet-stream";
|
|
|
@@ -206,18 +192,16 @@ public class OssClient {
|
|
|
.POST(HttpRequest.BodyPublishers.ofFile(filePath))
|
|
|
.build();
|
|
|
|
|
|
- System.out.println("开始上传文件: " + filePath.toString());
|
|
|
// 3. 发送请求
|
|
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
- if (response.statusCode() == 200) {
|
|
|
- System.out.println("服务端返回: " + response.body());
|
|
|
- } else {
|
|
|
- System.err.println("上传失败,状态码: " + response.statusCode());
|
|
|
+ if (response.statusCode() != 200) {
|
|
|
+ String errorMsg = String.format("上传失败,状态码: %s", response.statusCode());
|
|
|
+ throw new RuntimeException(errorMsg);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet uploadFilePart(String uploadUrl, String token, Path filePath) throws Exception {
|
|
|
- UploadPrepareRet uploadPrepareRet = prepareUpload(filePath);
|
|
|
+ public UploadFileRet uploadFilePart(int channelCode, String uploadUrl, String token, Path filePath) throws Exception {
|
|
|
+ UploadPrepareRet uploadPrepareRet = prepareUpload(channelCode, filePath);
|
|
|
if (uploadPrepareRet == null) {
|
|
|
return null;
|
|
|
}
|
|
|
@@ -225,12 +209,10 @@ public class OssClient {
|
|
|
if (uploadPrepareRet.isExist()) {
|
|
|
UploadFileRet uploadFileRet = checkSample(filePath, uploadPrepareRet);
|
|
|
if (uploadFileRet.isFastUpload()) {
|
|
|
- System.out.println("文件快传完成...");
|
|
|
return uploadFileRet;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- System.out.println("上传文件到 oss-store...");
|
|
|
String uploadId = uploadPrepareRet.getUploadId();
|
|
|
String identifier = uploadId;
|
|
|
File file = filePath.toFile();
|
|
|
@@ -248,8 +230,6 @@ public class OssClient {
|
|
|
Map<Integer, Long> map = new HashMap<>();
|
|
|
List<Integer> failedChunkNumbers = new ArrayList<>();
|
|
|
for (int i = 0; i < totalChunks; i++) {
|
|
|
- System.out.printf("上传第 %s 个分片...\n", i+1);
|
|
|
-
|
|
|
long start = i*chunkSize;
|
|
|
// 从 start 位置开始读取 chunkSize 的数据
|
|
|
byte[] part = fileSplitter.getPart(file.getAbsolutePath(), start);
|
|
|
@@ -262,13 +242,8 @@ public class OssClient {
|
|
|
|
|
|
UploadFileRet uploadFileRet = postObject(uploadUrl, token, part, uploadFilePart);
|
|
|
if (uploadFileRet == null) {
|
|
|
- System.out.printf("%s:%s upload failed\n", chunkNumber, currentChunkSize);
|
|
|
failedChunkNumbers.add(chunkNumber);
|
|
|
} else {
|
|
|
- System.out.printf("%s:%s uploaded %s bytes\n", totalChunks, chunkNumber, currentChunkSize);
|
|
|
- /*if (uploadFileRet.isMerged()) {
|
|
|
- return uploadFileRet;
|
|
|
- }*/
|
|
|
if (uploadFileRet.isUploaded()) {
|
|
|
return uploadFileRet;
|
|
|
}
|
|
|
@@ -331,8 +306,6 @@ public class OssClient {
|
|
|
return webBody.getUploadFileRet();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- System.out.printf("%s -> %s\n", statusCode, api);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@@ -356,8 +329,8 @@ public class OssClient {
|
|
|
// 发送请求并获取响应
|
|
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
if (response.statusCode() != 200) {
|
|
|
- System.out.printf("Error: %s\n", response.statusCode());
|
|
|
- return null;
|
|
|
+ String errorMsg = String.format("Error: %s", response.statusCode());
|
|
|
+ throw new RuntimeException(errorMsg);
|
|
|
}
|
|
|
|
|
|
String body = response.body();
|
|
|
@@ -427,12 +400,12 @@ public class OssClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void setObjectScope(String objectId, ObjectScope objectScope) {
|
|
|
+ public void setObjectScope(String objectId, int scope) {
|
|
|
Map<String, Object> body = Map.of(
|
|
|
"objectId", objectId,
|
|
|
- "scope", objectScope.getCode());
|
|
|
+ "scope", scope);
|
|
|
String jsonBody = JsonConverter.objectToJson(body);
|
|
|
- String api = "/api/oss/sdk/upload_request";
|
|
|
+ String api = "/api/oss/sdk/object/scope";
|
|
|
try {
|
|
|
String responseBody = getPostResponseBody(api, jsonBody);
|
|
|
} catch (Exception e) {
|
|
|
@@ -453,10 +426,9 @@ public class OssClient {
|
|
|
// 直接下载到指定路径,HttpClient 会自动处理流的关闭
|
|
|
HttpResponse<Path> response = httpClient.send(request,
|
|
|
HttpResponse.BodyHandlers.ofFile(Paths.get(targetFile.getAbsolutePath())));
|
|
|
- if (response.statusCode() == 200) {
|
|
|
- System.out.println("下载成功,存放在: " + targetFile.getAbsolutePath());
|
|
|
- } else {
|
|
|
- System.err.println("下载失败,状态码: " + response.statusCode());
|
|
|
+ if (response.statusCode() != 200) {
|
|
|
+ String errorMsg = String.format("下载失败,状态码: %s", response.statusCode());
|
|
|
+ throw new RuntimeException(errorMsg);
|
|
|
}
|
|
|
}
|
|
|
|