package com.search.supersearch.demo.oss;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/oss")
@Slf4j
public class OssUpload {


    public static final String BUCKET = "dsj-sjxt-testbucket01";
    private static final String endpoint = "http://oss-cn-anshun-dtcloud-d01-a.ops.dtcloud.spic";
    private static final String accessKeyId = "G4yqKKVMwEVfwDPg";
    private static final String accessKeySecret = "rkxHoNWeYJRfgX1NaaB2qjvcU72Spk";

/*    private static final String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    private static final String accessKeyId = "LTAI5tPoP9JgGLLDorkfyWEd";
    private static final String accessKeySecret = "wRYrd8XK6gDMldbVc3KImiNQLwqear";
    public static final String BUCKET = "platomixtest";*/

    @GetMapping("/getAll")
    public void getAll() throws IOException {
        log.info("getAll - start");
        OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//        ObjectListing objectListing = client.listObjects(BUCKET, "itemfile");
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(BUCKET, "itemfile", null, null, 1000);
        ObjectListing objectListing = client.listObjects(listObjectsRequest);
        log.info("========================================");
        log.info("文件总数={}", objectListing.getObjectSummaries().size());
        for (OSSObjectSummary summary : objectListing.getObjectSummaries()) {
            if (summary.getSize() > 0L) {
                log.info("========================================");
                log.info("getAll-summary-key = {}", summary.getKey());
                log.info("getAll-summary-size = {}", summary.getSize());
            }
        }

    }


    @GetMapping("/getTest")
    public void getTest() throws IOException {
        log.info("getTest- FileItemPurchase - start");
        String fileUrl = "/root/oss_demo/untitled.png";
        OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        InputStream inputStream = new FileInputStream(fileUrl);
        //String suffix = fileUrl.substring(fileUrl.lastIndexOf("."));
        //Long startNum = 1000000000000177L;
        String objectName = "itemfile/test.png";
        //oss(fileUrl,suffix,objectName);
        PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET, objectName, inputStream);
        PutObjectResult putObjectResult = client.putObject(putObjectRequest);
        //System.out.println("Object：" + objectName + "的内容是：" + objectContent);
        //log.info("getTest - Object 内容 = {}",objectContent);
        client.shutdown();
        log.info("getTest- FileItemPurchase - end");
    }


}
