AliyunOssAttachmentServiceImpl.java 25.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
package com.hotent.file.attachmentService;

import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.internal.OSSHeaders;
import com.aliyun.oss.model.*;
import com.hotent.base.attachment.Attachment;
import com.hotent.base.attachment.AttachmentService;
import com.hotent.base.attachment.MultipartFileParam;
import com.hotent.base.attachment.UploadShardResult;
import com.hotent.base.cache.annotation.*;
import com.hotent.base.constants.CacheKeyConst;
import com.hotent.base.util.*;
import com.hotent.file.model.AliyunOssObject;
import com.hotent.file.model.AliyunOssSettings;
import com.hotent.file.model.UploadProperties;
import com.hotent.file.service.FlowUploadPropertiesService;
import com.hotent.file.util.AppFileUtil;
import io.jsonwebtoken.lang.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class AliyunOssAttachmentServiceImpl implements AttachmentService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final String aliyunOssTempDir=File.separator+"aliyunOssTempDir";

    @Resource
    FlowUploadPropertiesService flowUploadPropertiesService;
    @Resource
    AliyunOssAttachmentServiceImpl aliyunOssAttachmentService;

    private AliyunOssObject initOssObject(Attachment attachment, String propertiesId) {
        AliyunOssObject aliyunOssObject=new AliyunOssObject();
        AliyunOssSettings ossSettings = AppUtil.getBean(AliyunOssSettings.class);
        aliyunOssObject.setAliyunOssSettings(ossSettings);
        setObjectKeyAndFilePath(attachment, aliyunOssObject);
        if (StringUtil.isNotEmpty(propertiesId)) {
            UploadProperties uploadProperties = flowUploadPropertiesService.getUploadProperties(propertiesId, ossSettings);
            attachment.setEntryptName(uploadProperties.getEncryptName()== 0 ? false : true);
            setObjectKeyAndFilePath(attachment, aliyunOssObject);
        }
        return aliyunOssObject;
    }

    // 设置 ossSettings 的 objectKey和 attachment 的 filePath
    private void setObjectKeyAndFilePath(Attachment attachment, AliyunOssObject ossObject) {
        String fileParentPath = "";
        String filePath = attachment.getFilePath();
        if (StringUtil.isNotEmpty(filePath)) {
            fileParentPath = Paths.get(filePath).getParent().toString();
            fileParentPath = fileParentPath.replaceAll("\\\\", "/");
            if (fileParentPath.startsWith("/")) {
                fileParentPath = fileParentPath.substring(1);
            }
        }
        if (attachment.getEntryptName()) {
            filePath = fileParentPath + "/" + attachment.getId() + "." + attachment.getExtensionName();
        } else {
            filePath = fileParentPath + "/" + attachment.getId() + "_" + attachment.getFileName() + "." + attachment.getExtensionName();
        }
        ossObject.setObjectKey(filePath);
        attachment.setFilePath(filePath);
    }

    private String getFilePath(Attachment attachment){
        String fileParentPath = "";
        String filePath = attachment.getFilePath();
        if (StringUtil.isNotEmpty(filePath)) {
            fileParentPath = Paths.get(filePath).getParent().toString();
            fileParentPath = fileParentPath.replaceAll("\\\\", "/");
            if (fileParentPath.startsWith("/")) {
                fileParentPath = fileParentPath.substring(1);
            }
        }
        if (attachment.getEntryptName()) {
            filePath = fileParentPath + "/" + attachment.getId() + "." + attachment.getExtensionName();
        } else {
            filePath = fileParentPath + "/" + attachment.getId() + "_" + attachment.getFileName() + "." + attachment.getExtensionName();
        }
        return filePath;
    }

    @Override
    public void remove(Attachment attachment, String propertiesId) throws Exception {
        AliyunOssObject ossObject = initOssObject(attachment, propertiesId);
        AliyunOssSettings ossSettings=ossObject.getAliyunOssSettings();
        OSS ossClient = new OSSClientBuilder().build(ossSettings.getEndpoint(), ossSettings.getAccessKeyId(), ossSettings.getAccessKeySecret());
        try {
            if (!ossClient.doesBucketExist(ossSettings.getBucketName())) {
                ossClient.createBucket(ossSettings.getBucketName());
            }
            ossClient.deleteObject(ossSettings.getBucketName(), ossObject.getObjectKey());
        } finally {
            ossClient.shutdown();
        }
    }

    @Override
    public void upload(Attachment attachment, InputStream inputStream, String propertiesId) throws Exception {
        AliyunOssObject ossObject = initOssObject(attachment, propertiesId);
        AliyunOssSettings ossSettings=ossObject.getAliyunOssSettings();
        //setObjectKeyAndFilePath(attachment, ossSettings);
        String objectKey = this.getFilePath(attachment);
        OSS ossClient = new OSSClientBuilder().build(ossSettings.getEndpoint(), ossSettings.getAccessKeyId(), ossSettings.getAccessKeySecret());
        try {
            if (!ossClient.doesBucketExist(ossSettings.getBucketName())) {
                ossClient.createBucket(ossSettings.getBucketName());
            }
            if(BeanUtils.isEmpty(inputStream)) {
                ossClient.putObject(ossSettings.getBucketName(), objectKey, new ByteArrayInputStream(attachment.getBytes()));
            }else{
                ossClient.putObject(ossSettings.getBucketName(), objectKey, inputStream);
            }
        } finally {
            inputStream.close();
            ossClient.shutdown();
            removeTemp(attachment);
        }
    }

    @Override
    public void download(Attachment attachment, OutputStream outStream, String propertiesId) throws Exception {
        AliyunOssObject aliyunOssObject = initOssObject(attachment, propertiesId);
        AliyunOssSettings ossSettings=aliyunOssObject.getAliyunOssSettings();
        OSS ossClient = new OSSClientBuilder().build(ossSettings.getEndpoint(), ossSettings.getAccessKeyId(), ossSettings.getAccessKeySecret());
        try (OSSObject ossObject = ossClient.getObject(ossSettings.getBucketName(), aliyunOssObject.getObjectKey())) {
            if (!ossClient.doesBucketExist(ossSettings.getBucketName())) {
                ossClient.createBucket(ossSettings.getBucketName());
            }
            IoUtil.copy(ossObject.getObjectContent(), outStream);
        } finally {
            outStream.close();
            ossClient.shutdown();
        }
    }

    @Override
    public String getStoreType() {
        return "aliyunOss";
    }


    @Override
    public boolean chekckFile(Attachment attachment, String propertiesId) throws Exception {
        AliyunOssObject aliyunOssObject = initOssObject(attachment, propertiesId);
        AliyunOssSettings ossSettings=aliyunOssObject.getAliyunOssSettings();
        OSS ossClient = new OSSClientBuilder().build(ossSettings.getEndpoint(), ossSettings.getAccessKeyId(), ossSettings.getAccessKeySecret());
        try {
            if (!ossClient.doesBucketExist(ossSettings.getBucketName())) {
                ossClient.createBucket(ossSettings.getBucketName());
            }
            boolean found = ossClient.doesObjectExist(ossSettings.getBucketName(), aliyunOssObject.getObjectKey());
            return found;
        } finally {
            ossClient.shutdown();
        }
    }

    public String getLocalTempFilePath(Attachment attachment){
        String localFilePath=attachment.getFilePath();
        localFilePath = localFilePath.replace("/", File.separator);
        if(!attachment.getFilePath().startsWith(File.separator)){
            localFilePath=File.separator+localFilePath;
        }
        localFilePath=aliyunOssTempDir+localFilePath;
        return localFilePath;
    }

    @Override
    public byte[] getFileBytes(Attachment attachment) throws Exception {
        String localFilePath=getLocalTempFilePath(attachment);
        FileUtil.createFolderFile(localFilePath);
        File file=new File(localFilePath);
        if(!file.exists()){
            String propertiesId = BeanUtils.isNotEmpty(attachment)?attachment.getProp6():"";
            AliyunOssObject aliyunOssObject = this.initOssObject(attachment, propertiesId);
            AliyunOssSettings ossSettings=aliyunOssObject.getAliyunOssSettings();
            OSS ossClient = new OSSClientBuilder().build(ossSettings.getEndpoint(), ossSettings.getAccessKeyId(), ossSettings.getAccessKeySecret());
            try {
                if (!ossClient.doesBucketExist(ossSettings.getBucketName())) {
                    ossClient.createBucket(ossSettings.getBucketName());
                }
                ossClient.getObject(new GetObjectRequest(ossSettings.getBucketName(), attachment.getFilePath()), file);
            } finally {
                ossClient.shutdown();
            }
        }
        try(FileInputStream fileInputStream=new FileInputStream(file);
            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        ) {
            int read;
            final byte[] bytes = new byte[1024];
            while ((read = fileInputStream.read(bytes)) != -1) {
                byteArrayOutputStream.write(bytes, 0, read);
            }
            byteArrayOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        }
    }

    /**
     * 分片上传
     * @param multipartFileParam
     * @param attachment
     * @param propertiesId
     * @return
     * @throws Exception
     */


    @Override
    public UploadShardResult uploadByShard(MultipartFileParam multipartFileParam, Attachment attachment, String propertiesId) throws Exception {
        multipartFileParam.setUniqueKey(multipartFileParam.getIdentifier()+"_"+multipartFileParam.getUniqueIdCard());
        String uploadId=aliyunOssAttachmentService.getUploadIdFromCache(multipartFileParam.getUniqueKey());
        String fileId= aliyunOssAttachmentService.getIdentifierIdFromCache(multipartFileParam.getUniqueKey());
        OSS ossClient=null;
        try {
            if(StringUtil.isEmpty(fileId)){
                fileId=UniqueIdUtil.getSuid();
                aliyunOssAttachmentService.putIdentifierIdCache(multipartFileParam.getUniqueKey(),fileId);
            }
            attachment.setId(fileId);
            AliyunOssObject aliyunOssObject = initOssObject(attachment, propertiesId);
            AliyunOssSettings ossSettings=aliyunOssObject.getAliyunOssSettings();
            ossClient = new OSSClientBuilder().build(ossSettings.getEndpoint(), ossSettings.getAccessKeyId(), ossSettings.getAccessKeySecret());
            if (!ossClient.doesBucketExist(ossSettings.getBucketName())) {
                ossClient.createBucket(ossSettings.getBucketName());
            }
            //如果uploadId为空就是初始上传,需要初始化uploadId
            if(StringUtil.isEmpty(uploadId)){
                uploadId=initUploadId(multipartFileParam,attachment,aliyunOssObject,ossClient);
            }
            multipartFileParam.setAliyunOssUploadId(uploadId);
            //进行分片上传
            UploadShardResult uploadShardResult=partUpload(multipartFileParam,attachment,aliyunOssObject,ossClient);
            return uploadShardResult;
        }catch(Exception e){
            aliyunOssAttachmentService.delUploadIdCache(multipartFileParam.getUniqueKey());
            aliyunOssAttachmentService.delPartTagListCache(multipartFileParam.getUniqueKey());
            aliyunOssAttachmentService.delIdentifierIdCache(multipartFileParam.getUniqueKey());
            throw e;
        } finally {
            if(ossClient!=null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * 调用阿里云初始化上传id
     * @param multipartFileParam
     * @param attachment
     * @param aliyunOssObject
     * @return
     */
    public String initUploadId(MultipartFileParam multipartFileParam,Attachment attachment,AliyunOssObject aliyunOssObject,OSS ossClient) {
        AliyunOssSettings ossSettings=aliyunOssObject.getAliyunOssSettings();
        // 文件名称
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        metadata.setCacheControl("no-cache");
        //设置文件为私有
        metadata.setObjectAcl(CannedAccessControlList.Private);
        //指定该Object被下载时的名称。
        metadata.setContentDisposition("attachment;filename=" + attachment.getFileName());
        // 创建InitiateMultipartUploadRequest对象。
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(ossSettings.getBucketName(), aliyunOssObject.getObjectKey());
        // 初始化分片。
        InitiateMultipartUploadResult upResult = ossClient.initiateMultipartUpload(request);
        String uploadId=upResult.getUploadId();
        aliyunOssAttachmentService.putUploadIdCache(multipartFileParam.getUniqueKey(),uploadId);
        aliyunOssAttachmentService.putPartETagListCache(multipartFileParam.getUniqueKey(),new ArrayList<>());
        return uploadId;
    }

    /**
     * 调用阿里云进行分片上传
     * @param multipartFileParam
     * @param attachment
     * @param aliyunOssObject
     * @param ossClient
     * @return
     * @throws Exception
     */
    public UploadShardResult partUpload(MultipartFileParam multipartFileParam,Attachment attachment,AliyunOssObject aliyunOssObject,OSS ossClient) throws Exception {
        AliyunOssSettings ossSettings=aliyunOssObject.getAliyunOssSettings();
        String uploadId = multipartFileParam.getAliyunOssUploadId();
        StopWatch st = new StopWatch();
        st.start();
        //封装分片上传请求
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setUploadId(uploadId);
        //part大小 1-10000
        uploadPartRequest.setPartNumber(multipartFileParam.getChunkNumber());
        uploadPartRequest.setPartSize(multipartFileParam.getCurrentChunkSize());
        uploadPartRequest.setBucketName(ossSettings.getBucketName());
        uploadPartRequest.setKey(aliyunOssObject.getObjectKey());
        // 每个分片不需要按顺序上传,甚至可以在不同客户端上传,OSS会按照分片号排序组成完整的文件。
        UploadPartResult uploadPartResult = null;
        try(ByteArrayInputStream inputStream=new ByteArrayInputStream(multipartFileParam.getFile().getBytes())){
            uploadPartRequest.setInputStream(inputStream);
            uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        } catch (OSSException e) {
            e.printStackTrace();
            throw new RuntimeException("上传失败:" + e.getMessage());
        } catch (com.aliyun.oss.ClientException e) {
            e.printStackTrace();
            throw new RuntimeException("上传失败,客户端错误:" + e.getMessage());
        }
        st.stop();
        logger.info("{}文件第 {} 片上传成功,上传结果:{},耗时:{}", uploadId, uploadPartRequest.getPartNumber(), JSONUtil.toJsonStr(uploadPartResult), st.getTotalTimeMillis());
        List<PartETag> partETagList=aliyunOssAttachmentService.getPartTagListFromCache(multipartFileParam.getUniqueKey());
        Assert.notNull(partETagList,"获取缓存分片历史失败!");
        partETagList.add(uploadPartResult.getPartETag());
        aliyunOssAttachmentService.putPartETagListCache(multipartFileParam.getUniqueKey(),partETagList);

        if (multipartFileParam.getChunkNumber() == multipartFileParam.getTotalChunks()) {
            //上传成功
            // 创建CompleteMultipartUploadRequest对象。
            // 在执行完成分片上传操作时,需要提供所有有效的partETags。OSS收到提交的partETags后,会逐一验证每个分片的有效性。
            // 当所有的数据分片验证通过后,OSS将把这些分片组合成一个完整的文件。
            mergeMultipart(partETagList,uploadId,aliyunOssObject,ossClient);
            aliyunOssAttachmentService.delUploadIdCache(multipartFileParam.getUniqueKey());
            aliyunOssAttachmentService.delPartTagListCache(multipartFileParam.getUniqueKey());
            aliyunOssAttachmentService.delIdentifierIdCache(multipartFileParam.getUniqueKey());
            return new UploadShardResult(true);
        }
        return new UploadShardResult(false);
    }

    /***
     * 当分片上传为最后一个分片时,进行合并分片操作
     * @param uploadId
     * @param aliyunOssObject
     * @param ossClient
     * @return
     */
    private CompleteMultipartUploadResult mergeMultipart(List<PartETag> partTagList,String uploadId,AliyunOssObject aliyunOssObject,OSS ossClient) throws Exception {
        AliyunOssSettings ossSettings=aliyunOssObject.getAliyunOssSettings();
        try {
            StopWatch st = new StopWatch();
            st.start();
            CompleteMultipartUploadRequest completeMultipartUploadRequest =
                    new CompleteMultipartUploadRequest(ossSettings.getBucketName(), aliyunOssObject.getObjectKey(), uploadId, partTagList);
            logger.info("{}文件上传完成,开始合并,partList:{}", uploadId, JSONUtil.toJsonStr(partTagList));
            // 完成分片上传。
            CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
            st.stop();
            logger.info("{}文件上传完成,上传结果:{},耗时:{}", uploadId, JSONUtil.toJsonStr(completeMultipartUploadResult), st.getTotalTimeMillis());
            return completeMultipartUploadResult;
        } catch (OSSException e) {
            logger.error("合并失败",e);
            throw new RuntimeException("合并失败:" + e.getMessage());
        } catch (com.aliyun.oss.ClientException e) {
            logger.error("合并失败,客户端异常",e);
            throw new RuntimeException("合并失败,客户端异常:" + e.getMessage());
        }
    }

    /**
     * 缓存阿里云上传id
     * @param key
     * @return
     */
    @Cacheable(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_ID, key="#key",
            firstCache = @FirstCache(expireTime = 1, timeUnit = TimeUnit.DAYS),secondaryCache = @SecondaryCache(expireTime = 1, timeUnit = TimeUnit.DAYS))
    public String getUploadIdFromCache(String key) {
        return null;
    }

    @CachePut(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_ID, key="#key",
            firstCache = @FirstCache(expireTime = 1, timeUnit = TimeUnit.DAYS),secondaryCache = @SecondaryCache(expireTime = 1, timeUnit = TimeUnit.DAYS))
    public String putUploadIdCache(String key,String uploadId) {
        return uploadId;
    }

    @CacheEvict(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_ID, key="#key")
    public void delUploadIdCache(String key) {}

    /**
     * 缓存阿里云上传分片记录
     * @param key
     * @return
     */
    @Cacheable(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_PARTLIST, key="#key",
            firstCache = @FirstCache(expireTime = 1, timeUnit = TimeUnit.DAYS),secondaryCache = @SecondaryCache(expireTime = 1, timeUnit = TimeUnit.DAYS))
    public List<PartETag> getPartTagListFromCache(String key) {
        return null;
    }

    @CachePut(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_PARTLIST, key="#key",
            firstCache = @FirstCache(expireTime = 1, timeUnit = TimeUnit.DAYS),secondaryCache = @SecondaryCache(expireTime = 1, timeUnit = TimeUnit.DAYS))
    public List<PartETag> putPartETagListCache(String key, List<PartETag> partETagList) {
        return partETagList;
    }

    @CacheEvict(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_PARTLIST, key="#key")
    public void delPartTagListCache(String key) {}

    /**
     * 缓存分片上传文件id
     * @param key
     * @return
     */
    @Cacheable(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_FILE_ID, key="#key",
            firstCache = @FirstCache(expireTime = 1, timeUnit = TimeUnit.DAYS),secondaryCache = @SecondaryCache(expireTime = 1, timeUnit = TimeUnit.DAYS))
    public String getIdentifierIdFromCache(String key) {
        return null;
    }

    @CachePut(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_FILE_ID, key="#key",
            firstCache = @FirstCache(expireTime = 1, timeUnit = TimeUnit.DAYS),secondaryCache = @SecondaryCache(expireTime = 1, timeUnit = TimeUnit.DAYS))
    public String putIdentifierIdCache(String key,String id) {
        return id;
    }

    @CacheEvict(value = CacheKeyConst.EIP_UPLOAD_ALIYUN_FILE_ID, key="#key")
    public void delIdentifierIdCache(String key) {}

    @Override
    public Map<String, Object> generatePolicy(Attachment attachment, String propertiesId){
        AliyunOssObject aliyunOssObject = initOssObject(attachment, propertiesId);
        AliyunOssSettings ossSettings=aliyunOssObject.getAliyunOssSettings();
        OSS ossClient = new OSSClientBuilder().build(ossSettings.getEndpoint(), ossSettings.getAccessKeyId(), ossSettings.getAccessKeySecret());
        String host = "https://" + ossSettings.getBucketName() + "." + ossSettings.getEndpoint(); // host的格式为 bucketname.endpoint
        // callbackUrl为上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。
//        String callbackUrl = "http://88.88.88.88:8888";
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String dir = attachment.getProp1(); //文件夹路径
        Map<String, Object> respMap = null;
        try {
            if (!ossClient.doesBucketExist(ossSettings.getBucketName())) {
                ossClient.createBucket(ossSettings.getBucketName());
            }
            long expireTime = 60*60*24;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            // PostObject请求最大可支持的文件大小为5 GB,即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 1, 1048576000);
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);
            respMap = new LinkedHashMap<>();
            respMap.put("accessid", ossSettings.getAccessKeyId());
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));
            // respMap.put("expire", formatISO8601Date(expiration));
        } catch (Exception e) {
            // Assert.fail(e.getMessage());
            System.out.println(e.getMessage());
        } finally {
            ossClient.shutdown();
        }
        return respMap;
    }

    @Override
    public String getUrl(Attachment attachment, String propertiesId) throws UnsupportedEncodingException {
        Map<String, Object> map = this.generatePolicy(attachment, "");
        String host = BeanUtils.isEmpty(map.get("host")) ? "" : map.get("host").toString();
        String dir = BeanUtils.isEmpty(map.get("dir")) ? "" : map.get("dir").toString();
        String accessid = BeanUtils.isEmpty(map.get("accessid")) ? "" : map.get("accessid").toString();
        String signature = BeanUtils.isEmpty(map.get("signature")) ? "" : map.get("signature").toString();
        String Expires = BeanUtils.isEmpty(map.get("Expires")) ? "" : map.get("Expires").toString();
        String s = "OSSAccessKeyId=" + accessid + "&Signature=" + signature + "&Expires=" + Expires;
        return host + "/" + dir + "?" + URLEncoder.encode(s, "utf-8");
    }

    @Override
    public InputStream getFileInputStream(Attachment attachment) throws FileNotFoundException {
        String localFilePath=getLocalTempFilePath(attachment);
        FileUtil.createFolderFile(localFilePath);
        File file=new File(localFilePath);
        if(!file.exists()){
            AliyunOssSettings ossSettings = AppUtil.getBean(AliyunOssSettings.class);
            OSS ossClient = new OSSClientBuilder().build(ossSettings.getEndpoint(), ossSettings.getAccessKeyId(), ossSettings.getAccessKeySecret());
            try {
                if (!ossClient.doesBucketExist(ossSettings.getBucketName())) {
                    ossClient.createBucket(ossSettings.getBucketName());
                }
                ossClient.getObject(new GetObjectRequest(ossSettings.getBucketName(), attachment.getFilePath()), file);
            } finally {
                ossClient.shutdown();
            }
        }
        return new FileInputStream(file);
    }

    @Override
    public String getFilePath(String account,String fileName) {
        if(StringUtil.isEmpty(account)){
            account="_unknow";
        }
        return AppFileUtil.getAliOssForderPath(account)+fileName;
    }

    public void removeTemp(Attachment attachment) {
        String localFilePath=getLocalTempFilePath(attachment);
        FileUtil.createFolderFile(localFilePath);
        File file=new File(localFilePath);
        if(file.exists()){
            file.delete();
        }
    }
}