package com.yizhi.application.caseLibrary.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;
import com.netflix.client.ClientException;

public class OssUpload {

    public static String upload(String uploadFile,String key,String active){


        String endpoint = "";
        String accessKeyId = "";
        String accessKeySecret = "";
        String bucketName = "";

        if ("dev".equals(active)||"sit".equals(active)||"uat".equals(active)) {
             endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
             accessKeyId = "LTAInZlfTwk5fCDJ";
             accessKeySecret = "UavtbFb2vUvRxiwUMLvUEelXTQtHCB";
             bucketName = "fulan-test";
        } else {
             endpoint = "https://oss-cn-shanghai.aliyuncs.com";
             accessKeyId = "LTAIeBliOjnD6v25";
             accessKeySecret = "qd6iAV7tUvumvEiXxv9IGJbSdHcnmH";
             bucketName = "cloud-wmy";
        }

        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        try {
            UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
            // 待上传的本地文件
            uploadFileRequest.setUploadFile(uploadFile);
            // 设置并发下载数，默认1
            uploadFileRequest.setTaskNum(5);
            // 设置分片大小，默认100KB
            uploadFileRequest.setPartSize(1024 * 1024 * 1);
            // 开启断点续传，默认关闭
            uploadFileRequest.setEnableCheckpoint(true);

            UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);

            CompleteMultipartUploadResult multipartUploadResult =
                    uploadResult.getMultipartUploadResult();

            System.out.println(multipartUploadResult.getLocation());

            return multipartUploadResult.getLocation();
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorCode());
            System.out.println("Error Code:       " + oe.getErrorCode());
            System.out.println("Request ID:      " + oe.getRequestId());
            System.out.println("Host ID:           " + oe.getHostId());
            return "error";
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ce.getMessage());
            return "error";
        } catch (Throwable e) {
            e.printStackTrace();
            return "error";
        } finally {
            ossClient.shutdown();
        }
    }


}
