You've already forked FrameTour-BE
彻底铲除OSSUtil,抽象、修改
This commit is contained in:
@ -1,126 +0,0 @@
|
||||
package com.ycwl.basic.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.HttpMethod;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.ycwl.basic.config.OssConfig;
|
||||
import com.ycwl.basic.enums.BizCodeEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.Date;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class OssUtil {
|
||||
|
||||
@Autowired
|
||||
private OssConfig ossConfig;
|
||||
|
||||
/**
|
||||
* 上传文件到oss
|
||||
* @param inputStream 文件数据流
|
||||
* @param filename 文件全路径名称
|
||||
* @return
|
||||
*/
|
||||
public String uploadFile(InputStream inputStream, String filename) {
|
||||
return uploadFile(inputStream, ossConfig.getObjectName(), filename);
|
||||
}
|
||||
|
||||
public String uploadAssetFile(InputStream inputStream, String filename) {
|
||||
return uploadFile(inputStream, "assets/", filename);
|
||||
}
|
||||
|
||||
public String uploadFile(InputStream inputStream, String path, String filename) {
|
||||
String uploadFileName = path + filename;
|
||||
OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
||||
try {
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(), uploadFileName, inputStream);
|
||||
ossClient.putObject(putObjectRequest);
|
||||
String fileUrl = ossConfig.getUrl() + uploadFileName;
|
||||
return fileUrl;
|
||||
} catch (OSSException oe) {
|
||||
log.error("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason."
|
||||
+ " \n Error Message:" + oe.getErrorMessage()
|
||||
+ " \n Error Code:" + oe.getErrorCode()
|
||||
+ " \n Request ID:" + oe.getRequestId()
|
||||
+ " \n Host ID:" + oe.getHostId()
|
||||
);
|
||||
|
||||
} catch (ClientException ce) {
|
||||
log.error("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network."
|
||||
+ "Error Message:" + ce.getMessage());
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
return BizCodeEnum.UPLOAD_FAILED.getMessage();
|
||||
}
|
||||
|
||||
public String generateSignedUrlForDownload(String path, String filename) {
|
||||
String downloadFile = path + filename;
|
||||
return generateSignedUrl(downloadFile, HttpMethod.GET);
|
||||
}
|
||||
|
||||
public String generateSignedUrlForUpload(String path, String filename) {
|
||||
String uploadFileName = path + filename;
|
||||
return generateSignedUrl(uploadFileName, HttpMethod.PUT);
|
||||
}
|
||||
|
||||
public String generateSignedUrl(String objectName, HttpMethod method) {
|
||||
OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
||||
return ossClient.generatePresignedUrl(ossConfig.getBucketName(), objectName, DateUtil.offsetHour(new Date(), 1), method).toString();
|
||||
}
|
||||
public String generateUrlOfFile(String path, String filename) {
|
||||
String objectName = path + filename;
|
||||
return ossConfig.getUrl() + objectName;
|
||||
}
|
||||
|
||||
public boolean deleteFile(String filename) {
|
||||
// 填写文件完整路径。文件完整路径中不能包含Bucket名称。
|
||||
String objectName = filename;
|
||||
|
||||
OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
||||
try {
|
||||
// 删除文件或目录。如果要删除目录,目录必须为空。
|
||||
ossClient.deleteObject(ossConfig.getBucketName(), objectName);
|
||||
return true;
|
||||
} catch (OSSException oe) {
|
||||
log.error("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason."
|
||||
+ " \n Error Message:" + oe.getErrorMessage()
|
||||
+ " \n Error Code:" + oe.getErrorCode()
|
||||
+ " \n Request ID:" + oe.getRequestId()
|
||||
+ " \n Host ID:" + oe.getHostId()
|
||||
);
|
||||
} catch (ClientException ce) {
|
||||
log.error("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network."
|
||||
+ "Error Message:" + ce.getMessage());
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void deleteFileByUrl(String faceUrl) {
|
||||
URI uri = URI.create(faceUrl);
|
||||
String objectName = uri.getPath();
|
||||
deleteFile(objectName);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user