Browse Source

first commit

ys 6 months ago
commit
e20d500e22

+ 17 - 0
阿里oss云存储/OSSConfig.java

@@ -0,0 +1,17 @@
+package com.yushu.wcyys.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "aliyun.oss")
+@Data
+//阿里云配置类
+public class OSSConfig {
+    private String endpoint;
+    private String accessKeyId;
+    private String accessKeySecret;
+    private String bucketName;
+    private int defaultExpiration = 3600;
+}

+ 114 - 0
阿里oss云存储/OSSService.java

@@ -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();
+        }
+    }
+}

+ 10 - 0
阿里oss云存储/yml配置.txt

@@ -0,0 +1,10 @@
+#阿里云配置
+aliyun:
+  oss:
+    #地址 
+    endpoint:oss-cn-guangzhou.aliyuncs.com #广州节点
+    access-key-id: 
+    access-key-secret: 
+    bucket-name: 
+    #超时时间
+    default-expiration:  604800 # 7天