commit 45697ccadae654ebd4d205256b3cae5f0cdcc65c Author: pqh <2897412868@qq.com> Date: Tue Oct 3 13:34:51 2023 +0800 初始化 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..63574ec --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/mavenServices.xml b/.idea/mavenServices.xml new file mode 100644 index 0000000..ace6d4b --- /dev/null +++ b/.idea/mavenServices.xml @@ -0,0 +1,9 @@ + + + + https://oss.sonatype.org/service/local/ + https://repo.jfrog.org/artifactory/api/ + https://repository.jboss.org/nexus/service/local/ + http://124.223.23.100:8081/repository/maven-public/ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..82dbec8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..96caf0b --- /dev/null +++ b/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + com.bawei + question2 + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + org.springframework.boot + spring-boot-starter-parent + 2.7.15 + + + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + cn.hutool + hutool-all + 5.8.18 + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba + fastjson + 1.2.67 + + + org.java-websocket + Java-WebSocket + 1.3.8 + + + com.squareup.okhttp3 + okhttp + 4.10.0 + + + org.projectlombok + lombok + + + org.slf4j + slf4j-log4j12 + + + mysql + mysql-connector-java + 5.1.32 + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.3.1 + + + org.springframework.boot + spring-boot-starter-aop + + + org.json + json + 20210307 + + + commons-codec + commons-codec + 1.15 + + + com.amazonaws + aws-java-sdk-s3 + 1.12.68 + + + + com.amazonaws + aws-java-sdk-core + 1.12.68 + + + + diff --git a/src/main/java/com/bawei/duojiyun/utils/DogGetApi.java b/src/main/java/com/bawei/duojiyun/utils/DogGetApi.java new file mode 100644 index 0000000..a0e980b --- /dev/null +++ b/src/main/java/com/bawei/duojiyun/utils/DogGetApi.java @@ -0,0 +1,122 @@ +package com.bawei.duojiyun.utils; + +import org.apache.commons.codec.binary.Hex; +import org.json.JSONObject; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Map; +/** + * @author 冯凯 + * @version 1.0 + * @description: TODO + * @date 2023/10/4 15:00 + */ +public class DogGetApi { + + + // 普通 API 请使用这个方法 + public static JSONObject dogeAPIGet(String apiPath, Map params) { + StringBuilder sb = new StringBuilder(); + for(Map.Entry hm : params.entrySet()){ + try { + sb.append(URLEncoder.encode(hm.getKey(), String.valueOf(StandardCharsets.UTF_8))).append('=').append(URLEncoder.encode(hm.getValue(), String.valueOf(StandardCharsets.UTF_8))).append("&"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + String bodyText = sb.toString().replace("&$", ""); + try { + return dogeAPIGet(apiPath, bodyText, false); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } + } + + // 要求请求内容 Body 是一个 JSON 的 API,请使用这个方法 + public static JSONObject dogeAPIGet(String apiPath, JSONObject params) { + String bodyText = params.toString(); + try { + return dogeAPIGet(apiPath, bodyText, true); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } + } + + // 无参数 API + public static JSONObject dogeAPIGet(String apiPath) { + try { + return dogeAPIGet(apiPath, "", true); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } + } + + public static JSONObject dogeAPIGet(String apiPath, String paramsText, Boolean jsonMode) throws IOException{ + // 这里返回值类型是 JSONObject,你也可以根据你的具体情况使用不同的 JSON 库并修改最下方 JSON 处理代码 + + // 这里替换为你的多吉云永久 AccessKey 和 SecretKey,可在用户中心 - 密钥管理中查看 + // 请勿在客户端暴露 AccessKey 和 SecretKey,那样恶意用户将获得账号完全控制权 + String accessKey = "47e4769cb4983a6"; + String secretKey = "230ad2a0ee190aa8b083424a87d8ccd1"; + + String signStr = apiPath + "\n" + paramsText; + String sign = ""; + try { + Mac mac = Mac.getInstance("HmacSHA1"); + mac.init(new SecretKeySpec(secretKey.getBytes(), "HmacSHA1")); + sign = new String(new Hex().encode(mac.doFinal(signStr.getBytes())), StandardCharsets.UTF_8); // 这里 Hex 来自 org.apache.commons.codec.binary.Hex + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } catch (InvalidKeyException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } + String authorization = "TOKEN " + accessKey + ':' + sign; + + URL u = new URL("https://api.dogecloud.com" + apiPath); + HttpURLConnection conn = (HttpURLConnection) u.openConnection(); + conn.setDoOutput(true); + conn.setRequestMethod("POST"); + conn.setRequestProperty( "Content-Type", jsonMode ? "application/json" : "application/x-www-form-urlencoded"); + conn.setRequestProperty( "Authorization", authorization); + conn.setRequestProperty( "Content-Length", String.valueOf(paramsText.length())); + OutputStream os = conn.getOutputStream(); + os.write(paramsText.getBytes()); + os.flush(); + os.close(); + StringBuilder retJSON = new StringBuilder(); + if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){ + String readLine = ""; + try (BufferedReader responseReader=new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { + while((readLine=responseReader.readLine())!=null){ + retJSON.append(readLine).append("\n"); + } + responseReader.close(); + } + JSONObject ret = new JSONObject(retJSON.toString()); + if (ret.getInt("code") != 200) { + System.err.println("{\"error\":\"API 返回错误:" + ret.getString("msg") + "\"}"); + } else { + JSONObject output = new JSONObject(); + JSONObject data = ret.getJSONObject("data"); + return data; + } + } else { + System.err.println("{\"error\":\"网络错误:" + conn.getResponseCode() + "\"}"); + } + return null; + } + +} diff --git a/src/main/java/com/bawei/duojiyun/utils/DuoJiYunUtil.java b/src/main/java/com/bawei/duojiyun/utils/DuoJiYunUtil.java new file mode 100644 index 0000000..92deee6 --- /dev/null +++ b/src/main/java/com/bawei/duojiyun/utils/DuoJiYunUtil.java @@ -0,0 +1,29 @@ +package com.bawei.duojiyun.utils; + +import org.json.JSONObject; + +import static com.bawei.duojiyun.utils.DogGetApi.dogeAPIGet; + + +/** + * @author 冯凯 + * @version 1.0 + * @description: TODO + * @date 2023/10/4 14:59 + */ +public class DuoJiYunUtil { + + // 这段代码本质上就是调用多吉云 API 获取临时密钥,你也可以自己实现: +// 该 API 参考文档: https://docs.dogecloud.com/oss/api-tmp-token + + public static JSONObject getCredentials() { + JSONObject body = new JSONObject(); + body.put("channel", "OSS_FULL"); + body.append("scopes", "muyu"); // 要操作的存储空间名称,注意不是 s3Bucket 值,也可以设置为 * + + JSONObject data = dogeAPIGet("/auth/tmp_token.json", body); + JSONObject credentials = data.getJSONObject("Credentials"); + return credentials; + } + +} diff --git a/src/main/java/com/bawei/duojiyun/utils/S3.java b/src/main/java/com/bawei/duojiyun/utils/S3.java new file mode 100644 index 0000000..76ad4ad --- /dev/null +++ b/src/main/java/com/bawei/duojiyun/utils/S3.java @@ -0,0 +1,53 @@ +package com.bawei.duojiyun.utils; + +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicSessionCredentials; +import com.amazonaws.client.builder.AwsClientBuilder; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.PutObjectResult; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.File; + +import static com.bawei.duojiyun.utils.DogGetApi.dogeAPIGet; + + +/** + * @author 冯凯 + * @version 1.0 + * @description: TODO + * @date 2023/10/4 15:10 + */ +public class S3 { + + public static AmazonS3 initS3(JSONObject credentials){ + BasicSessionCredentials awsCredentials = new BasicSessionCredentials(credentials.getString("accessKeyId"), credentials.getString("secretAccessKey"), credentials.getString("sessionToken")); + AmazonS3 s3 = AmazonS3ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) + .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration( + "https://cos.ap-shanghai.myqcloud.com", // 修改为多吉云控制台存储空间 SDK 参数中的 s3Endpoint + "automatic")) + .build(); + return s3; + } + + public static void main(String[] args) throws Exception { + JSONObject data = dogeAPIGet("/oss/bucket/list.json"); + JSONArray buckets = data.getJSONArray("buckets"); + for (int i = 0 ; i < buckets.length(); i++) { + System.out.println(buckets.getJSONObject(i).toString()); + } + + JSONObject credentials = DuoJiYunUtil.getCredentials(); + AmazonS3 s3 = initS3(credentials); + PutObjectResult putObjectResult = s3.putObject("s-sh-7714-muyu-1258813047", "lihao.png", new File("D:\\img\\4~NIBGJKOVQ}7HA$0J~9REW.png")); + String string = putObjectResult.toString(); + System.out.println(string); + + } + + + +}