java接入腾讯云COS

1、引入依赖

<!--腾讯云 COS-->
        <dependency>
            <groupId>com.qcloud</groupId>
            <artifactId>cos_api</artifactId>
            <version>5.6.54</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.qcloud</groupId>
            <artifactId>qcloud-java-sdk</artifactId>
            <version>2.0.6</version>
        </dependency>

2.腾讯云cos工具类

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.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import java.io.*;
/*
 * 腾讯云cos工具类
 * */
public class TencentUtils {
    static String bucketName = "你自己的"; //桶的名称
    static COSCredentials cred = null;
    static ClientConfig clientConfig = null;
    static COSClient cosClient = null;
    static {
        // 1 初始化用户身份信息(secretId, secretKey)。
        String secretId = "你自己的";
        String secretKey = "你自己的";
        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("你自己的");
        clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        cosClient = new COSClient(cred, clientConfig);
    }
    public static void main(String[] args) throws IOException {
//        upload2cloud("C:\\XX\\XX\\XX\\11.gif","test");
//        InputStream in = new FileInputStream("C:\\Users\\Administrator\\Desktop\\11.gif");
//        byte[] bytes = new byte[in.available()];
//        upload2cloud(bytes,"ss");
//        deleteFileFromcloud("test");
    }
    public static void upload2cloud(String filepath,String fileName){
        // 指定要上传的文件
        File localFile = new File(filepath);
        // 指定文件上传到 COS 上的路径,即对象键。例如对象键为folder/picture.jpg,则表示将文件 picture.jpg 上传到 folder 路径下
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        String eTag = putObjectResult.getETag();
        System.out.println(eTag);
        /**关闭*/
        cosClient.shutdown();
    }
    
    public static void upload2cloud(byte[] bytes,String fileName){
        int length = bytes.length;
        // 获取文件流
        InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        ObjectMetadata objectMetadata = new ObjectMetadata();
// 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据,存在内存OOM的情况
        objectMetadata.setContentLength(length);
// 默认下载时根据cos路径key的后缀返回响应的contenttype, 上传时设置contenttype会覆盖默认值
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, byteArrayInputStream, objectMetadata);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        String eTag = putObjectResult.getETag();
        System.out.println(eTag);
        /**关闭*/
        cosClient.shutdown();
    }
    
    public static void deleteFileFromcloud(String fileName){
        cosClient.deleteObject(bucketName, fileName);
        /**关闭*/
        cosClient.shutdown();
    }
}

注意:文件的访问权限一定要设置公有,很重要!!!!!!!
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Keep up

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

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

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

打赏作者

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

抵扣说明:

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

余额充值