From 5e07f160bb34e72186607556fff8841688a1f47a Mon Sep 17 00:00:00 2001
From: baizonghao <1719256278@qq.com>
Date: 星期六, 04 三月 2023 00:01:09 +0800
Subject: [PATCH] 阿里云测试

---
 ycl-common/src/main/java/com/ycl/controller/FileController.java  |   31 ++++++++-
 ycl-smoke/src/main/resources/application.yml                     |    2 
 ycl-platform/src/main/resources/application-dev.yml              |    9 ++
 ycl-platform/src/main/resources/application-prod.yml             |    2 
 ycl-platform/src/main/java/com/ycl/dto/video/AlarmDataParam.java |    2 
 pom.xml                                                          |   12 ++++
 ycl-common/src/main/java/com/ycl/utils/AliyunUtils.java          |   81 +++++++++++++++++++++++++++
 7 files changed, 129 insertions(+), 10 deletions(-)

diff --git a/pom.xml b/pom.xml
index 64fefc5..5b27209 100644
--- a/pom.xml
+++ b/pom.xml
@@ -196,6 +196,18 @@
             <artifactId>commons-logging</artifactId>
             <version>1.2</version>
         </dependency>
+
+
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+            <version>3.16.1</version>
+        </dependency>
+        <dependency>
+            <groupId>joda-time</groupId>
+            <artifactId>joda-time</artifactId>
+            <version>2.12.2</version>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/ycl-common/src/main/java/com/ycl/controller/FileController.java b/ycl-common/src/main/java/com/ycl/controller/FileController.java
index f0f1623..73eeb9e 100644
--- a/ycl-common/src/main/java/com/ycl/controller/FileController.java
+++ b/ycl-common/src/main/java/com/ycl/controller/FileController.java
@@ -3,6 +3,7 @@
 import com.ycl.api.CommonResult;
 import com.ycl.dto.media.Media;
 import com.ycl.dto.media.PictureZoomParameter;
+import com.ycl.utils.AliyunUtils;
 import com.ycl.utils.MediaFileUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -10,12 +11,17 @@
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.InputStream;
+
 @RestController
 @Api(tags = "鏂囦欢涓婁紶")
 @RequestMapping("/file")
 public class FileController {
 
     private MediaFileUtil mediaFileUtil;
+
+    @Autowired
+    private AliyunUtils aliyunUtils;
 
     @Autowired
     public void setMediaFileUtil(MediaFileUtil mediaFileUtil) {
@@ -29,17 +35,32 @@
      * @return
      */
 
+//    @ApiOperation("涓婁紶鍥剧墖")
+//    @RequestMapping(value = "/medias", method = RequestMethod.POST)
+//    public CommonResult<Media> mediaUpload(@RequestParam("file") MultipartFile file) {
+//        try {
+//            PictureZoomParameter zoomPar = PictureZoomParameter.getBoardPar();
+//            Media res = mediaFileUtil.save(file, zoomPar);
+//
+//            return CommonResult.success(res, "ok");
+//        } catch (Exception e) {
+//            return CommonResult.failed("鍥剧墖鏍煎紡鍙兘涓簀pg銆乯peg銆乸ng,鍙慨鏀规枃浠剁殑鍚庣紑鏃犳晥!");
+//        }
+//    }
+
     @ApiOperation("涓婁紶鍥剧墖")
     @RequestMapping(value = "/medias", method = RequestMethod.POST)
-    public CommonResult<Media> mediaUpload(@RequestParam("file") MultipartFile file) {
+    public CommonResult<String> mediaUpload(@RequestParam("file") MultipartFile file) {
         try {
-            PictureZoomParameter zoomPar = PictureZoomParameter.getBoardPar();
-            Media res = mediaFileUtil.save(file, zoomPar);
-
+            InputStream inputStream = file.getInputStream();
+            String orginalFileName = file.getOriginalFilename();
+            String res = aliyunUtils.upload(inputStream, orginalFileName);
             return CommonResult.success(res, "ok");
         } catch (Exception e) {
-            return CommonResult.failed("鍥剧墖鏍煎紡鍙兘涓簀pg銆乯peg銆乸ng,鍙慨鏀规枃浠剁殑鍚庣紑鏃犳晥!");
+
+            e.printStackTrace();
         }
+        return CommonResult.failed("鍥剧墖鏍煎紡鍙兘涓簀pg銆乯peg銆乸ng,鍙慨鏀规枃浠剁殑鍚庣紑鏃犳晥!");
     }
 
     @ApiOperation("鍒犻櫎鍥剧墖")
diff --git a/ycl-common/src/main/java/com/ycl/utils/AliyunUtils.java b/ycl-common/src/main/java/com/ycl/utils/AliyunUtils.java
new file mode 100644
index 0000000..fa3b84e
--- /dev/null
+++ b/ycl-common/src/main/java/com/ycl/utils/AliyunUtils.java
@@ -0,0 +1,81 @@
+package com.ycl.utils;
+
+import com.aliyun.oss.ClientException;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.OSSException;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.io.InputStream;
+import java.util.UUID;
+
+@Component
+public class AliyunUtils {
+    private static String endpoint;
+    private static String keyId;
+    private static String keySecret;
+    private static String bucketName;
+
+    @Value("${aliyun.oss.endpoint}")
+    public void setEndpoint(String endpoint) {
+        AliyunUtils.endpoint = endpoint;
+    }
+    @Value("${aliyun.oss.keyId}")
+    public void setKeyId(String keyId) {
+        AliyunUtils.keyId = keyId;
+    }
+    @Value("${aliyun.oss.keySecret}")
+    public void setKeySecret(String keySecret) {
+        AliyunUtils.keySecret = keySecret;
+    }
+    @Value("${aliyun.oss.bucketName}")
+    public void setBucketName(String bucketName) {
+        AliyunUtils.bucketName = bucketName;
+    }
+
+    public static String upload(InputStream inputStream, String orginalFileName){
+        // 鍒涘缓OSSClient瀹炰緥銆�
+        OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
+        // 鏂囦欢鍚�
+        String fileName = UUID.randomUUID().toString();
+        //鏂囦欢鎵╁睍鍚�
+        String fileExtention = orginalFileName.substring(orginalFileName.lastIndexOf("."));
+        //鏈�缁堢殑璺緞 绫讳技avatar/2021/12/05/xxxxxxxxx.jpg
+        String objectName = fileName+fileExtention;
+        ossClient.putObject(bucketName, objectName, inputStream);
+        // 鍏抽棴OSSClient銆�
+        ossClient.shutdown();
+        return "https://"+bucketName+"."+endpoint+"/"+objectName;
+    }
+
+    public static void delete(String link){
+        if (link == null || link.equals("")) {
+            return;
+        }
+
+        OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
+        try {
+            ossClient.deleteObject(bucketName, link);
+        } catch (OSSException oe) {
+            System.out.println("Caught an OSSException, which means your request made it to OSS, "
+                    + "but was rejected with an error response for some reason.");
+            System.out.println("Error Message:" + oe.getErrorMessage());
+            System.out.println("Error Code:" + oe.getErrorCode());
+            System.out.println("Request ID:" + oe.getRequestId());
+            System.out.println("Host ID:" + oe.getHostId());
+        } catch (ClientException ce) {
+            System.out.println("Caught an ClientException, which means the client encountered "
+                    + "a serious internal problem while trying to communicate with OSS, "
+                    + "such as not being able to access the network.");
+            System.out.println("Error Message:" + ce.getMessage());
+        } finally {
+            if (ossClient != null) {
+                ossClient.shutdown();
+            }
+        }
+    }
+
+
+
+}
diff --git a/ycl-platform/src/main/java/com/ycl/dto/video/AlarmDataParam.java b/ycl-platform/src/main/java/com/ycl/dto/video/AlarmDataParam.java
index 92a12b4..c5de217 100644
--- a/ycl-platform/src/main/java/com/ycl/dto/video/AlarmDataParam.java
+++ b/ycl-platform/src/main/java/com/ycl/dto/video/AlarmDataParam.java
@@ -1,8 +1,6 @@
 package com.ycl.dto.video;
 
-import com.baomidou.mybatisplus.annotation.TableField;
 import com.fasterxml.jackson.annotation.JsonProperty;
-import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 /**
diff --git a/ycl-platform/src/main/resources/application-dev.yml b/ycl-platform/src/main/resources/application-dev.yml
index 469d7c8..babaf60 100644
--- a/ycl-platform/src/main/resources/application-dev.yml
+++ b/ycl-platform/src/main/resources/application-dev.yml
@@ -71,4 +71,11 @@
   port: 8281
   userName: suichang
   passWord: a12345677
-  ip: 10.10.10.10
\ No newline at end of file
+  ip: 10.10.10.10
+
+aliyun:
+  oss:
+    endpoint: https://oss-cn-chengdu.aliyuncs.com
+    keyId: LTAI5tJTmV5pnPKjwvpdjyfi
+    keySecret: HRXGIrDdnhdlvV5AoOB4k9NxYP0G9Z
+    bucketName: baizonghao-upload
diff --git a/ycl-platform/src/main/resources/application-prod.yml b/ycl-platform/src/main/resources/application-prod.yml
index d896614..c8168e6 100644
--- a/ycl-platform/src/main/resources/application-prod.yml
+++ b/ycl-platform/src/main/resources/application-prod.yml
@@ -25,7 +25,7 @@
   redis:
     database: 0
     host: 127.0.0.1
-    port: 6379
+    port: 6380
     password: ycl2018
     jedis:
       pool:
diff --git a/ycl-smoke/src/main/resources/application.yml b/ycl-smoke/src/main/resources/application.yml
index aca341d..a5c6f78 100644
--- a/ycl-smoke/src/main/resources/application.yml
+++ b/ycl-smoke/src/main/resources/application.yml
@@ -1,6 +1,6 @@
 spring:
   profiles:
-    active: pro
+    active: dev
   main:
     allow-circular-references: true
     allow-bean-definition-overriding: true

--
Gitblit v1.8.0