package com.fzm.mall.server.admin.util;

import com.obs.services.ObsClient;
import com.obs.services.model.ObjectMetadata;

import java.io.*;
import java.net.URL;

/**
 * @author wangp
 * @date 2021/5/25 17:47
 * @description 华为云对象存储
 * @since JDK 1.8
 */
public class OBSUtil {

    private static String accessKey = "HIULSHJZJOTWB72Z38ZB"; //取值为获取的AK LTAI4GCoj6DGZx5JgUYVLs3P
    private static String securityKey = "GI0oWEGRJuMKmPXJq2fCmuMpFPIITLmjeCQxo2tA";  //取值为获取的SK 7TEBqdFmFtKT7AFDhh1DNgdoUSosYJ
    private static String endpoint = "https://obs.cn-east-3.myhuaweicloud.com"; //终端节点（Endpoint）
    private static String region = "cn-east-3"; // 取值为规划桶所在的区域
    private static String bucket = "yimu-file"; //存储桶
    private static String baseUrl = "https://yimu-file.obs.cn-east-3.myhuaweicloud.com/";

    public static void main(String[] str) {
        String S = uploadFile("C:\\Users\\bl\\Desktop\\链命令.txt", "manage/img/ttt", "a.txt");
        System.err.println(S);    }

    // 创建ObsClient实例
    public static ObsClient getObsClient() {
        return new ObsClient(accessKey, securityKey, endpoint);
    }

    /**
     * 上传Byte数组
     *
     * @param content  待上传字节数组
     * @param folder   OSS文件夹名
     * @param fileName OSS文件名
     * @return OSS访问地址
     */
    public static String uploadByte(byte[] content, String folder, String fileName) {
        // 创建ObsClient实例。
        ObsClient obsClient = getObsClient();

        // 上传Byte数组。
        obsClient.putObject(bucket, folder + fileName, new ByteArrayInputStream(content));

        // 关闭obsClient
        try {
            obsClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return baseUrl + folder + fileName;
    }

    /**
     * 上传网络流
     *
     * @param spec     网络地址
     * @param folder   OSS文件夹名
     * @param fileName OSS文件名
     * @return OSS访问地址
     */
    public static String uploadNet(String spec, String folder, String fileName) {
        // 创建ObsClient实例。
        ObsClient obsClient = getObsClient();

        // 上传网络流。
        InputStream inputStream = null;
        try {
            inputStream = new URL(spec).openStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        obsClient.putObject(bucket, folder + fileName, inputStream);

        // 关闭obsClient
        try {
            obsClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return baseUrl + folder + fileName;
    }

    /**
     * 上传文件流
     *
     * @param filePath 待上传文件路径
     * @param folder   OSS文件夹名
     * @param fileName OSS文件名
     * @return OSS访问地址
     */
    public static String uploadFile(String filePath, String folder, String fileName) {
        // 创建ObsClient实例。
        ObsClient obsClient = getObsClient();
        System.err.println("obsClient:" + obsClient);


        // 上传文件流。
        FileInputStream fis = null;  // 待上传的本地文件路径，需要指定到具体的文件名
        try {
            fis = new FileInputStream(new File(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        obsClient.putObject(bucket, folder + fileName, fis);

        // 关闭obsClient
        try {
            obsClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return baseUrl + folder + fileName;
    }

    /**
     * 上传文件流
     *
     * @param in       待上传文件流
     * @param folder   OSS文件夹名
     * @param fileName OSS文件名
     * @return OSS访问地址
     */
    public static String uploadStream(InputStream in, String folder, String fileName) {
        // 创建ObsClient实例。
        ObsClient obsClient = getObsClient();

        // 上传Byte数组。
        obsClient.putObject(bucket, folder + fileName, in);

        // 关闭obsClient
        try {
            obsClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return baseUrl + folder + fileName;
    }

}
