Commit c187e1b4 authored by 张永's avatar 张永

获取云上传的临时密钥

parent 31fc640f
......@@ -106,6 +106,42 @@
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
<!-- 阿里云 start -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.11.6</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<!-- <version>2.3.0</version> -->
<version>2.8.3</version>
</dependency>
<!-- 阿里云 end -->
<!-- 腾讯云 start -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.7</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>cos-sts-java</artifactId>
<version>3.0.4</version>
</dependency>
<!-- 腾讯云 end-->
</dependencies>
<build>
<plugins>
......@@ -150,5 +186,17 @@
<name>mvnrepository</name>
<url>http://mvnrepository.com/</url>
</repository>
<repository>
<id>bintray-qcloud-maven-repo</id>
<name>qcloud-maven-repo</name>
<url>https://dl.bintray.com/qcloud/maven-repo/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
\ No newline at end of file
package com.egolm.shop.controller;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx;
import com.egolm.shop.service.SystemCtrlService;
import com.egolm.shop.util.ShopContstrant;
import com.tencent.cloud.CosStsClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api(tags={"服务云接口"})
@RestController
@RequestMapping("yun")
public class YunController {
@Autowired
private SystemCtrlService systemCtrlService;
@ApiOperation("获取上传凭证及上传的云平台")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "langID", dataType = "String", required = false, value = "语言", defaultValue = "936"),
@ApiImplicitParam(paramType = "query", name = "sign", dataType = "String", required = true, value = "签名", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "timestamp", dataType = "String", required = true, value = "时间戳", defaultValue = "")
})
@RequestMapping(value = "/getPolicy",method=RequestMethod.GET)
public String createGuest(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> choiceMap = systemCtrlService.queryTSystemCtrlByCode(ShopContstrant.Choice_OSS_KEY, "936");
if(choiceMap != null) {
String value1 = choiceMap.get("sValue1") + "";
System.out.println(value1);
if (value1.equalsIgnoreCase(ShopContstrant.Aliyun_OSS_KEY)) {
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("tmpSecretId", "");
resultMap.put("tmpSecretKey", "没做");
resultMap.put("sessionToken", "");
resultMap.put("webUrl", "");
resultMap.put("region","");
resultMap.put("bucketName", "");
resultMap.put("type", "aliyun");
return Rjx.json(resultMap).setCode(200).toString();
}
if(value1.equalsIgnoreCase(ShopContstrant.Tencent_Cos_Key)) {
int expiryTime = 1800; //秒
Map<String,Object> expiryMap = systemCtrlService.queryTSystemCtrlByCode(ShopContstrant.Tencent_Expiry_Time, "936"); //过期时间
if(expiryMap != null) {
try {
String expriyMM = expiryMap.get("sValue1") + ""; //分钟
if(StringUtil.isNotEmpty(expriyMM)) {
expiryTime = Integer.valueOf(expriyMM) * 60;
}
} catch (NumberFormatException e) {
}
}
Map<String, Object> tencentyunMap = systemCtrlService.queryTSystemCtrlByCode(ShopContstrant.Tencent_Cos_Key, "936");
if(tencentyunMap == null) {
return Rjx.jsonErr().setMessage("腾讯云COS参数未配置").toJson();
}
String region = tencentyunMap.get("sValue1") + "";
String bucketName = tencentyunMap.get("sValue2") + "";
String appID = tencentyunMap.get("sValue3") + "";
String secret = tencentyunMap.get("sValue4") + "";
String webUrl = tencentyunMap.get("sValue5")+"";
TreeMap<String, Object> config = new TreeMap<String, Object>();
try {
// 替换为您的 SecretId
config.put("SecretId", appID);
// 替换为您的 SecretKey
config.put("SecretKey", secret);
// 临时密钥有效时长,单位是秒
config.put("durationSeconds", expiryTime);
// 换成您的 bucket
config.put("bucket", bucketName);
// 换成 bucket 所在地区
config.put("region", region);
// 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的目录,例子:* 或者 doc/* 或者 picture.jpg
config.put("allowPrefix", "*");
// 密钥的权限列表。简单上传、表单上传和分片上传需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
String[] allowActions = new String[] {
// 简单上传
"name/cos:PutObject",
// 表单上传、小程序上传
"name/cos:PostObject",
// 分片上传
"name/cos:InitiateMultipartUpload",
"name/cos:ListMultipartUploads",
"name/cos:ListParts",
"name/cos:UploadPart",
"name/cos:CompleteMultipartUpload"
};
config.put("allowActions", allowActions);
System.out.println(config);
JSONObject credential = CosStsClient.getCredential(config);
System.out.println(credential);
//成功返回临时密钥信息,如下打印密钥信息
JSONObject credentialObj = credential.getJSONObject("credentials");
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put("tmpSecretId", credentialObj.getString("tmpSecretId"));
resultMap.put("tmpSecretKey", credentialObj.getString("tmpSecretKey"));
resultMap.put("sessionToken", credentialObj.getString("sessionToken"));
resultMap.put("webUrl", webUrl);
resultMap.put("region", region);
resultMap.put("bucketName", bucketName);
resultMap.put("type", "tencentyun");
return Rjx.json(resultMap).setCode(200).toString();
}catch (Exception e) {
e.printStackTrace();
//失败抛出异常
return Rjx.json().setMessage("腾讯云临时密钥获取失败").toJson();
}
}
}
return Rjx.jsonOk().toString();
}
}
......@@ -15,4 +15,13 @@ public class ShopContstrant {
public static final String SystemCtl_Redis_Base_Key = "SystemCtl"; //与job服务中缓存的Key一样 存放在 opsForHash中
public static final String CLIENT_MD5KEY ="Client_Md5Key"; //客户端计算签名的MD5 Key
//上传的
public static final String Choice_OSS_KEY= "ChoiceOssKEY"; //OSS选择
public static final String Self_OSS_KEY="SelfOssKey"; //自建OSS
public static final String Aliyun_OSS_KEY="AliyunOssKey"; //阿里云OSS
public static final String Tencent_Cos_Key="TencentCosKey"; //腾讯云COS
public static final String Aliyun_Expiry_Time="AliOssFileExpiryTime"; //阿里文件 过期时间 单位分钟
public static final String Tencent_Expiry_Time="TencentExpiryTime"; //腾讯文件 过期时间 单位分钟
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment