|
@@ -5,6 +5,14 @@ import com.aliyun.oss.OSSClientBuilder;
|
|
|
import com.aliyun.oss.model.GetObjectRequest;
|
|
|
import com.aliyun.oss.model.ObjectMetadata;
|
|
|
import com.aliyun.oss.model.UploadFileRequest;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.auth.sts.AssumeRoleRequest;
|
|
|
+import com.aliyuncs.auth.sts.AssumeRoleResponse;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.aliyuncs.http.MethodType;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import com.aliyuncs.profile.IClientProfile;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -12,6 +20,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
@@ -35,6 +44,78 @@ public class OssUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public static HashMap<String, String> getToken() {
|
|
|
+ //构建一个阿里云客户端,用于发起请求。
|
|
|
+ //设置调用者(RAM用户或RAM角色)的AccessKey ID和AccessKey Secret。
|
|
|
+// DefaultProfile.addEndpoint("oss-cn-shenzhen", "Sts", endpoint.replace("https://",""));
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile("oss-cn-shenzhen", accessKeyId, accessKeySecret);
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+
|
|
|
+ //构造请求,设置参数。
|
|
|
+ AssumeRoleRequest request = new AssumeRoleRequest();
|
|
|
+ request.setRegionId(endpoint);
|
|
|
+ request.setRoleArn("acs:ram::1565565840178476:role/caimeitst");
|
|
|
+ request.setRoleSessionName("TSTtest");
|
|
|
+ AssumeRoleResponse response = null;
|
|
|
+ try {
|
|
|
+ response = client.getAcsResponse(request);
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ String requestId = response.getRequestId();
|
|
|
+ String securityToken = response.getCredentials().getSecurityToken();
|
|
|
+ String accessKeySecret = response.getCredentials().getAccessKeySecret();
|
|
|
+ String accessKeyId = response.getCredentials().getAccessKeyId();
|
|
|
+ HashMap<String, String> tokenMap = new HashMap<>(5);
|
|
|
+ tokenMap.put("requestId", requestId);
|
|
|
+ tokenMap.put("accessKeyId", accessKeyId);
|
|
|
+ tokenMap.put("accessKeySecret", accessKeySecret);
|
|
|
+ tokenMap.put("securityToken", securityToken);
|
|
|
+ tokenMap.put("bucket", privateBucket);
|
|
|
+ return tokenMap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*public static HashMap<String, String> getToken() throws ClientException {
|
|
|
+ // regionId表示RAM的地域ID。以华东1(杭州)地域为例,regionID填写为cn-hangzhou。也可以保留默认值,默认值为空字符串("")。
|
|
|
+ String regionId = "";
|
|
|
+ // 添加endpoint。适用于Java SDK 3.12.0及以上版本。
|
|
|
+ DefaultProfile.addEndpoint(regionId, "Sts", endpoint.replace("https://",""));
|
|
|
+ // 添加endpoint。适用于Java SDK 3.12.0以下版本。
|
|
|
+ // DefaultProfile.addEndpoint("",regionId, "Sts", endpoint);
|
|
|
+ // 构造default profile。
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
|
|
|
+ // 构造client。
|
|
|
+ DefaultAcsClient client = new DefaultAcsClient(profile);
|
|
|
+ final AssumeRoleRequest request = new AssumeRoleRequest();
|
|
|
+ // 适用于Java SDK 3.12.0及以上版本。
|
|
|
+ request.setSysMethod(MethodType.POST);
|
|
|
+ // 适用于Java SDK 3.12.0以下版本。
|
|
|
+ //request.setMethod(MethodType.POST);
|
|
|
+ request.setRoleArn("acs:ram::1565565840178476:role/caimeitst");
|
|
|
+ request.setRoleSessionName("TSTtest");
|
|
|
+ request.setPolicy();
|
|
|
+// request.setPolicy(null); // 如果policy为空,则用户将获得该角色下所有权限。
|
|
|
+ request.setDurationSeconds(3600L); // 设置临时访问凭证的有效时间为3600秒。
|
|
|
+ final AssumeRoleResponse response = client.getAcsResponse(request);
|
|
|
+ System.out.println("Expiration: " + response.getCredentials().getExpiration());
|
|
|
+ System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
|
|
|
+ System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
|
|
|
+ System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
|
|
|
+ System.out.println("RequestId: " + response.getRequestId());
|
|
|
+
|
|
|
+ String requestId = response.getRequestId();
|
|
|
+ String securityToken = response.getCredentials().getSecurityToken();
|
|
|
+ String accessKeySecret = response.getCredentials().getAccessKeySecret();
|
|
|
+ String accessKeyId = response.getCredentials().getAccessKeyId();
|
|
|
+ HashMap<String, String> tokenMap = new HashMap<>(5);
|
|
|
+ tokenMap.put("requestId", requestId);
|
|
|
+ tokenMap.put("accessKeyId", accessKeyId);
|
|
|
+ tokenMap.put("accessKeySecret", accessKeySecret);
|
|
|
+ tokenMap.put("securityToken", securityToken);
|
|
|
+ tokenMap.put("bucket", privateBucket);
|
|
|
+ return tokenMap;
|
|
|
+ }*/
|
|
|
|
|
|
public static String ossUpload(String fileName, File file, String contentType) {
|
|
|
String url = null;
|