文件上傳至oss後,獲取圖片縮率圖、獲取視頻截幀等後續操做

上一篇文章說了一下文件上傳至oss:https://blog.csdn.net/new_programmer_h/article/details/84307005html

這裏說一下上傳後的一些後續操做:經常使用的獲取圖片縮率圖、獲取視頻截幀生成封面圖。自我感受阿里oss對於這些處理封裝的很好,只要根據:"%s|sys/saveas,o_%s,b_%s" 格式設置想要處理的方式他就會針對視頻或者圖片進行處理。app

 

    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com"; 
    private static String accessKeyId = "<yourAccessKeyId>";
    private static String accessKeySecret = "<yourAccessKeySecret>";
    private static String bucketName = "<yourBucketName>";ide

 

圖片上傳至oss後獲取縮率圖工具類:工具

/**
     * 圖片處理:生成縮率圖
     *
     * @author LH_Yu
     * @Param uploadFile 上傳文件
     * @Param picturePath 原圖key
     * @Param thumbPath 縮率圖上傳路徑及取出url的key
     * "%s|sys/saveas,o_%s,b_%s" 具體處理格式化
     */
    public static String compressPicture(MultipartFile uploadFile, String picturePath, String thumbPath) throws Exception {
        // 建立OSSClient實例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 圖片處理持久化 : 縮放
        StringBuilder sbStyle = new StringBuilder();
        Formatter styleFormatter = new Formatter(sbStyle);
        //設置圖片
        String styleType = "image/resize,m_lfit,w_400,h_400"; //圖片設置
        String targetImage = thumbPath + uploadFile.getOriginalFilename();
        styleFormatter.format("%s|sys/saveas,o_%s,b_%s", styleType,
                BinaryUtil.toBase64String(targetImage.getBytes()),
                BinaryUtil.toBase64String(bucketName.getBytes()));
        ProcessObjectRequest request = new ProcessObjectRequest(bucketName, picturePath + uploadFile.getOriginalFilename(), sbStyle.toString());
        GenericResult processResult = ossClient.processObject(request);
        processResult.getResponse().getContent().close();
        //設置過時時間 -- 十年
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        String url = ossClient.generatePresignedUrl(bucketName, thumbPath + uploadFile.getOriginalFilename(), expiration).toString();
        return url;
    }

視頻上傳至oss後截幀獲取視頻封面圖工具類:ui

  ​    /**
* 視頻處理:視頻截幀生成封面圖
     *
     * @author LH_Yu
     * @Param uploadFile 上傳文件
     * @Param videoPath 原視頻key
     * @Param coverPicturePath 封面圖上傳路徑及取出url的key
     * "%s|sys/saveas,o_%s,b_%s" 具體處理格式化
     */
    public static String coverPicture(MultipartFile uploadFile, String videoPath, String coverPicturePath) throws Exception {
        // 建立OSSClient實例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 圖片處理持久化 : 截幀生成封面圖
        StringBuilder sbStyle = new StringBuilder();
        Formatter styleFormatter = new Formatter(sbStyle);
        //設置圖片處理
        String styleType = "video/snapshot,t_3000,f_jpg,w_800,h_0,m_fast";//視頻處理
        String targetImage = coverPicturePath + uploadFile.getOriginalFilename();
        styleFormatter.format("%s|sys/saveas,o_%s,b_%s", styleType,
                BinaryUtil.toBase64String(targetImage.getBytes()),
                BinaryUtil.toBase64String(bucketName.getBytes()));
        ProcessObjectRequest request = new ProcessObjectRequest(bucketNamePrivate, videoPath + uploadFile.getOriginalFilename(), sbStyle.toString());
        GenericResult processResult = ossClient.processObject(request);
        processResult.getResponse().getContent().close();
        //設置過時時間 -- 十年
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        String url = ossClient.generatePresignedUrl(bucketName, coverPicturePath + uploadFile.getOriginalFilename(), expiration).toString();
        return url;
    }
相關文章
相關標籤/搜索