完成后端架构重构等
This commit is contained in:
+66
@@ -2,14 +2,18 @@ package com.nanri.aiimage.modules.file.service.oss;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import com.nanri.aiimage.config.OssProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@@ -33,6 +37,68 @@ public class OssStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
public String uploadText(String objectKey, String content) {
|
||||
if (objectKey == null || objectKey.isBlank()) {
|
||||
throw new IllegalArgumentException("objectKey must not be blank");
|
||||
}
|
||||
OSS ossClient = buildClient();
|
||||
try (ByteArrayInputStream stream = new ByteArrayInputStream(
|
||||
Objects.requireNonNullElse(content, "").getBytes(StandardCharsets.UTF_8))) {
|
||||
ossClient.putObject(ossProperties.getBucket(), objectKey, stream);
|
||||
return objectKey;
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException("failed to upload text to oss", ex);
|
||||
} finally {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public String uploadTaskScopePayload(String moduleType, Long taskId, String scopeHash, String content) {
|
||||
String normalizedModuleType = moduleType == null || moduleType.isBlank()
|
||||
? "unknown"
|
||||
: moduleType.trim().toLowerCase();
|
||||
String normalizedScopeHash = scopeHash == null || scopeHash.isBlank() ? UUID.randomUUID().toString() : scopeHash;
|
||||
String objectKey = String.format("task-scope/%s/%s/%s.json", normalizedModuleType, taskId, normalizedScopeHash);
|
||||
return uploadText(objectKey, content);
|
||||
}
|
||||
|
||||
public String uploadTaskParsedPayload(String moduleType, Long taskId, String scopeHash, String content) {
|
||||
String normalizedModuleType = moduleType == null || moduleType.isBlank()
|
||||
? "unknown"
|
||||
: moduleType.trim().toLowerCase();
|
||||
String normalizedScopeHash = scopeHash == null || scopeHash.isBlank() ? UUID.randomUUID().toString() : scopeHash;
|
||||
String objectKey = String.format("task-parsed/%s/%s/%s.json", normalizedModuleType, taskId, normalizedScopeHash);
|
||||
return uploadText(objectKey, content);
|
||||
}
|
||||
|
||||
public String readObjectAsString(String value) {
|
||||
String objectKey = resolveObjectKey(value);
|
||||
if (objectKey == null || objectKey.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
OSS ossClient = buildClient();
|
||||
try (OSSObject ossObject = ossClient.getObject(ossProperties.getBucket(), objectKey)) {
|
||||
return new String(ossObject.getObjectContent().readAllBytes(), StandardCharsets.UTF_8);
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException("failed to read object from oss", ex);
|
||||
} finally {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteObject(String value) {
|
||||
String objectKey = resolveObjectKey(value);
|
||||
if (objectKey == null || objectKey.isBlank()) {
|
||||
return;
|
||||
}
|
||||
OSS ossClient = buildClient();
|
||||
try {
|
||||
ossClient.deleteObject(ossProperties.getBucket(), objectKey);
|
||||
} finally {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 objectKey 生成预签名下载 URL(1小时有效)。
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user