初始化
commit
952595dbb7
|
@ -0,0 +1,35 @@
|
|||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
|
@ -0,0 +1,62 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>alibailian</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>3.2.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>3.2.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.17.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>dashscope-sdk-java</artifactId>
|
||||
<version>2.18.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>bailian20231229</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-openapi</artifactId>
|
||||
<version>0.3.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-console</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>tea-util</artifactId>
|
||||
<version>0.2.23</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,18 @@
|
|||
package com.muyu.aliyun;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author dongzeliang
|
||||
* @version 1.0
|
||||
* @description: 阿里云百炼
|
||||
* @date 2025/2/21 17:33
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class AliYunBaiLianApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AliYunBaiLianApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.muyu.aliyun.bailian;
|
||||
|
||||
import com.aliyun.bailian20231229.Client;
|
||||
import com.aliyun.bailian20231229.models.AddFileRequest;
|
||||
import com.aliyun.bailian20231229.models.AddFileResponse;
|
||||
import com.aliyun.bailian20231229.models.AddFileResponseBody;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.muyu.aliyun.bailian.data.UploadDataManagementMode;
|
||||
import com.muyu.aliyun.bailian.data.fixed.AddFileResponseBodyDataMode;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@Component
|
||||
@Log4j2
|
||||
public class AddDataManagement {
|
||||
|
||||
|
||||
@Autowired
|
||||
private Client client;
|
||||
|
||||
public AddFileResponseBodyDataMode addFile(UploadDataManagementMode uploadDataManagementMode) {
|
||||
AddFileRequest addFileRequest = new AddFileRequest();
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
try {
|
||||
AddFileResponse addFileResponse = client.addFileWithOptions(uploadDataManagementMode.getWorkspaceId(), addFileRequest, headers, runtime);
|
||||
if (addFileResponse.getStatusCode() != 200) {
|
||||
throw new RuntimeException(addFileResponse.toString());
|
||||
}
|
||||
AddFileResponseBody body = addFileResponse.getBody();
|
||||
AddFileResponseBody.AddFileResponseBodyData data = body.getData();
|
||||
log.info("data:{}", data);
|
||||
return AddFileResponseBodyDataMode.builder()
|
||||
.fileId(data.getFileId())
|
||||
.parser(data.getParser())
|
||||
.build();
|
||||
|
||||
} catch (Exception error) {
|
||||
log.error("异常:「{}」", error.getMessage(), error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.muyu.aliyun.bailian;
|
||||
|
||||
import com.aliyun.bailian20231229.Client;
|
||||
import com.aliyun.bailian20231229.models.ApplyFileUploadLeaseResponse;
|
||||
import com.aliyun.bailian20231229.models.ApplyFileUploadLeaseResponseBody;
|
||||
import com.aliyun.bailian20231229.models.ApplyFileUploadLeaseRequest;
|
||||
import com.aliyun.tea.*;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.muyu.aliyun.bailian.data.FileUploadLeaseMode;
|
||||
import com.muyu.aliyun.bailian.data.fixed.UploadLeaseParamsMode;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@Log4j2
|
||||
public class ApplyLease {
|
||||
|
||||
@Autowired
|
||||
private Client client;
|
||||
|
||||
public UploadLeaseParamsMode applyFileUploadLease(FileUploadLeaseMode fileUploadLeaseModel) {
|
||||
ApplyFileUploadLeaseRequest applyFileUploadLeaseRequest = new ApplyFileUploadLeaseRequest();
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
|
||||
applyFileUploadLeaseRequest.setFileName(fileUploadLeaseModel.getFileName());
|
||||
applyFileUploadLeaseRequest.setMd5(fileUploadLeaseModel.getMd5());
|
||||
applyFileUploadLeaseRequest.setSizeInBytes(fileUploadLeaseModel.getSizeInBytes());
|
||||
applyFileUploadLeaseRequest.setCategoryType(fileUploadLeaseModel.getCategoryId());
|
||||
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
ApplyFileUploadLeaseResponse applyFileUploadLeaseResponse =
|
||||
client.applyFileUploadLeaseWithOptions(fileUploadLeaseModel.getCategoryId(), fileUploadLeaseModel.getWorkspaceId(), applyFileUploadLeaseRequest, headers, runtime);
|
||||
if (applyFileUploadLeaseResponse.getStatusCode() != 200) {
|
||||
throw new RuntimeException(applyFileUploadLeaseResponse.toString());
|
||||
}
|
||||
ApplyFileUploadLeaseResponseBody body = applyFileUploadLeaseResponse.getBody();
|
||||
ApplyFileUploadLeaseResponseBody.ApplyFileUploadLeaseResponseBodyData data = body.getData();
|
||||
ApplyFileUploadLeaseResponseBody.ApplyFileUploadLeaseResponseBodyDataParam param = data.getParam();
|
||||
log.info("param:「{}」", param);
|
||||
return UploadLeaseParamsMode.builder()
|
||||
.Url(param.getUrl())
|
||||
.Method(param.getMethod())
|
||||
.build();
|
||||
|
||||
} catch (TeaException error) {
|
||||
String exceptionName;
|
||||
switch (error.getCode()) {
|
||||
case "SDK.ConnectTimeout":
|
||||
exceptionName = "网络连接超时";
|
||||
break;
|
||||
case "SDK.ReadTimeout":
|
||||
exceptionName = "网络读取超时";
|
||||
break;
|
||||
case "SDK.ServerUnreachable":
|
||||
exceptionName = "服务器不可达";
|
||||
break;
|
||||
case "InvalidAccessKeyId.NotFound":
|
||||
exceptionName = "无效的访问密钥 ID";
|
||||
break;
|
||||
case "SignatureDoesNotMatch":
|
||||
exceptionName = "签名不匹配";
|
||||
break;
|
||||
case "MissingParameter":
|
||||
exceptionName = "缺少必要参数";
|
||||
break;
|
||||
case "InvalidParameterValue":
|
||||
exceptionName = "参数值无效";
|
||||
break;
|
||||
case "Forbidden.NoPermission":
|
||||
exceptionName = "没有权限";
|
||||
break;
|
||||
default:
|
||||
exceptionName = "未知阿里云 API 异常";
|
||||
}
|
||||
log.error("异常名称:「{}」,错误码: {}, 错误信息: {}, 错误数据: {}", exceptionName, error.getCode(), error.getMessage(), error.getData());
|
||||
} catch (NullPointerException e) {
|
||||
log.error("异常名称:「空指针异常」,错误信息: {}", e.getMessage(), e);
|
||||
} catch (ClassCastException e) {
|
||||
log.error("异常名称:「类型转换异常」,错误信息: {}", e.getMessage(), e);
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
log.error("异常名称:「数组越界异常」,错误信息: {}", e.getMessage(), e);
|
||||
} catch (Exception _error) {
|
||||
log.error("异常名称:「未知通用异常」,错误信息: {}", _error.getMessage(), _error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.muyu.aliyun.bailian;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* Md5工具类
|
||||
*/
|
||||
public class Md5Utils {
|
||||
public static String getFileMd5(String filePath) {
|
||||
MessageDigest digest = null;
|
||||
try {
|
||||
digest = MessageDigest.getInstance("MD5");
|
||||
|
||||
try (InputStream is = Files.newInputStream(Paths.get(filePath))) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int read;
|
||||
while ((read = is.read(buffer)) > 0) {
|
||||
digest.update(buffer, 0, read);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
byte[] md5Bytes = digest.digest();
|
||||
|
||||
StringBuilder md5String = new StringBuilder();
|
||||
for (byte b : md5Bytes) {
|
||||
md5String.append(String.format("%02x", b));
|
||||
}
|
||||
return md5String.toString();
|
||||
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.muyu.aliyun.bailian;
|
||||
|
||||
import com.aliyun.bailian20231229.Client;
|
||||
import com.aliyun.bailian20231229.models.*;
|
||||
import com.aliyun.tea.*;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.muyu.aliyun.bailian.data.UploadDescribeFileMode;
|
||||
import com.muyu.aliyun.bailian.data.fixed.DescribeFileResponseBodyDataMode;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@Component
|
||||
@Log4j2
|
||||
public class ReviewResolution {
|
||||
|
||||
@Autowired
|
||||
private Client client;
|
||||
|
||||
public DescribeFileResponseBodyDataMode DescribeFile(UploadDescribeFileMode uploadDescribeFileMode) {
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
DescribeFileResponse describeFileResponse = client.describeFileWithOptions(uploadDescribeFileMode.getWorkspaceId(), uploadDescribeFileMode.getFileId(), headers, runtime);
|
||||
if (describeFileResponse.getStatusCode() == 200) {
|
||||
log.info("文件信息获取成功");
|
||||
} else {
|
||||
log.info("文件信息获取失败");
|
||||
throw new RuntimeException(describeFileResponse.toString());
|
||||
}
|
||||
|
||||
DescribeFileResponseBody body = describeFileResponse.getBody();
|
||||
DescribeFileResponseBody.DescribeFileResponseBodyData data = body.getData();
|
||||
log.info("data:{}", data);
|
||||
return DescribeFileResponseBodyDataMode.builder()
|
||||
.categoryId(data.getCategoryId())
|
||||
.createTime(data.getCreateTime())
|
||||
.fileId(data.getFileId())
|
||||
.fileName(data.getFileName())
|
||||
.fileType(data.getFileType())
|
||||
.parser(data.getParser())
|
||||
.sizeInBytes(data.getSizeInBytes())
|
||||
.status(data.getStatus())
|
||||
.tags(data.getTags())
|
||||
.build();
|
||||
|
||||
} catch (TeaException error) {
|
||||
log.error("异常:「」", error);
|
||||
} catch (Exception _error) {
|
||||
log.error("异常:「」", _error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
package com.muyu.aliyun.bailian;
|
||||
|
||||
import com.muyu.aliyun.bailian.data.UploadFileModel;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
@Component
|
||||
@Log4j2
|
||||
public class TemporaryStorage {
|
||||
|
||||
public static void uploadFile(UploadFileModel uploadFileModel) {
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
// 创建URL对象
|
||||
URL url = new URL(uploadFileModel.getPreSignedUrl());
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
// 设置请求方法用于文档上传,需与您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Method字段的值一致
|
||||
connection.setRequestMethod(uploadFileModel.getMethod());
|
||||
|
||||
// 允许向connection输出,因为这个连接是用于上传文档的
|
||||
connection.setDoOutput(true);
|
||||
|
||||
connection.setRequestProperty("X-bailian-extra", uploadFileModel.getBailianExtra());
|
||||
connection.setRequestProperty("Content-Type", uploadFileModel.getContentType());
|
||||
|
||||
// 读取文档并通过连接上传
|
||||
try (DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
|
||||
FileInputStream fileInputStream = new FileInputStream(uploadFileModel.getFilePath())) {
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
||||
outStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
outStream.flush();
|
||||
}
|
||||
|
||||
// 检查响应
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
// 文档上传成功处理
|
||||
log.info("File uploaded successfully");
|
||||
} else {
|
||||
// 文档上传失败处理
|
||||
log.error("Failed to upload the file. ResponseCode: " + responseCode);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void uploadFileLink(UploadFileModel uploadFileModel) {
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
// 创建URL对象
|
||||
URL url = new URL(uploadFileModel.getPreSignedUrl());
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
// 设置请求方法用于文档上传,需与您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Method字段的值一致
|
||||
connection.setRequestMethod(uploadFileModel.getMethod());
|
||||
|
||||
// 允许向connection输出,因为这个连接是用于上传文档的
|
||||
connection.setDoOutput(true);
|
||||
|
||||
connection.setRequestProperty("X-bailian-extra", uploadFileModel.getBailianExtra());
|
||||
connection.setRequestProperty("Content-Type", uploadFileModel.getContentType());
|
||||
|
||||
URL sourceUrl = new URL(uploadFileModel.getSourceUrlString());
|
||||
HttpURLConnection sourceConnection = (HttpURLConnection) sourceUrl.openConnection();
|
||||
|
||||
// 设置访问OSS的请求方法为GET
|
||||
sourceConnection.setRequestMethod("GET");
|
||||
// 获取响应码,200表示请求成功
|
||||
int sourceFileResponseCode = sourceConnection.getResponseCode();
|
||||
if (sourceFileResponseCode != 200){
|
||||
throw new RuntimeException();
|
||||
}
|
||||
// 从OSS读取文档并通过连接上传
|
||||
if (sourceFileResponseCode != HttpURLConnection.HTTP_OK){
|
||||
throw new RuntimeException("Failed to get source file.");
|
||||
}
|
||||
try (DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
|
||||
InputStream in = new BufferedInputStream(sourceConnection.getInputStream())) {
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
outStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
outStream.flush();
|
||||
}
|
||||
|
||||
// 检查响应
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
// 文档上传成功
|
||||
System.out.println("File uploaded successfully.");
|
||||
} else {
|
||||
// 文档上传失败
|
||||
System.out.println("Failed to upload the file. ResponseCode: " + responseCode);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.muyu.aliyun.bailian.config;
|
||||
import com.aliyun.bailian20231229.Client;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.muyu.aliyun.config.OssConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class AlibabaCloudClientConfig {
|
||||
|
||||
@Autowired
|
||||
private OssConfig ossConfig;
|
||||
|
||||
/**
|
||||
* 创建 Client Bean
|
||||
* @return Client 对象
|
||||
* @throws Exception 创建过程中可能抛出的异常
|
||||
*/
|
||||
@Bean
|
||||
public Client createClient() throws Exception {
|
||||
Config config = new Config()
|
||||
.setAccessKeyId(ossConfig.getAccessKeyId())
|
||||
.setAccessKeySecret(ossConfig.getAccessKeySecret());
|
||||
|
||||
config.endpoint = ossConfig.getEndpoint();
|
||||
return new Client(config);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.muyu.aliyun.bailian.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class FileUploadLeaseMode {
|
||||
|
||||
/**
|
||||
* CategoryId字段用于存储文件的类别ID
|
||||
* 这个ID有助于对文件进行分类管理
|
||||
*/
|
||||
private String CategoryId;
|
||||
|
||||
/**
|
||||
* FileName字段用于存储文件的名称
|
||||
* 它帮助用户识别和搜索特定的文件
|
||||
*/
|
||||
private String FileName;
|
||||
|
||||
/**
|
||||
* Md5字段用于存储文件的MD5哈希值
|
||||
* 这个值用于验证文件的完整性,确保文件未被篡改
|
||||
*/
|
||||
private String Md5;
|
||||
|
||||
/**
|
||||
* SizeInBytes字段用于存储文件的大小,以字节为单位
|
||||
* 这个信息有助于用户了解文件的存储空间占用情况
|
||||
*/
|
||||
private String SizeInBytes;
|
||||
|
||||
/**
|
||||
* WorkspaceId字段用于存储文件所属的工作空间ID
|
||||
* 这个ID用于确定文件属于哪个工作空间,有助于进行权限管理和访问控制
|
||||
*/
|
||||
private String WorkspaceId;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.muyu.aliyun.bailian.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UploadDataManagementMode {
|
||||
|
||||
/**
|
||||
* 工作空间的唯一标识符
|
||||
*
|
||||
* 用于在系统中唯一标识一个工作空间,便于在不同模块和功能中引用和操作特定的工作空间
|
||||
*/
|
||||
private String WorkspaceId;
|
||||
/**
|
||||
* 租约的标识符
|
||||
*
|
||||
* 用于在系统中唯一标识一个租约,便于在不同模块和功能中引用和操作特定的租约
|
||||
*/
|
||||
private String LeaseId;
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.muyu.aliyun.bailian.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UploadDescribeFileMode {
|
||||
|
||||
/**
|
||||
* 工作空间标识符
|
||||
* 用于唯一标识一个工作空间,在处理与工作空间相关的操作时使用
|
||||
*/
|
||||
private String WorkspaceId;
|
||||
|
||||
/**
|
||||
* 文件标识符
|
||||
* 用于唯一标识一个文件,在处理与文件相关的操作时使用
|
||||
*/
|
||||
private String FileId;
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.muyu.aliyun.bailian.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UploadFileModel {
|
||||
|
||||
/**
|
||||
* 用于存储预签名的URL,该URL用于访问存储服务中的对象
|
||||
*/
|
||||
private String preSignedUrl;
|
||||
|
||||
/**
|
||||
* 用于存储文件在本地的路径
|
||||
*/
|
||||
private String filePath;
|
||||
|
||||
/**
|
||||
* 用于存储HTTP请求的方法,例如GET、POST等
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
* 用于存储百链额外信息,可能包括与百链系统相关的特定参数或元数据
|
||||
*/
|
||||
private String bailianExtra;
|
||||
|
||||
/**
|
||||
* 用于存储文件的内容类型,例如text/plain、image/jpeg等
|
||||
*/
|
||||
private String contentType;
|
||||
|
||||
/**
|
||||
* 用于存储源URL字符串,即文件原始位置的URL
|
||||
*/
|
||||
private String sourceUrlString;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.muyu.aliyun.bailian.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UploadPathMode {
|
||||
|
||||
// 用于存储预签名URL或HTTP URL,以便进行后续的文件上传或下载操作
|
||||
private String preSignedUrlOrHttpUrl;
|
||||
// 本地文件路径,用于指定需要上传的文件位置或下载的文件保存位置
|
||||
private String filePath;
|
||||
// 服务器提供的URL,用于构建预签名URL或作为HTTP请求的地址
|
||||
private String Url;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.muyu.aliyun.bailian.data.fixed;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AddFileResponseBodyDataMode {
|
||||
/**
|
||||
* 文件ID,用于唯一标识一个文件
|
||||
*/
|
||||
public String fileId;
|
||||
|
||||
/**
|
||||
* 解析器标识,用于指示哪个解析器将用于处理文件
|
||||
*/
|
||||
public String parser;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.muyu.aliyun.bailian.data.fixed;
|
||||
|
||||
import com.aliyun.tea.NameInMap;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class DescribeFileResponseBodyDataMode {
|
||||
|
||||
/**
|
||||
* 文件类别ID,用于标识文件的类别
|
||||
*/
|
||||
public String categoryId;
|
||||
|
||||
/**
|
||||
* 创建时间,记录文件何时被创建
|
||||
*/
|
||||
public String createTime;
|
||||
|
||||
/**
|
||||
* 文件ID,唯一标识一个文件
|
||||
*/
|
||||
public String fileId;
|
||||
|
||||
/**
|
||||
* 文件名,存储文件的名称
|
||||
*/
|
||||
public String fileName;
|
||||
|
||||
/**
|
||||
* 文件类型,描述文件的格式或种类
|
||||
*/
|
||||
public String fileType;
|
||||
|
||||
/**
|
||||
* 解析器类型,标识用于解析文件的工具或方法
|
||||
*/
|
||||
public String parser;
|
||||
|
||||
/**
|
||||
* 文件大小,以字节为单位
|
||||
*/
|
||||
public Long sizeInBytes;
|
||||
|
||||
/**
|
||||
* 文件状态,描述文件的当前状态(如已上传、处理中等)
|
||||
*/
|
||||
public String status;
|
||||
|
||||
/**
|
||||
* 标签列表,用于标识文件的关键字或类别
|
||||
*/
|
||||
public List<String> tags;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.aliyun.bailian.data.fixed;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UploadLeaseParamsMode {
|
||||
|
||||
/**
|
||||
* 请求头信息
|
||||
* 存储HTTP请求的头部信息,如内容类型、认证信息等
|
||||
*/
|
||||
private String Headers;
|
||||
|
||||
/**
|
||||
* 请求方法
|
||||
* 存储HTTP请求所使用的 方法,如GET、POST等
|
||||
*/
|
||||
private String Method;
|
||||
|
||||
/**
|
||||
* 请求URL
|
||||
* 存储HTTP请求的目标URL,即请求的地址
|
||||
*/
|
||||
private String Url;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.muyu.aliyun.config;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "oss")
|
||||
public class OssConfig {
|
||||
/**
|
||||
* OSS服务的终端节点,用于指定OSS服务的接入地址
|
||||
*/
|
||||
private String endpoint;
|
||||
|
||||
/**
|
||||
* 阿里云访问密钥ID,用于身份验证
|
||||
*/
|
||||
private String accessKeyId;
|
||||
|
||||
/**
|
||||
* 阿里云访问密钥,与accessKeyId一起用于身份验证
|
||||
*/
|
||||
private String accessKeySecret;
|
||||
|
||||
/**
|
||||
* OSS服务中的存储空间名称
|
||||
*/
|
||||
private String bucketName;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.muyu.aliyun.controller;
|
||||
|
||||
import com.muyu.aliyun.service.KnowledgeBaseUploadService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/ai/industry/report")
|
||||
public class KnowledgeBaseUploadController {
|
||||
|
||||
@Autowired
|
||||
private KnowledgeBaseUploadService knowledgeBaseUploadService;
|
||||
|
||||
@PostMapping("/KnowledgeBaseUpload")
|
||||
public void uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
String workspaceId = "llm-98lxu358qfvxdxvt";
|
||||
String categoryId = "cate_997567e8a7e94f4caab94e78c580adb4_10062067";
|
||||
knowledgeBaseUploadService.uploadDocument(file, workspaceId, categoryId);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.muyu.aliyun.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface KnowledgeBaseUploadService {
|
||||
void uploadDocument(MultipartFile file, String workspaceId, String categoryId);
|
||||
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
package com.muyu.aliyun.service.impl;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.muyu.aliyun.bailian.AddDataManagement;
|
||||
import com.muyu.aliyun.bailian.ApplyLease;
|
||||
import com.muyu.aliyun.bailian.ReviewResolution;
|
||||
import com.muyu.aliyun.bailian.TemporaryStorage;
|
||||
import com.muyu.aliyun.bailian.data.FileUploadLeaseMode;
|
||||
import com.muyu.aliyun.bailian.data.UploadDataManagementMode;
|
||||
import com.muyu.aliyun.bailian.data.UploadDescribeFileMode;
|
||||
import com.muyu.aliyun.bailian.data.UploadFileModel;
|
||||
import com.muyu.aliyun.bailian.data.fixed.AddFileResponseBodyDataMode;
|
||||
import com.muyu.aliyun.bailian.data.fixed.DescribeFileResponseBodyDataMode;
|
||||
import com.muyu.aliyun.bailian.data.fixed.UploadLeaseParamsMode;
|
||||
import com.muyu.aliyun.config.OssConfig;
|
||||
import com.muyu.aliyun.service.KnowledgeBaseUploadService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.muyu.aliyun.bailian.Md5Utils.getFileMd5;
|
||||
|
||||
|
||||
@Service
|
||||
public class KnowledgeBaseUploadServiceImpl implements KnowledgeBaseUploadService {
|
||||
|
||||
@Autowired
|
||||
private ApplyLease applyLease;
|
||||
|
||||
@Autowired
|
||||
private TemporaryStorage temporaryStorage;
|
||||
|
||||
|
||||
@Autowired
|
||||
private AddDataManagement addDataManagement;
|
||||
|
||||
@Autowired
|
||||
private ReviewResolution reviewResolution;
|
||||
|
||||
@Override
|
||||
public void uploadDocument(MultipartFile file, String workspaceId, String categoryId) {
|
||||
try {
|
||||
// 1. 保存临时文件
|
||||
File tempFile = convertMultipartFileToFile(file);
|
||||
// 获取文件大小
|
||||
long sizeInBytes = tempFile.length();
|
||||
|
||||
// 2. 计算文件 MD5
|
||||
String md5 = getFileMd5(tempFile.getAbsolutePath());
|
||||
|
||||
// 3. 申请文档上传租约
|
||||
FileUploadLeaseMode fileUploadLeaseModel = new FileUploadLeaseMode();
|
||||
fileUploadLeaseModel.setWorkspaceId(workspaceId);
|
||||
fileUploadLeaseModel.setCategoryId(categoryId);
|
||||
fileUploadLeaseModel.setFileName(file.getOriginalFilename());
|
||||
fileUploadLeaseModel.setMd5(md5);
|
||||
fileUploadLeaseModel.setSizeInBytes(String.valueOf(sizeInBytes));
|
||||
|
||||
UploadLeaseParamsMode leaseParams = applyLease.applyFileUploadLease(fileUploadLeaseModel);
|
||||
if (leaseParams == null) {
|
||||
throw new RuntimeException("申请文档上传租约失败");
|
||||
}
|
||||
|
||||
|
||||
// 4. 上传文档至百炼的临时存储
|
||||
UploadFileModel uploadFileModel = new UploadFileModel();
|
||||
uploadFileModel.setPreSignedUrl(leaseParams.getUrl());
|
||||
uploadFileModel.setMethod(leaseParams.getMethod());
|
||||
// 这里需要根据实际情况设置 X-bailian-extra 和 Content-Type
|
||||
uploadFileModel.setBailianExtra("");
|
||||
uploadFileModel.setContentType("");
|
||||
uploadFileModel.setFilePath(tempFile.getAbsolutePath());
|
||||
temporaryStorage.uploadFile(uploadFileModel);
|
||||
|
||||
// 5. 将文档添加至百炼的数据管理
|
||||
UploadDataManagementMode uploadDataManagementMode = new UploadDataManagementMode();
|
||||
uploadDataManagementMode.setWorkspaceId(workspaceId);
|
||||
// 这里需要设置 LeaseId,假设 LeaseId 在 UploadLeaseParamsMode 中有对应的 getter
|
||||
uploadDataManagementMode.setLeaseId("");
|
||||
AddFileResponseBodyDataMode addFileResult = addDataManagement.addFile(uploadDataManagementMode);
|
||||
if (addFileResult == null) {
|
||||
throw new RuntimeException("将文档添加至百炼的数据管理失败");
|
||||
}
|
||||
|
||||
// 6. 查看文档解析状态
|
||||
UploadDescribeFileMode uploadDescribeFileMode = new UploadDescribeFileMode();
|
||||
uploadDescribeFileMode.setWorkspaceId(workspaceId);
|
||||
uploadDescribeFileMode.setFileId(addFileResult.getFileId());
|
||||
DescribeFileResponseBodyDataMode describeFileResult = reviewResolution.DescribeFile(uploadDescribeFileMode);
|
||||
if (describeFileResult == null) {
|
||||
throw new RuntimeException("查看文档解析状态失败");
|
||||
}
|
||||
|
||||
// 清理临时文件
|
||||
tempFile.delete();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private File convertMultipartFileToFile(MultipartFile file) throws IOException {
|
||||
Path tempFilePath = Files.createTempFile(null, null);
|
||||
File tempFile = tempFilePath.toFile();
|
||||
file.transferTo(tempFile);
|
||||
return tempFile;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
oss:
|
||||
internal:
|
||||
endpoint: https://oss-cn-hangzhou-internal.aliyuncs.com
|
||||
endpoint: https://oss-cn-hangzhou.aliyuncs.com
|
||||
accessKeyId: LTAI5tLvo45NpqjoH11G1beU
|
||||
accessKeySecret: NjfA4hBwban4W8Db7RBwDJd0pBHJeM
|
||||
bucketName: idicc-pangu-prod
|
Loading…
Reference in New Issue