You've already forked FrameTour-BE
微信支付、回调、订单查询;
微信用户登录、用户信息查询、修改用户信息、同意用户协议; 文件OSS上传、删除接口;
This commit is contained in:
85
src/main/java/com/ycwl/basic/utils/OssUtil.java
Normal file
85
src/main/java/com/ycwl/basic/utils/OssUtil.java
Normal file
@ -0,0 +1,85 @@
|
||||
package com.ycwl.basic.utils;
|
||||
|
||||
import com.aliyun.oss.ClientException;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class OssUtil {
|
||||
|
||||
@Autowired
|
||||
private OssConfig ossConfig;
|
||||
|
||||
public String uploadFile(InputStream inputStream, String filename) {
|
||||
String uploadFileName = ossConfig.getObjectName() + System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
|
||||
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 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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user