package com.ruoyi;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
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 org.joda.time.DateTime;
import org.junit.Test;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

@Component
public class TencentCosUtilTest {
    //    accessKey: d8dd89e4275bbe242824511aba875d11b491b1c30811e7cd6010dca97ac2a69f1ab5a5305e4c3dfb5c561d95c7e3b2c2
//    secretKey: 2284e285ac55f67cc9774c27de0bfed0dbca5930c02af8e5473b66c21e5770a38e64ce873f174dbb2423fcd814580e15
//    endpoint: cos.na-ashburn.myqcloud.com
//    bucketName: gamerwa-1331490964
//    domain: https://gamerwa-1331490964.cos.accelerate.myqcloud.com
    // 设置好账号的ACCESS_KEY和SECRET_KEY
    private String SECRET_ID = "AKIDHWbUDk5ELJ4e1EG6jBRaVCi9PTN4Ypup";
    private String SECRET_KEY = "obBammdIm5FFJFzazAAxWGtNhqnY8Vil";
    // 要上传的空间（创建空间的名称）
    private String bucketName = "gamerwa-1331490964";
    private String endpoint = "cos.na-ashburn.myqcloud.com";
    // 使用的是测试域名
    private String domain = "https://gamerwa-1331490964.cos.accelerate.myqcloud.com";
    // 地域标识
    private String regionId = "na-ashburn";

    public static void main(String[] args) {
        // 手动实例化当前类
        TencentCosUtilTest cosUtil = new TencentCosUtilTest();
        // 调用上传方法
        cosUtil.upload();
    }

    @Test
    public void upload() {

        // 3 生成 cos 客户端。
        String url = "C:\\Users\\Administrator\\Desktop\\darkiris\\任务栏icon.png";
        File file = new File(url);
        // 存储桶的命名格式为 BucketName-APPID，此处填写的存储桶名称必须为此格式
        // 对象键(Key)是对象在存储桶中的唯一标识。  998u-09iu-09i-333
        //在文件名称前面添加uuid值
        String key = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8) + "_"
                + file.getName();
        //对上传文件分组，根据当前日期  /2022/11/11
        String dateTime = new DateTime().toString("yyyy/MM/dd");
        key = dateTime + "/" + key;
        COSClient cosClient = createCosClient();
        try {
            //获取上传文件输入流
            InputStream inputStream = new FileInputStream(file);
            ObjectMetadata objectMetadata = new ObjectMetadata();
            PutObjectRequest putObjectRequest = new PutObjectRequest(
                    bucketName,
                    key,
                    inputStream,
                    objectMetadata);
            // 高级接口会返回一个异步结果Upload
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);

            //返回上传文件路径
            //https://ggkt-atguigu-1310644373.cos.ap-beijing.myqcloud.com/01.jpg
            System.out.println(domain + "/" + key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private COSClient createCosClient() {
        //1.1 初始化用户身份信息
        BasicCOSCredentials credentials = new BasicCOSCredentials(SECRET_ID, SECRET_KEY);
        //1.2 设置bucket的地域
        Region region = new Region(regionId);
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        clientConfig.setHttpProtocol(HttpProtocol.https);
        clientConfig.setConnectionTimeout(30 * 1000); // 连接超时30秒
        clientConfig.setSocketTimeout(60 * 1000);    // 读取超时60秒
        //1.3 生成cos客户端
        return new COSClient(credentials, clientConfig);
    }
}
