package com.jeeplus.common.oss;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import com.jeeplus.common.config.Global;

public class UploadSample {
    private static String endpoint = Global.getEndpoint();
    private static String key = "<downloadKey>";
    private static String uploadFile = "<uploadFile>";
    private static String accessKeyId = Global.getAccessKeyId();
    private static String accessKeySecret = Global.getAccessKeySecret();
    private static String bucketName = Global.getBucketName();
    private String savePath = Global.getAliyunUrl();
    private String avatarDir = Global.getAvatarDir();
    private String notifyDir = Global.getNotifyDir();
    private String reportDir = Global.getReportDir();
    private String rqcode = Global.getRqcode();
    private String goout = Global.getGoout();
    private String leave = Global.getLeave();
    private String overtimeform = Global.getOvertimeform();
    private String sealform = Global.getSealform();
    private String evection = Global.getEvection();
    private String photo = Global.getPhoto();
    private String userEvaluation = Global.getUserEvaluation();
    private String workBidingDocument = Global.getWorkBidingDocument();
    private String workEngineeringProject = Global.getWorkEngineeringProject();
    private String workFullExecute = Global.getWorkFullExecute();
    private String workFullMeetingminutes = Global.getWorkFullMeetingminutes();
    private String workFullDesignchange = Global.getWorkFullDesignchange();
    private String workFullConstructsheet = Global.getWorkFullConstructsheet();
    private String workFullProprietorsheet = Global.getWorkFullProprietorsheet();
    private String workFullSupervisorsheet = Global.getWorkFullSupervisorsheet();
    private String workProjectReport = Global.getWorkProjectReport();
    private String workProjectBasis = Global.getWorkProjectBasis();
    private String workProjectRemote = Global.getWorkProjectRemote();
    private String workProjectSummary = Global.getWorkProjectSummary();
    private String workProjectOther = Global.getWorkProjectOther();
    private String workVisa = Global.getWorkVisa();
    private String Officehonor = Global.getOfficehonor();
    private String jobResume = Global.getJobResume();
    private String satisfaction = Global.getSatisfaction();
    private String certificate = Global.getCertificate();


    public static void main(String[] args) throws IOException {
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        String accessKeyId = "LTAIi7VuxcgfJR2x";
        String accessKeySecret = "Q9xF9V7tcnCI28ttUsP8H4GyAhZta7";
        String bucketName = "gangwan-app";
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        String key = "app-data/appfiles/932059357.war";
        String url = "F:\\code\\jeeplus_maven\\target\\jeeplus.war";
        List<PartETag> partETags = new ArrayList<PartETag>();
        InputStream instream = new FileInputStream(new File(url));
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult result = ossClient.initiateMultipartUpload(request);
        String uploadId = result.getUploadId();
        uploadPartRequest.setUploadId(uploadId);
        uploadPartRequest.setInputStream(instream);
        // 设置分片大小，除最后一个分片外，其它分片要大于100KB
        uploadPartRequest.setPartSize(100 * 1024);
        // 设置分片号，范围是1~10000，
        uploadPartRequest.setPartNumber(1);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());

        try {
            // 带进度条的上传
            ossClient.putObject(new PutObjectRequest(bucketName, key, new FileInputStream(url)).
                    <PutObjectRequest>withProgressListener(new PutObjectProgressListener()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ossClient.shutdown();
        /*OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        try {
            UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, "attachment-file/workBidingDocument/3fcf03e37b3ba0fd5c7821998e392395.doc");
            // The local file to upload---it must exist.
            uploadFileRequest.setUploadFile("F:\\2017082515");
            // Sets the concurrent upload task number to 5.
            uploadFileRequest.setTaskNum(5);
            // Sets the part size to 1MB.
            uploadFileRequest.setPartSize(1024 * 1024 * 1);
            // Enables the checkpoint file. By default it's off.
            uploadFileRequest.setEnableCheckpoint(true);

            UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);

            CompleteMultipartUploadResult multipartUploadResult =
                    uploadResult.getMultipartUploadResult();
            System.out.println(multipartUploadResult.getETag());

        } 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());
        } 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());
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();
        }*/
    }
}
