目 录CONTENT

文章目录

又拍云Java工具类

BKUN
2022-06-11 / 0 评论 / 0 点赞 / 2,320 阅读 / 605 字
package com.ice.app.system.file;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import com.ice.app.system.utils.ConfigUtils;
import com.ice.app.system.utils.FilenameUtils;
import com.upyun.RestManager;
import com.upyun.UpException;

import okhttp3.Response;


public class UpYunStorageUtils {
	
	/**
     * upyun oss source
     */
    private static final String OSS_SOURCE ="oss_upyun_source";

    /**
     * upyun oss password
     */
    private static final String OSS_PASSWORD = "oss_upyun_password";

    /**
     * upyun oss bucket
     */
    private static final String OSS_BUCKET = "oss_upyun_bucket";

    /**
     * upyun oss domain protocol
     */
    private static final String OSS_PROTOCOL = "oss_upyun_domain_protocol";

    /**
     * upyun oss domain
     */
    private static final String OSS_DOMAIN = "oss_upyun_domain";

    /**
     * upyun oss operator
     */
    private static final String OSS_OPERATOR = "oss_upyun_operator";

    /**
     * upyun oss style rule
     */
    private static final String OSS_STYLE_RULE = "oss_upyun_style_rule";

    /**
     * upyun oss thumbnail style rule
     */
    private static final String OSS_THUMBNAIL_STYLE_RULE = "oss_upyun_thumbnail_style_rule";
    
    private static Logger logger = LoggerFactory.getLogger(UpYunStorageUtils.class);
	
	
	public static String upload(MultipartFile file) throws IOException {
        Assert.notNull(file, "Multipart file must not be null");

        String source = ConfigUtils.getCacheConfig(OSS_SOURCE);
        String password = ConfigUtils.getCacheConfig(OSS_PASSWORD);
        String bucket = ConfigUtils.getCacheConfig(OSS_BUCKET);
        String protocol = ConfigUtils.getCacheConfig(OSS_PROTOCOL);
        String domain = ConfigUtils.getCacheConfig(OSS_DOMAIN);
        String operator = ConfigUtils.getCacheConfig(OSS_OPERATOR);
        // style rule can be null
        String styleRule = ConfigUtils.getCacheConfig(OSS_STYLE_RULE);
        String thumbnailStyleRule = ConfigUtils.getCacheConfig(OSS_THUMBNAIL_STYLE_RULE);

        RestManager manager = new RestManager(bucket, operator, password);
        manager.setTimeout(60 * 10);
        manager.setApiDomain(RestManager.ED_AUTO);

        Map<String, String> params = new HashMap<>();

        // Get file basename
        String basename = FilenameUtils.getBasename(Objects.requireNonNull(file.getOriginalFilename()));
        // Get file extension
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        // Get md5 value of the file
        String md5OfFile = DigestUtils.md5DigestAsHex(file.getInputStream());
        // Build file path
        String upFilePath = StringUtils.appendIfMissing(source + "/", md5OfFile + '.' + extension);
        
        String filePath = protocol + StringUtils.removeEnd(domain, "/") + upFilePath;
        try {
            // Set md5Content
            params.put(RestManager.PARAMS.CONTENT_MD5.getValue(), md5OfFile);
            // Write file
            Response result = manager.writeFile(upFilePath, file.getInputStream(), params);
            if (!result.isSuccessful()) {
                throw new RuntimeException("上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + upFilePath);
            }
            result.close();
        } catch (Exception e) {
        	throw new RuntimeException("上传附件 " + file.getOriginalFilename() + " 到又拍云失败" + upFilePath);
        }
        return filePath;
    }
    
    public static void delete(String key) {
        Assert.notNull(key, "File key must not be blank");
        // Get config
        String password = ConfigUtils.getCacheConfig(OSS_PASSWORD);
        String bucket = ConfigUtils.getCacheConfig(OSS_BUCKET);
        String operator = ConfigUtils.getCacheConfig(OSS_OPERATOR);

        RestManager manager = new RestManager(bucket, operator, password);
        manager.setTimeout(60 * 10);
        manager.setApiDomain(RestManager.ED_AUTO);

        try {
            Response result = manager.deleteFile(key, null);
            if (!result.isSuccessful()) {
            	logger.warn("附件 " + key + " 从又拍云删除失败");
                throw new RuntimeException("附件 " + key + " 从又拍云删除失败");
            }
        } catch (IOException | UpException e) {
            e.printStackTrace();
            throw new RuntimeException("附件 " + key + " 从又拍云删除失败", e);
        }
    }
    
    public static String getKeyNameByUrl(String key) {
        String protocol = ConfigUtils.getCacheConfig(OSS_PROTOCOL);
        String domain = ConfigUtils.getCacheConfig(OSS_DOMAIN);
        String filePathKey = protocol + StringUtils.removeEnd(domain, "/");
        return key.replace(filePathKey, "");
    }
    
}
0

评论区