腾讯云存储 cos

package com.jc.chile.booksgrab.utils;

import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.*;
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.region.Region;

import java.io.*;

/**
 * @Description 腾讯云存储
 * @Author liangzhiqing
 * @Date 2019/10/9 9:56
 */
public class CosConstants {
    //用户身份信息
    private final static String SECRET_ID = "";
    private final static String SECRET_KEY = "";
    //bucket 区域
    private final static String BUCKET_REGION = "";
    //存储桶
    private final static String BUCKET_NAME = "";
    private COSClient cosClient;

    public CosConstants(){
        COSCredentials credentials = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
        //设置区域
        Region region = new Region(BUCKET_REGION);
        ClientConfig clientConfig = new ClientConfig(region);
        //生成COS客户端
        cosClient = new COSClient(credentials, clientConfig);
    }

    //上传不超过5G的文件
    //key:上传到COS的键,例如:key = images/picture.jpg  上传后访问地址是:xxxxx/images/picture.jpg
    public void uploadSmallFile(String key, File file){
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, key, file);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        } catch (CosServiceException serverException){
            serverException.printStackTrace();
        } catch (CosClientException clientException) {
            clientException.printStackTrace();
        } finally {
            closeCosClient();
        }
    }

    //输入流上传
    public void uploadSmallFile(String key, InputStream inputStream){
        try {
            ObjectMetadata objectMetadata = new ObjectMetadata();
            //设置输入流长度为500
            objectMetadata.setContentLength(inputStream.available());
            // 设置 Content type, 默认是 application/octet-stream
            //objectMetadata.setContentType("application/pdf");
            PutObjectResult putObjectResult = cosClient.putObject(BUCKET_NAME, key, inputStream, objectMetadata);
           String etag = putObjectResult.getETag();
        } catch (CosServiceException serverException){
            serverException.printStackTrace();
        } catch (CosClientException clientException) {
            clientException.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeCosClient();
        }
    }

    //读取COS文件内容
    public String readerFileContent(String key){
        String content = null;
        BufferedReader reader = null;
        try {
            //获取下载输入流
            GetObjectRequest getObjectRequest = new GetObjectRequest(BUCKET_NAME, key);
            COSObject cosObject = cosClient.getObject(getObjectRequest);
            COSObjectInputStream cosObjectInputStream = cosObject.getObjectContent();
            reader = new BufferedReader(new InputStreamReader(cosObjectInputStream));
            byte[] buf = new byte[1024];
            String line = null;
            while ((line = reader.readLine()) != null){
                content += line;
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭cosClient
            closeCosClient();
        }
        return content;
    }

    public static void main(String[] arts){
        CosConstants cosConstants = new CosConstants();
        String content = cosConstants.readerFileContent("novel/万古术皇雕龙画凤/第七百零四章暂时结盟 巧骗蝼蚁.txt");
        System.out.println(content);
    }

    //关闭cosClient
    private void closeCosClient(){
        this.cosClient.shutdown();
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值