Newer
Older
newfiber-termite / newfiber-termites / newfiber-termites-dataup / src / main / java / com / newfiber / termite / util / CosUtil.java
@xiongkai xiongkai on 5 Jun 4 KB 对接云存储
package com.newfiber.termite.util;

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.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;

public class CosUtil {
    private static String secretId = "AKIDoOp3B8JLacTElCnM5bbxHOGuHsGOd5NB";
    private static String secretKey = "lK8xY6MAkm8F6kQr1xkHYJp9EgtpVv87";
    private static String bucketName = "termite-1255570142";
    private static String region = "ap-nanjing";
    private static COSClient cosClient = createCli();;
    private static String url = "https://termite-1255570142.cos.ap-nanjing.myqcloud.com/";

    private static COSClient createCli() {
        // 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
        // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        // 生成cos客户端
        return new COSClient(cred, clientConfig);
    }

    public static String putFile(MultipartFile file){

        try {
            String fileName = FileUploadUtils.extractFilename(file);
            InputStream inputStream = file.getInputStream();
            ObjectMetadata objectMetadata = new ObjectMetadata();

            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream, objectMetadata);
            // 设置单链接限速(如有需要),不需要可忽略
            putObjectRequest.setTrafficLimit(8*1024*1024);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
//            System.out.println(putObjectResult.getRequestId());
            return url + fileName;
        } catch (CosServiceException e) {
            e.printStackTrace();
        } catch (CosClientException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    private static void putLocalFileDemo() {
        String key = "abc/abc.txt";
        String localPath = "abc.txt";

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));
        try {
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            System.out.println(putObjectResult.getRequestId());
        } catch (CosServiceException cse) {
            cse.printStackTrace();
        } catch (CosClientException cce) {
            cce.printStackTrace();
        }
    }

    private static void putInputStreamDemo() {
        String key = "abc/def.txt";
        // 这里创建一个 ByteArrayInputStream 来作为示例,实际中这里应该是您要上传的 InputStream 类型的流
        int inputStreamLength = 1024 * 1024;
        byte data[] = new byte[inputStreamLength];
        InputStream inputStream = new ByteArrayInputStream(data);

        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 上传的流如果能够获取准确的流长度,则推荐一定填写 content-length
        // 如果确实没办法获取到,则下面这行可以省略,但同时高级接口也没办法使用分块上传了
        objectMetadata.setContentLength(inputStreamLength);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
        // 设置单链接限速(如有需要),不需要可忽略
        putObjectRequest.setTrafficLimit(8*1024*1024);
        try {
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            System.out.println(putObjectResult.getRequestId());
        } catch (CosServiceException e) {
            e.printStackTrace();
        } catch (CosClientException e) {
            e.printStackTrace();
        }
    }
}