SpringBoot 腾讯云OSS获取临时凭证

先在腾讯云控制台创建桶

在这里插入图片描述

引入依赖

<!--腾讯云OSS-->
<dependency>
	<groupId>com.qcloud</groupId>
	<artifactId>cos_api</artifactId>
	<version>5.6.227</version>
</dependency>

<!--腾讯云STS-->
<dependency>
	<groupId>com.qcloud</groupId>
	<artifactId>cos-sts_api</artifactId>
	<version>3.1.1</version>
</dependency>

配置参数

public class TencentOSSConfig {
    // secretid
    public final static String SECRETID = "替换";
    // secretkey
    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<>();
        // 云 api 密钥 SecretId
        config.put("secretId", TencentOSSConfig.SECRETID);
        // 云 api 密钥 SecretKey
        config.put("secretKey", TencentOSSConfig.SECRETKEY);
        // 临时密钥有效时长,单位是秒
        config.put("durationSeconds", 1800);
        // 桶名称
        config.put("bucket", TencentOSSConfig.IDCRAD_BUCKET_NAME);
        // bucket 所在地区
        config.put("region", TencentOSSConfig.REGION);
        // 可以通过 allowPrefixes 指定访问前缀目录权限
        // 例子: a.jpg 或者 a/*
        // * 全部都可以访问
        config.put("allowPrefixes", new String[] {
                "*"
        });
        // 密钥的权限列表。
        // 权限列表请看 https://cloud.tencent.com/document/product/436/31923
        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"
},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值