腾讯云对象存储COS计算文件的大小

properties配置类

java 复制代码
TencentCos.APPID=xxxxx
TencentCos.SecretId=xxxxxxx
TencentCos.SecretKey=xxxxxx
TencentCos.testBucket=xxxxxx
TencentCos.CosPath=https://xxxxxxxx.cos.ap-chengdu.myqcloud.com
TencentCos.region=ap-chengdu

读取properties中的配置

java 复制代码
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
 * 读取yml中的配置
 */
@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TencentConfig implements InitializingBean {
    @Value("${TencentCos.CosPath}")
    private String cosPath;

    @Value("${TencentCos.SecretId}")
    private String secretId;

    @Value("${TencentCos.SecretKey}")
    private String secretKey;
    @Value("${TencentCos.region}")
    private String regionName;

    @Value("${TencentCos.testBucket}")
    private String bucketName;

    @Value("${TencentCos.APPID}")
    private String appId ;


    public static String COSPATH;
    public static String SECRET_ID;
    public static String SECRET_KEY;
    public static String REGION_NAME;
    public static String BUCKET_NAME;
    public static String APPID;

    @Override
    public void afterPropertiesSet() throws Exception {
        COSPATH = cosPath;
        SECRET_ID = secretId;
        SECRET_KEY = secretKey;
        REGION_NAME = regionName;
        BUCKET_NAME = bucketName;
        APPID = appId;

    }
}

初始化cos客户端

java 复制代码
    //初始化cos客户端
    public COSClient cosClientnew(String secretId, String secretKey, String regionName){
        // 1 初始化用户身份信息(secretId, secretKey)。
        // SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
        COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(regionName);
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        // 从 5.6.54 版本开始,默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 以下的设置,是可选的:

        // 设置 socket 读取超时,默认 30s
        clientConfig.setSocketTimeout(30*1000);
        // 设置建立连接超时,默认 30s
        clientConfig.setConnectionTimeout(30*1000);

        // 3 初始化 cos 客户端。
        return new COSClient(cred, clientConfig);
    }

测试读取数据

java 复制代码
    @Test
    public void test01wwww1(){
        COSClient cosClient = cosClientnew(TencentConfig.SECRET_ID,TencentConfig.SECRET_KEY,TencentConfig.REGION_NAME);
        // Bucket 的命名格式为 BucketName-APPID ,此处填写的存储桶名称必须为此格式
        String bucketName = "donglin-1304838938";

        String key = "ldl.txt";
        try {
            ObjectMetadata objectMetadata = cosClient.getObjectMetadata(bucketName, key);
            System.out.println(objectMetadata.getContentLength());
        } catch (CosServiceException e) {
            e.printStackTrace();
        } catch (CosClientException e) {
            e.printStackTrace();
        }
    }

测试

相关推荐
真上帝的左手1 天前
19. 大数据- 湖仓一体&BI - 阿里云实践
大数据·阿里云·云计算
小新讲网安2 天前
云安全攻防实战:AWS与阿里云渗透测试全流程指南
安全·web安全·网络安全·阿里云·自动化·云计算·aws
行业研究员2 天前
腾讯云ADP:智能体平台封神榜
人工智能·microsoft·腾讯云·智能体·智能体平台·腾讯云adp
星野川崎2063 天前
电商多店运维:云机长期挂机频繁掉线、账号无故风控原因剖析与解决方案
大数据·运维·云计算·电商
AKAMAI3 天前
Akamai Cloud Pulse 审计日志现已全面开放使用
人工智能·云计算
刘梦薇3 天前
SSH连接失败connection reset解决办法
linux·运维·ubuntu·ssh·腾讯云
workflower3 天前
智能无人机成低空经济核心赛道
运维·人工智能·机器学习·机器人·云计算·无人机
运维全栈笔记3 天前
腾讯云 CVM 搭建 Ceph 集群部署笔记
笔记·ceph·腾讯云
海兰3 天前
【AI工具】腾讯云开源自研 AI 助手 Octop介绍及安装使用指南
人工智能·云计算·腾讯云
谷哥的小弟4 天前
腾讯云服务器安装Redis Stack图文教程
服务器·redis·腾讯云·redis stack