檔案上傳至oss後,獲取圖片縮率圖、獲取視訊截幀等後續操作
阿新 • • 發佈:2018-11-21
上一篇文章說了一下檔案上傳至oss:https://blog.csdn.net/new_programmer_h/article/details/84307005
這裡說一下上傳後的一些後續操作:常用的獲取圖片縮率圖、獲取視訊截幀生成封面圖。自我感覺阿里oss對於這些處理封裝的很好,只要根據:"%s|sys/saveas,o_%s,b_%s" 格式設定想要處理的方式他就會針對視訊或者圖片進行處理。
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>";
圖片上傳至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後截幀獲取視訊封面圖工具類:
/*** 視訊處理:視訊截幀生成封面圖 * * @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; }