New file |
| | |
| | | package com.hx.util; |
| | | |
| | | import com.obs.services.ObsClient; |
| | | import com.obs.services.exception.ObsException; |
| | | import com.obs.services.model.PutObjectResult; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.UUID; |
| | | |
| | | /**华为云 OBS(OSS) |
| | | * @author ChenJiaHe |
| | | * @date 2021-05-06 |
| | | */ |
| | | public class OBSUtil { |
| | | |
| | | //log4j日志 |
| | | private static Logger logger = LoggerFactory.getLogger(OBSUtil.class.getName()); |
| | | |
| | | /** 后端调用上传图片 |
| | | * @param localFile 图片文件 |
| | | * @param ak 访问秘钥 |
| | | * @param sk 访问秘钥 |
| | | * @param endPoint 存储桶 |
| | | * @param fileName 文件名称(带后缀) |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static PutObjectResult uploadImg(MultipartFile localFile,String ak,String sk,String endPoint,String fileName) throws IOException { |
| | | |
| | | //没有名次的时候自动生成 |
| | | if(StringUtils.isNull(fileName)){ |
| | | fileName = UUID.randomUUID().toString(); |
| | | if (localFile.getOriginalFilename().lastIndexOf(".") >= 0){ |
| | | fileName += localFile.getOriginalFilename().substring(localFile.getOriginalFilename().lastIndexOf(".")); |
| | | } |
| | | } |
| | | |
| | | PutObjectResult putObjectResult = null; |
| | | |
| | | //初始化OBS客户端 |
| | | ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
| | | try{ |
| | | putObjectResult = obsClient.putObject(endPoint, fileName,localFile.getInputStream()); |
| | | }catch (ObsException e){ |
| | | logger.error("华为云 OBS 上传文件报错 "); |
| | | logger.error("HTTP Code: " + e.getResponseCode()); |
| | | logger.error("Error Code:" + e.getErrorCode()); |
| | | logger.error("Error Message: " + e.getErrorMessage()); |
| | | |
| | | logger.error("Request ID:" + e.getErrorRequestId()); |
| | | logger.error("Host ID:" + e.getErrorHostId()); |
| | | }finally { |
| | | if(obsClient != null){ |
| | | obsClient.close(); |
| | | } |
| | | } |
| | | |
| | | return putObjectResult; |
| | | } |
| | | |
| | | |
| | | /** 后端调用上传图片 |
| | | * @param localFile 存储文件 |
| | | * @param ak 访问秘钥 |
| | | * @param sk 访问秘钥 |
| | | * @param endPoint 存储桶 |
| | | * @param fileName 文件名称(带后缀) |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | public static PutObjectResult uploadImg(File localFile,String ak,String sk,String endPoint,String fileName) throws IOException { |
| | | |
| | | //没有名次的时候自动生成 |
| | | if(StringUtils.isNull(fileName)){ |
| | | fileName = UUID.randomUUID().toString(); |
| | | if (localFile.getName().lastIndexOf(".") >= 0){ |
| | | fileName += localFile.getName().substring(localFile.getName().lastIndexOf(".")); |
| | | } |
| | | } |
| | | |
| | | PutObjectResult putObjectResult = null; |
| | | |
| | | //初始化OBS客户端 |
| | | ObsClient obsClient = new ObsClient(ak, sk, endPoint); |
| | | try{ |
| | | putObjectResult = obsClient.putObject(endPoint, fileName,localFile); |
| | | }catch (ObsException e){ |
| | | logger.error("华为云 OBS 上传文件报错 "); |
| | | logger.error("HTTP Code: " + e.getResponseCode()); |
| | | logger.error("Error Code:" + e.getErrorCode()); |
| | | logger.error("Error Message: " + e.getErrorMessage()); |
| | | |
| | | logger.error("Request ID:" + e.getErrorRequestId()); |
| | | logger.error("Host ID:" + e.getErrorHostId()); |
| | | }finally { |
| | | if(obsClient != null){ |
| | | obsClient.close(); |
| | | } |
| | | } |
| | | return putObjectResult; |
| | | |
| | | } |
| | | |
| | | } |