|
|
@@ -5,6 +5,7 @@ import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
import com.aliyun.oss.model.ObjectMetadata;
|
|
|
import com.aliyun.oss.model.PutObjectRequest;
|
|
|
@@ -14,6 +15,7 @@ import jakarta.annotation.PostConstruct;
|
|
|
import lombok.Data;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -28,6 +30,7 @@ import java.time.LocalDateTime;
|
|
|
* @author haoxr
|
|
|
* @since 2.3.0
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Component
|
|
|
@ConditionalOnProperty(value = "oss.type", havingValue = "aliyun")
|
|
|
@ConfigurationProperties(prefix = "oss.aliyun")
|
|
|
@@ -55,13 +58,21 @@ public class AliyunFileService implements FileService {
|
|
|
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
- aliyunOssClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ try {
|
|
|
+ aliyunOssClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ log.info("阿里云OSS客户端初始化成功,endpoint: {}, bucketName: {}", endpoint, bucketName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("阿里云OSS客户端初始化失败: ", e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@SneakyThrows
|
|
|
public FileInfo uploadFile(MultipartFile file) {
|
|
|
-
|
|
|
+ // 检查客户端是否初始化成功
|
|
|
+ if (aliyunOssClient == null) {
|
|
|
+ throw new RuntimeException("阿里云OSS客户端未正确初始化,请检查配置");
|
|
|
+ }
|
|
|
// 获取文件名称
|
|
|
String originalFilename = file.getOriginalFilename();
|
|
|
// 生成文件名(日期文件夹)
|
|
|
@@ -78,8 +89,10 @@ public class AliyunFileService implements FileService {
|
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream, metadata);
|
|
|
// 上传文件
|
|
|
aliyunOssClient.putObject(putObjectRequest);
|
|
|
+ log.info("文件上传成功: {}", fileName);
|
|
|
} catch (Exception e) {
|
|
|
- throw new RuntimeException("文件上传失败");
|
|
|
+ log.error("文件上传失败: ", e);
|
|
|
+ throw new RuntimeException("文件上传失败: " + e.getMessage());
|
|
|
}
|
|
|
// 获取文件访问路径
|
|
|
String fileUrl = "https://" + bucketName + "." + endpoint + "/" + fileName;
|
|
|
@@ -91,10 +104,16 @@ public class AliyunFileService implements FileService {
|
|
|
|
|
|
@Override
|
|
|
public boolean deleteFile(String filePath) {
|
|
|
- Assert.notBlank(filePath, "删除文件路径不能为空");
|
|
|
- String fileHost = "https://" + bucketName + "." + endpoint; // 文件主机域名
|
|
|
- String fileName = filePath.substring(fileHost.length() + 1); // +1 是/占一个字符,截断左闭右开
|
|
|
- aliyunOssClient.deleteObject(bucketName, fileName);
|
|
|
- return true;
|
|
|
+ try {
|
|
|
+ Assert.notBlank(filePath, "删除文件路径不能为空");
|
|
|
+ String fileHost = "https://" + bucketName + "." + endpoint; // 文件主机域名
|
|
|
+ String fileName = filePath.substring(fileHost.length() + 1); // +1 是/占一个字符,截断左闭右开
|
|
|
+ aliyunOssClient.deleteObject(bucketName, fileName);
|
|
|
+ log.info("文件删除成功: {}", fileName);
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("文件删除失败: ", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
}
|