Kaynağa Gözat

添加 aliyun oss 和 cdn demo

reghao 2 yıl önce
ebeveyn
işleme
e71f81d1ca

+ 11 - 0
manager/pom.xml

@@ -169,6 +169,17 @@
             <version>2.6</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>cdn20180510</artifactId>
+            <version>3.2.0</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+            <version>3.8.0</version>
+        </dependency>
+
         <!--<dependency>
             <groupId>org.apache.dubbo</groupId>
             <artifactId>dubbo-spring-boot-starter</artifactId>

+ 69 - 0
manager/src/test/java/AliyunCdn.java

@@ -0,0 +1,69 @@
+import com.aliyun.cdn20180510.Client;
+import com.aliyun.cdn20180510.models.DescribeRefreshTasksRequest;
+import com.aliyun.cdn20180510.models.DescribeRefreshTasksResponse;
+import com.aliyun.cdn20180510.models.RefreshObjectCachesRequest;
+import com.aliyun.cdn20180510.models.RefreshObjectCachesResponse;
+import com.aliyun.tea.TeaModel;
+import com.aliyun.teaopenapi.models.Config;
+
+import java.util.Map;
+
+public class AliyunCdn {
+    /**
+     * 刷新 aliyun-cdn 缓存
+     *
+     * @param
+     * @return
+     * @date 2023-12-18 16:24:01
+     */
+    static void refreshObjectCaches(Client client, String objectType, String objectPath) throws Exception {
+        RefreshObjectCachesRequest req = new RefreshObjectCachesRequest();
+        // 此参数为刷新的类型, 其值可以为File或Directory。默认值:File。
+        req.objectType = objectType;
+        // 加速的文件位置,wdtest.licai.cn为配置的域名,后加加速的文件名
+        req.objectPath = objectPath;
+        RefreshObjectCachesResponse resp = client.refreshObjectCaches(req);
+        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);
+    }
+
+    /**
+     * 获取 aliyun-cdn 缓存缓存刷新任务状态
+     *
+     * @param
+     * @return
+     * @date 2023-12-18 16:24:19
+     */
+    static void describeRefreshTasks(Client client, String taskId) throws Exception {
+        DescribeRefreshTasksRequest request = new DescribeRefreshTasksRequest();
+        request.taskId = taskId;
+        try {
+            DescribeRefreshTasksResponse resp = client.describeRefreshTasks(request);
+            Map<String, Object> map = TeaModel.buildMap(resp);
+            System.out.println();
+        } catch (Exception error) {
+            error.printStackTrace();
+        }
+    }
+
+    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);
+
+        String objectType = "Directory";
+        String objectPath = "http://dnkt.iquizoo.com/";
+        //refreshObjectCaches(client, objectType, objectPath);
+
+        String taskId = "17442221351";
+        describeRefreshTasks(client, taskId);
+    }
+}

+ 116 - 0
manager/src/test/java/AliyunOss.java

@@ -0,0 +1,116 @@
+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;
+import com.aliyun.oss.model.*;
+
+/**
+ * @author reghao
+ * @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);
+    }
+
+    static 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();
+        objectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
+        putObjectRequest.setMetadata(objectMetadata);
+        ossClient.putObject(putObjectRequest);
+    }
+
+    static 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) {
+        ossClient.deleteObject(bucketName, objectName);
+    }
+
+    static void listObjects(String prefix) {
+        System.out.println("Listing objects");
+            ObjectListing objectListing = ossClient.listObjects(bucketName, prefix);
+            for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
+                String objectName = objectSummary.getKey();
+                long size = objectSummary.getSize();
+                if (size != 0) {
+                    deleteObject(objectName);
+                }
+
+                System.out.println(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
+            }
+            System.out.println();
+    }
+
+    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();
+
+        /*String baseDir = "/home/reghao/data/image/";
+        File dir = new File(baseDir);
+        for (File file : dir.listFiles()) {
+            if (file.isDirectory()) {
+                continue;
+            }
+
+            String filename = file.getName();
+            String objectName = String.format("%s%s", objectNamePrefix, filename);
+            uploadObject(objectName, file);
+
+            String url = String.format("https://static.cdn.iquizoo.com/%s", objectName);
+            System.out.println("url: " + url);
+        }*/
+
+        /*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();
+    }
+}