提交 使用多吉云进行文件上传
parent
40ae59240b
commit
b4c33c69f6
22
pom.xml
22
pom.xml
|
@ -72,6 +72,28 @@
|
|||
<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>
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,123 @@
|
|||
package com.duojiyun.util;
|
||||
|
||||
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;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
/**
|
||||
* @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 = "da22704e6e286726";
|
||||
String secretKey = "af0e5b9e236bed79e96ad5877f500fba";
|
||||
|
||||
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,28 @@
|
|||
package com.duojiyun.util;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import static com.duojiyun.util.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,55 @@
|
|||
package com.duojiyun.util;
|
||||
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 com.amazonaws.services.s3.model.S3Object;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
import static com.duojiyun.util.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