先在腾讯云控制台创建桶
引入依赖
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.227</version>
</dependency>
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos-sts_api</artifactId>
<version>3.1.1</version>
</dependency>
配置参数
public class TencentOSSConfig {
public final static String SECRETID = "替换";
public final static String SECRETKEY = "替换";
public final static String IDCRAD_BUCKET_NAME = "替换";
public final static String REGION = "替换";
代码
@GetMapping("/certificate")
public Map<String, Object> certificate() {
TreeMap<String, Object> config = new TreeMap<>();
config.put("secretId", TencentOSSConfig.SECRETID);
config.put("secretKey", TencentOSSConfig.SECRETKEY);
config.put("durationSeconds", 1800);
config.put("bucket", TencentOSSConfig.IDCRAD_BUCKET_NAME);
config.put("region", TencentOSSConfig.REGION);
config.put("allowPrefixes", new String[] {
"*"
});
String[] allowActions = new String[] {
"name/cos:PutObject",
"name/cos:PostObject"
};
config.put("allowActions", allowActions);
Response response = null;
try {
response = CosStsClient.getCredential(config);
} catch (IOException e) {
throw new RuntimeException(e);
}
Map<String, Object> map = new HashMap<>();
map.put("tmpSecretId", response.credentials.tmpSecretId);
map.put("tmpSecretKey", response.credentials.tmpSecretKey);
map.put("sessionToken", response.credentials.sessionToken);
map.put("region", TencentOSSConfig.REGION);
return map;
}
"result": {
"tmpSecretId": "AKIDza6X1cbVtbYwNa9owNlXrERw6jJKX..."
"tmpSecretKey": "Wk9vEC9kjDBpYRKMnfXHxYYx+wottnh...",
"sessionToken": "21IQFM5UorOC8XxP0hKfAf6GsqwzXJ...",
"region": "ap-guangzhou"
},