|
|
@@ -0,0 +1,114 @@
|
|
|
+package com.yushu.wcyys.util;
|
|
|
+
|
|
|
+import com.aliyun.oss.*;
|
|
|
+import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
|
|
+import com.aliyun.oss.model.*;
|
|
|
+import com.yushu.wcyys.config.OSSConfig;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.PreDestroy;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class OSSService {
|
|
|
+ private final OSS ossClient;
|
|
|
+ private final String bucketName;
|
|
|
+ private final int defaultExpiration;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public OSSService(OSSConfig ossConfig) {
|
|
|
+ this.bucketName = ossConfig.getBucketName();
|
|
|
+ this.defaultExpiration = ossConfig.getDefaultExpiration();
|
|
|
+
|
|
|
+ this.ossClient = new OSSClientBuilder().build(
|
|
|
+ ossConfig.getEndpoint(),
|
|
|
+ new DefaultCredentialProvider(
|
|
|
+ ossConfig.getAccessKeyId(),
|
|
|
+ ossConfig.getAccessKeySecret()),
|
|
|
+ null
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ * @param objectName OSS文件路径
|
|
|
+ * @param file 要上传的文件
|
|
|
+ * @return 文件的URL(上传成功时)
|
|
|
+ */
|
|
|
+ public String uploadFile(String objectName, MultipartFile file) {
|
|
|
+ try (InputStream inputStream = file.getInputStream()) {
|
|
|
+ // 上传文件到OSS
|
|
|
+ ossClient.putObject(bucketName, objectName, inputStream);
|
|
|
+ // 上传成功,生成并返回文件的URL
|
|
|
+ return generatePresignedUrl(objectName);
|
|
|
+ } catch (OSSException | ClientException | IOException e) {
|
|
|
+ e.printStackTrace(); // 打印异常堆栈
|
|
|
+ return null; // 上传失败时返回null
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载文件
|
|
|
+ * @param objectName OSS文件路径
|
|
|
+ * @return 文件输入流
|
|
|
+ * @throws IOException 如果文件下载失败
|
|
|
+ */
|
|
|
+ public InputStream downloadFile(String objectName) throws IOException {
|
|
|
+ try {
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|
|
|
+ return ossObject.getObjectContent();
|
|
|
+ } catch (OSSException | ClientException e) {
|
|
|
+ throw new IOException("文件下载失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成预签名URL(使用默认有效期)
|
|
|
+ */
|
|
|
+ public String generatePresignedUrl(String objectName) {
|
|
|
+ return generatePresignedUrl(objectName, defaultExpiration);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成预签名URL(自定义有效期)
|
|
|
+ */
|
|
|
+ public String generatePresignedUrl(String objectName, int expirationSeconds) {
|
|
|
+ GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(
|
|
|
+ bucketName,
|
|
|
+ objectName,
|
|
|
+ HttpMethod.GET
|
|
|
+ );
|
|
|
+ request.setExpiration(new Date(
|
|
|
+ System.currentTimeMillis() + expirationSeconds * 1000
|
|
|
+ ));
|
|
|
+ return ossClient.generatePresignedUrl(request).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文件
|
|
|
+ * @param objectName OSS文件路径
|
|
|
+ * @return 是否删除成功
|
|
|
+ */
|
|
|
+ public boolean deleteFile(String objectName) {
|
|
|
+ try {
|
|
|
+ ossClient.deleteObject(bucketName, objectName);
|
|
|
+ return true;
|
|
|
+ } catch (OSSException | ClientException e) {
|
|
|
+ e.printStackTrace(); // 打印异常堆栈
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭OSS客户端(建议通过@PreDestroy触发)
|
|
|
+ */
|
|
|
+ @PreDestroy
|
|
|
+ public void shutdown() {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|