package com.doumee.core.oss; 
 | 
  
 | 
import com.aliyun.oss.ClientException; 
 | 
import com.aliyun.oss.OSS; 
 | 
import com.aliyun.oss.OSSClientBuilder; 
 | 
import com.aliyun.oss.OSSException; 
 | 
import com.aliyun.oss.model.BucketInfo; 
 | 
import com.doumee.biz.system.SystemDictDataBiz; 
 | 
import com.doumee.core.utils.Constants; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Service; 
 | 
  
 | 
import java.io.InputStream; 
 | 
import java.util.HashMap; 
 | 
import java.util.Map; 
 | 
  
 | 
@Service 
 | 
public class AliOssService { 
 | 
  
 | 
    @Autowired 
 | 
    private SystemDictDataBiz systemDictDataBiz; 
 | 
  
 | 
    /** 
 | 
     * oss上传文件流 
 | 
     * 
 | 
     * @param fullFileName 
 | 
     * @param file 
 | 
     * @return 
 | 
     */ 
 | 
    public Map uploadFile(String fullFileName, InputStream file) { 
 | 
        Map res = new HashMap(); 
 | 
        String endpoint = systemDictDataBiz.queryByCode(Constants.OSS,Constants.ENDPOINT).getCode(); 
 | 
        String accessKeyId = systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_ID).getCode(); 
 | 
        String accessKeySecret = systemDictDataBiz.queryByCode(Constants.OSS,Constants.ACCESS_KEY).getCode(); 
 | 
        String bucketName = systemDictDataBiz.queryByCode(Constants.OSS,Constants.BUCKETNAME).getCode(); 
 | 
        OSS ossClient = null; 
 | 
        try { 
 | 
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); 
 | 
            ossClient.putObject(bucketName, fullFileName, file); 
 | 
            String url = "https://" + bucketName + "." + endpoint + "/" + fullFileName; 
 | 
            ossClient.shutdown(); 
 | 
            res.put("code", "200"); 
 | 
            res.put("url", url); 
 | 
            res.put("msg", "上传成功"); 
 | 
        } catch (OSSException oe) { 
 | 
            res.put("code", "500"); 
 | 
            res.put("url", ""); 
 | 
            res.put("msg", "尝试操作OSS失败:(" + oe.getErrorCode() + "," + oe.getMessage() + "),请登录阿里云后台核实BucketName域名空间或其它信息"); 
 | 
        } catch (ClientException ce) { 
 | 
            res.put("code", "500"); 
 | 
            res.put("url", ""); 
 | 
            res.put("msg", "尝试连接OSS失败:(" + ce.getMessage() + "),请登录阿里云后台核实"); 
 | 
        } finally { 
 | 
            if (ossClient != null) { 
 | 
                ossClient.shutdown(); 
 | 
            } 
 | 
        } 
 | 
        return res; 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    /** 
 | 
     * 空间校验 
 | 
     * 
 | 
     * @param endpoint 
 | 
     * @param accessKeyId 
 | 
     * @param accessKeySecret 
 | 
     * @param bucketName 
 | 
     * @return 
 | 
     */ 
 | 
    public Map vaid(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) { 
 | 
        Map res = new HashMap(); 
 | 
        OSS ossClient = null; 
 | 
        try { 
 | 
            ossClient = new OSSClientBuilder().build(endpoint.trim(), accessKeyId.trim(), accessKeySecret.trim()); 
 | 
            BucketInfo info = ossClient.getBucketInfo(bucketName.trim()); 
 | 
            String location = info.getBucket().getLocation(); 
 | 
            if (!endpoint.trim().contains(info.getBucket().getLocation())) { 
 | 
                res.put("code", "500"); 
 | 
                res.put("msg", "Endpoint校验失败(检测" + bucketName + "结果为" + location + ",与您实际输入的Endpoint不符合,请前去更正)"); 
 | 
                return res; 
 | 
            } 
 | 
            if (info != null) { 
 | 
                res.put("code", "200"); 
 | 
                res.put("msg", "OSS校验成功"); 
 | 
            } else { 
 | 
                res.put("code", "500"); 
 | 
                res.put("msg", "bucketName空间不存在,请登录阿里云后台进行创建操作"); 
 | 
            } 
 | 
        } catch (OSSException oe) { 
 | 
            res.put("code", "500"); 
 | 
            res.put("msg", "尝试操作OSS失败:(" + oe.getErrorCode() + "," + oe.getMessage() + "),请登录阿里云后台核实BucketName域名空间或其它信息"); 
 | 
        } catch (ClientException ce) { 
 | 
            res.put("code", "500"); 
 | 
            res.put("msg", ce.getMessage()); 
 | 
            res.put("msg", "尝试连接OSS失败:(" + ce.getMessage() + "),请登录阿里云后台核实"); 
 | 
        } finally { 
 | 
            if (ossClient != null) { 
 | 
                ossClient.shutdown(); 
 | 
            } 
 | 
        } 
 | 
        return res; 
 | 
    } 
 | 
} 
 |