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

测试

相关推荐
Database_Cool_2 小时前
阿里云 RDS MySQL 降本增效实战:从规格选型到成本优化,月成本降低64%全攻略
mysql·阿里云·云计算
Smoothcloud润云11 小时前
国内GPU算力租赁平台横向测评:资源、成本、稳定性三维对比
人工智能·ai·云计算·gpu算力·gpu
故乡de云18 小时前
AWS 绑卡失败排查全流程:预授权、3D Secure 与账单信息不一致的完整定位方法
云计算·aws
行业研究员19 小时前
AI原生的商城智能经营平台合作商推荐:腾讯云Mall
云计算·腾讯云·ai-native
故乡de云20 小时前
AWS 更换信用卡完整教程:绑定新卡、切换默认支付方式与扣款失败排查
云计算·aws
酷可达拉斯21 小时前
Linux操作系统-shell编程(0)
linux·运维·服务器·python·云计算
huijingjituan1 天前
三方聊天软件工具定制开发|打造企业级智能通讯平台
数据库·安全·阿里云·实时互动·腾讯云
国科安芯1 天前
空间机电一体化:AS32S601型抗辐射MCU在卫星推进与执行机构控制中的关键技术研究
单片机·嵌入式硬件·物联网·机器人·云计算
Qianyunzhiyun2 天前
AWS根用户MFA怎么设置?新账号安全教程
安全·云计算·aws
2601_955759412 天前
AWS 账单支付失败会怎样?NiceCloud 说明影响与补救
云计算·aws