Browse Source

设置华为云OBS

bmmx 1 year ago
parent
commit
684432d636

+ 172 - 0
src/main/java/com/om/utils/HuaweiObsUtil.java

@@ -0,0 +1,172 @@
+package com.om.utils;
+
+import com.alibaba.fastjson.JSONObject;
+import com.obs.services.ObsClient;
+import com.obs.services.model.HeaderResponse;
+import com.obs.services.model.HttpMethodEnum;
+import com.obs.services.model.TemporarySignatureRequest;
+import com.obs.services.model.TemporarySignatureResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+
+@Slf4j
+@Component
+
+public class HuaweiObsUtil {
+
+    //Access Key Id
+    @Value("${obs.ak}")
+    private String ak;
+
+    //Secret Access Key
+    @Value("${obs.sk}")
+    private String sk;
+
+    //桶名称
+    @Value("${obs.bucketName}")
+    private String bucketName;
+
+    // 终端节点访问Endpoint
+    @Value("${obs.endpoint}")
+    private String endpoint;
+
+    // 访问域名 在域名后面或文件目录前加“/”
+    @Value("${obs.path}")
+    private String path;
+
+    /**
+     * 文件上传
+     *
+     * @param file 要上传的文件
+     * @return 文件在OBS中的URL
+     * @throws IOException IO异常
+     */
+    public String upload(MultipartFile file,String prifix) throws IOException {
+        ObsClient obsClient = null;
+        try {
+            String originalFilename = file.getOriginalFilename();
+            // 拼接文件在OBS中的路径
+            String objectName = prifix + "/"+ originalFilename;
+            // 初始化OBS客户端
+            obsClient = new ObsClient(ak, sk, endpoint);
+            // 上传文件到OBS
+            HeaderResponse response = obsClient.putObject(bucketName, objectName, file.getInputStream());
+            log.info(JSONObject.toJSONString(response));
+            // 判断上传是否成功
+            int statusCode = response.getStatusCode();
+            if (200 == statusCode) {
+                // 拼接文件在OBS中的URL并返回
+                String objectUrl = "https://"+path + "/" + objectName;
+                return objectUrl;
+            }
+        } finally {
+            // 关闭OBS客户端
+            obsClient.close();
+        }
+        return null;
+    }
+
+
+
+
+
+    /**
+     * 下载文件
+     *
+     * @param fileName 文件名称
+     * @param fileType 文件路径
+     * @return
+     */
+    public String getDownloadUrl(String fileName, FileType fileType) {
+        ObsClient obsClient = null;
+        obsClient = new ObsClient(ak, sk, endpoint);
+        // URL有效期,3600秒.5分钟
+        long expireSeconds = 3600L;
+        String objectName = fileType.getType().concat("/").concat(fileName);
+        TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
+        request.setBucketName(bucketName);
+        request.setObjectKey(objectName);
+        TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
+        return response.getSignedUrl();
+    }
+
+    /**
+     * 获取上传地址
+     *
+     * @param fileName 文件名称
+     * @param fileType 文件路径
+     * @return
+     */
+    public String getUploadUrl(String fileName, FileType fileType) {
+        try {
+            // 创建ObsClient实例
+            ObsClient obsClient = new ObsClient(ak, sk, endpoint);
+            // URL有效期,3600秒
+            long expireSeconds = 3600L;
+            String objectName = fileType.getType().concat("/").concat(fileName);
+            TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
+            request.setBucketName(bucketName);
+            request.setObjectKey(objectName);
+            TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
+            return response.getSignedUrl();
+        } catch (Exception e) {
+            log.error("获取上传地址异常:{}", e.getMessage(), e);
+        }
+        return null;
+    }
+
+
+
+    /**
+     * 上传视频
+     *
+     * @param fileName 文件名称
+     * @param fileType 文件路径
+     * @return
+     */
+    public boolean uploadFileVideo(String fileName, FileType fileType, InputStream is) {
+        try {
+            String objectName = fileType.getType().concat("/").concat(fileName);
+            ObsClient obsClient = new ObsClient(ak, sk, endpoint);
+            // 添加 ContentType (添加后可在浏览器中直接浏览,而非下载链接)
+            obsClient.putObject(bucketName, objectName, is);
+            obsClient.close();
+            return true;
+        } catch (Exception e) {
+            log.error("上传视频文件异常:{}", e.getMessage(), e);
+        }
+        return false;
+    }
+
+
+
+
+    public enum FileType {
+        TEST("test", "测试");
+
+        private String type;
+        private String desc;
+
+        FileType(String type, String desc) {
+            this.type = type;
+            this.desc = desc;
+        }
+
+        public String getType() {
+            return type;
+        }
+
+        public String getDesc() {
+            return desc;
+        }
+    }
+}
+

+ 7 - 0
src/main/resources/application-dev.yml

@@ -45,3 +45,10 @@ files:
 photo:
   tmp: C:/temp/Photo/
 
+
+obs:
+  ak: FL8POSPAPQDTW42RL8SN
+  sk: XQTIcmKTZqxPfwYnINySG0LRUthy0iRBFqDdTnQ0
+  bucketName: wl-resource
+  endpoint: obs.cn-south-1.myhuaweicloud.com
+  path: wl-resource.obs.cn-south-1.myhuaweicloud.com

+ 38 - 0
src/test/java/com/feng/FinalTest.java

@@ -0,0 +1,38 @@
+package com.feng;
+
+
+import cn.hutool.core.bean.BeanUtil;
+import org.junit.jupiter.api.Test;
+
+public class FinalTest {
+
+    @Override
+    public boolean equals(Object obj) {
+        return super.equals(obj);
+    }
+
+    @Test
+    public void eqlAndD(){
+        Girl g = new Girl("1");
+        Girl f = new Girl("1");
+        Girl h = new Girl("h");
+        String s1 = "1";
+        String s2 = "1";
+        System.out.println(s1.equals(s2));
+        System.out.println(g);
+        System.out.println(f);
+
+        System.out.println(g == f);
+        System.out.println(g.equals(f));
+        System.out.println(g.equals(h));
+
+    }
+    class Girl{
+        String name;
+        public Girl(String name){
+            this.name = name;
+        }
+    }
+
+
+}

+ 20 - 0
src/test/java/com/om/ObsTest.java

@@ -0,0 +1,20 @@
+package com.om;
+
+import com.om.utils.HuaweiObsUtil;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.annotation.Resource;
+
+@SpringBootTest(classes = com.om.OperationApplication.class)
+public class ObsTest {
+
+    @Resource
+    private HuaweiObsUtil huaweiObsUtil;
+
+    @Test
+    public void UploadTest(){
+        String uploadUrl = huaweiObsUtil.getUploadUrl("1122.jpg", HuaweiObsUtil.FileType.TEST);
+        System.out.println(uploadUrl);
+    }
+}