Kaynağa Gözat

update aliyun-sdk

reghao 2 yıl önce
ebeveyn
işleme
50c9184a6b

+ 22 - 15
manager/src/test/java/AliyunCdn.java → manager/src/main/java/cn/reghao/devops/manager/aliyun/AliyunCdn.java

@@ -1,3 +1,5 @@
+package cn.reghao.devops.manager.aliyun;
+
 import com.aliyun.cdn20180510.Client;
 import com.aliyun.cdn20180510.models.DescribeRefreshTasksRequest;
 import com.aliyun.cdn20180510.models.DescribeRefreshTasksResponse;
@@ -9,6 +11,20 @@ import com.aliyun.teaopenapi.models.Config;
 import java.util.Map;
 
 public class AliyunCdn {
+    private final Client client;
+
+    public AliyunCdn() throws Exception {
+        String accessKeyId = "LTAI5tAH6NQD3wcARukkSuNd";
+        String accessKeySecret = "v13JyqOFbtELD1I17r3bvAXaySJw4L";
+        Config config = new Config()
+                // 必填,您的 AccessKey ID
+                .setAccessKeyId(accessKeyId)
+                // 必填,您的 AccessKey Secret
+                .setAccessKeySecret(accessKeySecret);
+        config.endpoint = "cdn.aliyuncs.com";
+        this.client = new Client(config);
+    }
+
     /**
      * 刷新 aliyun-cdn 缓存
      *
@@ -16,7 +32,7 @@ public class AliyunCdn {
      * @return
      * @date 2023-12-18 16:24:01
      */
-    static void refreshObjectCaches(Client client, String objectType, String objectPath) throws Exception {
+    public void refreshObjectCaches(String objectType, String objectPath) throws Exception {
         RefreshObjectCachesRequest req = new RefreshObjectCachesRequest();
         // 此参数为刷新的类型, 其值可以为File或Directory。默认值:File。
         req.objectType = objectType;
@@ -26,7 +42,7 @@ public class AliyunCdn {
         Map<String, Object> map = TeaModel.buildMap(resp);
         Map<String, Object> map1 = (Map<String, Object>)map.get("body");
         String taskId = (String) map1.get("RefreshTaskId");
-        describeRefreshTasks(client, taskId);
+        describeRefreshTasks(taskId);
     }
 
     /**
@@ -36,7 +52,7 @@ public class AliyunCdn {
      * @return
      * @date 2023-12-18 16:24:19
      */
-    static void describeRefreshTasks(Client client, String taskId) throws Exception {
+    public void describeRefreshTasks(String taskId) throws Exception {
         DescribeRefreshTasksRequest request = new DescribeRefreshTasksRequest();
         request.taskId = taskId;
         try {
@@ -49,21 +65,12 @@ public class AliyunCdn {
     }
 
     public static void main(String[] args) throws Exception {
-        String accessKeyId = "LTAI5tAH6NQD3wcARukkSuNd";
-        String accessKeySecret = "v13JyqOFbtELD1I17r3bvAXaySJw4L";
-        Config config = new Config()
-                // 必填,您的 AccessKey ID
-                .setAccessKeyId(accessKeyId)
-                // 必填,您的 AccessKey Secret
-                .setAccessKeySecret(accessKeySecret);
-        config.endpoint = "cdn.aliyuncs.com";
-        Client client = new Client(config);
-
+        AliyunCdn aliyunCdn = new AliyunCdn();
         String objectType = "Directory";
         String objectPath = "http://dnkt.iquizoo.com/";
-        //refreshObjectCaches(client, objectType, objectPath);
+        aliyunCdn.refreshObjectCaches(objectType, objectPath);
 
         String taskId = "17442221351";
-        describeRefreshTasks(client, taskId);
+        aliyunCdn.describeRefreshTasks(taskId);
     }
 }

+ 34 - 44
manager/src/test/java/AliyunOss.java → manager/src/main/java/cn/reghao/devops/manager/aliyun/AliyunOss.java

@@ -1,11 +1,10 @@
+package cn.reghao.devops.manager.aliyun;
+
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
 
 import com.aliyun.oss.OSS;
 import com.aliyun.oss.OSSClientBuilder;
@@ -16,17 +15,17 @@ import com.aliyun.oss.model.*;
  * @date 2023-12-18 16:50:50
  */
 public class AliyunOss {
-    private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
-    private static String accessKeyId = "jYk8lOKwSCorFEz7";
-    private static String accessKeySecret = "hzSrcew08V5zk58kVVgInV7OqbHyVc";
-    private static String bucketName = "iquizoo";
-    private static String objectNamePrefix = "dev/1/";
-    private static OSS ossClient;
-    static {
-        ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+    private final String bucketName = "iquizoo";
+    private final OSS ossClient;
+
+    public AliyunOss() {
+        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
+        String accessKeyId = "jYk8lOKwSCorFEz7";
+        String accessKeySecret = "hzSrcew08V5zk58kVVgInV7OqbHyVc";
+        this.ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
     }
 
-    static void uploadObject(String objectName, File file) throws IOException {
+    public void uploadObject(String objectName, File file) throws IOException {
         System.out.println("Uploading a new object to OSS from a file\n");
         PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);
         ObjectMetadata objectMetadata = new ObjectMetadata();
@@ -35,18 +34,31 @@ public class AliyunOss {
         ossClient.putObject(putObjectRequest);
     }
 
-    static void downloadObject(String objectName) throws IOException {
+    public void downloadObject(String objectName) throws IOException {
         System.out.println("Downloading an object");
         OSSObject object = ossClient.getObject(bucketName, objectName);
         System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
         displayTextInputStream(object.getObjectContent());
     }
 
-    static void deleteObject(String objectName) {
+    private void displayTextInputStream(InputStream input) throws IOException {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
+        while (true) {
+            String line = reader.readLine();
+            if (line == null) break;
+
+            System.out.println("    " + line);
+        }
+        System.out.println();
+
+        reader.close();
+    }
+
+    public void deleteObject(String objectName) {
         ossClient.deleteObject(bucketName, objectName);
     }
 
-    static void listObjects(String prefix) {
+    public void listObjects(String prefix) {
         System.out.println("Listing objects");
             ObjectListing objectListing = ossClient.listObjects(bucketName, prefix);
             for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
@@ -61,10 +73,14 @@ public class AliyunOss {
             System.out.println();
     }
 
+    public void close() {
+        ossClient.shutdown();
+    }
+
     public static void main(String[] args) throws IOException {
         String accessKeyId = System.getenv("ACCESS_KEY_ID");
         String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");
-        System.out.println();
+        AliyunOss aliyunOss = new AliyunOss();
 
         /*String baseDir = "/home/reghao/data/image/";
         File dir = new File(baseDir);
@@ -83,34 +99,8 @@ public class AliyunOss {
 
         /*String objectName = "dev/test/1.txt";
         File file = createSampleFile();*/
-        listObjects(objectNamePrefix);
-    }
-
-    static void close() {
-        ossClient.shutdown();
-    }
-
-    private static File createSampleFile() throws IOException {
-        File file = File.createTempFile("oss-java-sdk-", ".txt");
-        file.deleteOnExit();
 
-        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
-        writer.write("abcdefghijklmnopqrstuvwxyz\n");
-        writer.write("0123456789011234567890\n");
-        writer.close();
-        return file;
-    }
-
-    private static void displayTextInputStream(InputStream input) throws IOException {
-        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
-        while (true) {
-            String line = reader.readLine();
-            if (line == null) break;
-
-            System.out.println("    " + line);
-        }
-        System.out.println();
-
-        reader.close();
+        String objectNamePrefix = "dev/1/";
+        aliyunOss.listObjects(objectNamePrefix);
     }
 }