【新增】新增OSS-SDK
This commit is contained in:
@@ -42,6 +42,7 @@ dependencies {
|
||||
annotationProcessor 'org.projectlombok:lombok' // Lombok 注解处理
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test' // 测试框架
|
||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' // thymeleaf模版引擎
|
||||
implementation 'com.aliyun.oss:aliyun-sdk-oss:3.15.1' // OSS SDK
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
|
||||
86
src/main/java/com/corewing/app/util/OSSUploadUtil.java
Normal file
86
src/main/java/com/corewing/app/util/OSSUploadUtil.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package com.corewing.app.util;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Component
|
||||
public class OSSUploadUtil {
|
||||
|
||||
private static final String ENDPOINT = "oss-cn-shenzhen.aliyuncs.com"; // 地域节点
|
||||
private static final String ACCESS_KEY_ID = "your-access-key-id"; // 替换为你的 AccessKey ID
|
||||
private static final String ACCESS_KEY_SECRET = "your-access-key-secret"; // 替换为你的 AccessKey Secret
|
||||
private static final String BUCKET_NAME = "corewing-app"; // 替换为你的 Bucket 名称
|
||||
|
||||
/**
|
||||
* 上传文件到 OSS
|
||||
* @param inputStream 文件输入流
|
||||
* @param objectName OSS 中存储的文件路径(如 "firmware/20231104/update.bin")
|
||||
* @return 上传成功后的文件 URL
|
||||
*/
|
||||
public static String uploadFile(InputStream inputStream, String objectName) {
|
||||
// 创建 OSS 客户端实例
|
||||
OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
||||
|
||||
try {
|
||||
// 设置文件元数据(可选,如 Content-Type)
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentType(getContentType(objectName)); // 自动识别文件类型
|
||||
|
||||
// 上传文件
|
||||
PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, objectName, inputStream, metadata);
|
||||
PutObjectResult result = ossClient.putObject(request);
|
||||
|
||||
// 生成文件访问 URL(公网访问需 Bucket 设为公共读)
|
||||
return String.format("https://%s.%s/%s", BUCKET_NAME, ENDPOINT, objectName);
|
||||
} finally {
|
||||
// 关闭 OSS 客户端,释放资源
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传本地文件到 OSS
|
||||
* @param localFilePath 本地文件路径(如 "D:/firmware/update.bin")
|
||||
* @param objectName OSS 中存储的文件路径
|
||||
* @return 上传成功后的文件 URL
|
||||
*/
|
||||
public static String uploadLocalFile(String localFilePath, String objectName) {
|
||||
OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
||||
try {
|
||||
File file = new File(localFilePath);
|
||||
ossClient.putObject(BUCKET_NAME, objectName, file);
|
||||
return String.format("https://%s.%s/%s", BUCKET_NAME, ENDPOINT, objectName);
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件名获取 Content-Type
|
||||
* @param fileName 文件名(如 "update.bin")
|
||||
* @return Content-Type
|
||||
*/
|
||||
private static String getContentType(String fileName) {
|
||||
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
||||
switch (suffix) {
|
||||
case "bin": return "application/octet-stream";
|
||||
case "jpg": case "jpeg": return "image/jpeg";
|
||||
case "png": return "image/png";
|
||||
case "txt": return "text/plain";
|
||||
// 其他文件类型可自行扩展
|
||||
default: return "application/octet-stream";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user