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

测试

相关推荐
IT·小灰灰10 小时前
告别“翻墙“烦恼:DMXAPI让Gemini-3-pro-thinking调用快如闪电
网络·人工智能·python·深度学习·云计算
Lynnxiaowen11 小时前
今天我们继续学习kubernetes内容Helm
linux·学习·容器·kubernetes·云计算
我是六月生14 小时前
于windows环境下,使用阿里云oss搭建在线图床,Obsidian/Typora自动将图片上传,实现图片粘贴复制
笔记·阿里云·云计算
SaaS_Product15 小时前
有没有像OneDrive一样的自动同步网盘?
人工智能·云计算·saas·onedrive
@HNUSTer19 小时前
基于 GEE 的 Landsat 8 数据构建遥感生态指数(RSEI)并进行生态质量评估
云计算·数据集·遥感大数据·gee·云平台·遥感生态指数(rsei)·landsat 8
骚团长19 小时前
阿里云99元一年的云服务器部署Centos7.9通过MobaXterm安装RustDesk(图文并茂)
阿里云·云计算
屋外雨大,惊蛰出没20 小时前
如何在spring boot项目中利用腾讯云上传存储文件
云计算·腾讯云
风为你而吹21 小时前
【传统自建idc和云计算的比较】
云计算
风为你而吹21 小时前
【超融合架构和传统云计算架构】
架构·云计算
风为你而吹1 天前
【云计算和云原生】
云原生·云计算