package com.tzld.piaoquan.recommend.model.produce.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.tzld.piaoquan.recommend.model.produce.util.CompressUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.Serializable;

/**
 * @author dyp
 */
@Slf4j
public class OSSService implements Serializable {
    private String accessId = "LTAI5tHMkNaRhpiDB1yWMZPn";
    private String accessKey = "XLi5YUJusVwbbQOaGeGsaRJ1Qyzbui";
    //private String endpoint = "https://oss-cn-hangzhou-internal.aliyuncs.com";
    private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

    public void upload(String bucketName, String localFile, String objectName) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessId, accessKey);
        try {
            PutObjectRequest request = new PutObjectRequest(bucketName, objectName, new File(localFile));
            ossClient.putObject(request);
        } catch (Exception e) {
            log.error("upload error bucketName {}, localFile {}, objectName {}", bucketName, localFile, objectName, e);
        }
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }

    public void download(String bucketName, String localFile, String objectName) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessId, accessKey);
        try {
            GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
            ossClient.getObject(request, new File(localFile));
            System.out.println("");
        } catch (Exception e) {
            log.error("download error bucketName {}, localFile {}, objectName {}", bucketName, localFile, objectName, e);
        }
        if (ossClient != null) {
            ossClient.shutdown();
        }
    }

    public static void main(String[] args) {

        String bucketName = "art-test-video";
        String objectName = "test/model.tar.gz";
        OSSService ossService = new OSSService();


//        String inputPath = "/Users/dingyunpeng/Desktop/model";
//        String outputPath = "/Users/dingyunpeng/Desktop/model.tar.gz";
//        CompressUtil.compressDirectoryToGzip(inputPath, outputPath);
//
//        String ossPath = "test/model.tar.gz";
//        ossService.upload(bucketName, outputPath, ossPath);


        String destPath = "/Users/dingyunpeng/Desktop/model2.tar.gz";
        ossService.download(bucketName, destPath, objectName);
        String destDir = "/Users/dingyunpeng/Desktop/model2";
        CompressUtil.decompressGzFile(destPath, destDir);

    }
}
