spring对腾讯云COS进行封装注入

package xyz.lengmaomao.lengblog.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @program: blog
 * @description
 * @author: szn
 * @create: 2022-03-01 23:16
 * 使用autowired注入CosClient
 **/
@Component
public class TencentStoreConfig {

    private String secretId = "这里是你的id";

    private String secretKey = "这里是你的key";

    private String cos_region = "这里是你的存储桶所在地";

    /**
     * 初始化用户身份
     */
    @Bean
    public BasicCOSCredentials getBasicCOSCredentials(){
        return new BasicCOSCredentials(secretId, secretKey);
    }

    /**
     * // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
     * // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
     * @return
     */
    @Bean
    public ClientConfig getClientConfig(){
        Region region = new Region(cos_region);
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        return clientConfig;
    }
    @Bean
    public COSClient getCosClient(BasicCOSCredentials cred, ClientConfig clientConfig){
        return new COSClient(cred, clientConfig);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值