腾讯云对象存储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();
        }
    }

测试

相关推荐
卖芒果的潇洒农民17 小时前
20260201 AWS VPC相关概念
云计算·aws
G311354227317 小时前
如何选择适合项目的云服务器配置?
腾讯云
李白你好19 小时前
基于腾讯云函数 (SCF) 的分布式 IP 代理池.
分布式·tcp/ip·腾讯云
Genie cloud1 天前
1Panel SSL证书申请完整教程
服务器·网络协议·云计算·ssl
JiL 奥2 天前
Ubuntu系统安装AWS SAM
云计算·aws
liyuanchao_blog2 天前
linuxptp适配记录
linux·云计算
YongCheng_Liang2 天前
从零开始学虚拟化:性能优化全指南(资源分配 + 存储网络 + 监控)
运维·云计算
YongCheng_Liang2 天前
从零开始学虚拟化:高可用与灾备技术全解析(集群 + 备份 + 异地灾备)
运维·云计算
珠海西格2 天前
“主动预防” vs “事后补救”:分布式光伏防逆流技术的代际革命,西格电力给出标准答案
大数据·运维·服务器·分布式·云计算·能源
xianyinsuifeng3 天前
RAG + Code Analysis 的标准路线
数据仓库·自动化·云计算·原型模式·aws