腾讯云对象存储工具类 Java代码 即开即用

该代码示例展示了如何使用腾讯云COSClient进行文件的上传、下载和删除操作。它初始化了COSCredentials和ClientConfig,定义了存储桶的区域和名称,并提供了静态方法处理文件操作。
摘要由CSDN通过智能技术生成

腾讯云对象存储工具类

package com.seed.pro.utils;  
  
import com.qcloud.cos.COSClient;  
import com.qcloud.cos.ClientConfig;  
import com.qcloud.cos.auth.BasicCOSCredentials;  
import com.qcloud.cos.auth.COSCredentials;  
import com.qcloud.cos.exception.CosClientException;  
import com.qcloud.cos.exception.CosServiceException;  
import com.qcloud.cos.model.*;  
import com.qcloud.cos.region.Region;  
  
import java.io.File;  
import java.util.Calendar;  
import java.util.Random;  
  
  
/**  
 * 腾讯云对象存储工具类  
 *  
 */public class TencentCosUtil {  
  
    //bucket的区域  
 private static final String bucket = "ap-nanjing";  
    // 此处填写的存储桶名称  
 private static final String bucketName = "seed2-1302289795";  
    // secretId  
 private static final String secretId = "AKIDDG2YnkRr0sUptgDTSAcnhKgmb9QRCgtp";  
    // secretKey  
 private static final String secretKey = "i9JCpBG4fz3qMddMo0vZw3tSkkTv72h5";  
  
    // 1 初始化用户身份信息(secretId, secretKey,可在腾讯云后台中的API密钥管理中查看!  
 private static COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);  
  
    // 2 设置bucket的区域, COS地域的简称请参照  
 // https://cloud.tencent.com/document/product/436/6224,根据自己创建的存储桶选择地区  
 private static ClientConfig clientConfig = new ClientConfig(new Region(bucket));  
  
  
    private static String FilePath = "img";  
    /**  
 * 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口 大文件上传请参照 API 文档高级 API 上传  
 *  
 * @param localFile  
 */ public static String uploadfile(File localFile) throws CosClientException, CosServiceException {  
        Calendar calendar = Calendar.getInstance();  
        int year = calendar.get(Calendar.YEAR);  
        int month = calendar.get(Calendar.MONTH)+1;  //注意加1  
 int day = calendar.get(Calendar.DATE);  
  
        // 生成cos客户端  
 COSClient cosclient = new COSClient(cred, clientConfig);  
        String fileName = localFile.getName();  
        try {  
            String substring = fileName.substring(fileName.lastIndexOf("."));  
            Random random = new Random();  
            // 指定要上传到 COS 上的路径  
 //random.nextInt(10000) + System.currentTimeMillis()或者UUID  
 fileName = FilePath+"/"+year+"/"+month+"/"+day+"/"+ random.nextInt(10000) + System.currentTimeMillis()+ substring;  
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName , localFile);  
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            // 关闭客户端(关闭后台线程)  
 cosclient.shutdown();  
        }  
        return fileName;  
    }  
  
  
    /**  
 * * @Title: downFile  
 * @Description: 下载文件  
 * @return  
 */  
 public static void downFile() {  
        // 生成cos客户端  
 COSClient cosclient = new COSClient(cred, clientConfig);  
        //要下载的文件路径和名称  
 String key = "down/demo1.jpg";  
        // 指定文件的存储路径  
 File downFile = new File("src/test/resources/mydown.txt");  
        // 指定要下载的文件所在的 bucket 和对象键  
 GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);  
        ObjectMetadata downObjectMeta = cosclient.getObject(getObjectRequest, downFile);  
    }  
  
  
    /**  
 * 删除文件  
 *  
 * @param key  
 */ public static void deletefile(String key) throws CosClientException, CosServiceException {  
        // 生成cos客户端  
 COSClient cosclient = new COSClient(cred, clientConfig);  
        // 指定要删除的 bucket 和路径  
 cosclient.deleteObject(bucketName, key);  
        // 关闭客户端(关闭后台线程)  
 cosclient.shutdown();  
    }  
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葛济维的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值