初始化
commit
45697ccada
|
@ -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
|
|
@ -0,0 +1,8 @@
|
|||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
<file url="PROJECT" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="MavenServices">
|
||||
<service-url>https://oss.sonatype.org/service/local/</service-url>
|
||||
<service-url>https://repo.jfrog.org/artifactory/api/</service-url>
|
||||
<service-url>https://repository.jboss.org/nexus/service/local/</service-url>
|
||||
<service-url>http://124.223.23.100:8081/repository/maven-public/</service-url>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,99 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.bawei</groupId>
|
||||
<artifactId>question2</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.15</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.18</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.67</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.java-websocket</groupId>
|
||||
<artifactId>Java-WebSocket</artifactId>
|
||||
<version>1.3.8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.10.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.32</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20210307</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.15</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-s3</artifactId>
|
||||
<version>1.12.68</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-core</artifactId>
|
||||
<version>1.12.68</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
|
@ -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<String, String> params) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for(Map.Entry<String, String> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue