|
@@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
import com.aliyun.oss.model.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
@@ -13,7 +14,8 @@ import java.io.*;
|
|
|
* @date 2019/11/29
|
|
|
* @description aliyun OSS工具类
|
|
|
*/
|
|
|
-public class AliyunOSSUtil {
|
|
|
+@Slf4j
|
|
|
+public class AliyunOSS {
|
|
|
|
|
|
// Endpoint以杭州为例,其它Region请按实际情况填写。
|
|
|
private String endpoint = "http://oss-cn-beijing.aliyuncs.com";
|
|
@@ -30,7 +32,7 @@ public class AliyunOSSUtil {
|
|
|
/**
|
|
|
* 无参构造,初始化oss
|
|
|
*/
|
|
|
- public AliyunOSSUtil(){
|
|
|
+ public AliyunOSS(){
|
|
|
if(null == ossClient){
|
|
|
ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
}
|
|
@@ -64,7 +66,7 @@ public class AliyunOSSUtil {
|
|
|
String suffix = filename.substring(filename.indexOf("."));
|
|
|
System.out.println("filename: " + filename);
|
|
|
System.out.println("suffix: " + suffix);
|
|
|
- AliyunOSSUtil util = AliyunOSSUtil.me();
|
|
|
+ AliyunOSS util = AliyunOSS.me();
|
|
|
String objectName = DateUtil.today().replaceAll("-","/") + "/" +System.currentTimeMillis() + suffix;
|
|
|
System.out.println("objectname: " + objectName);
|
|
|
util.putFile(objectName,file);
|
|
@@ -74,7 +76,7 @@ public class AliyunOSSUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public AliyunOSSUtil(String endpoint, String accessKeyId, String accessKeySecret,String bucketName) {
|
|
|
+ public AliyunOSS(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
|
|
|
this.endpoint = endpoint;
|
|
|
this.accessKeyId = accessKeyId;
|
|
|
this.accessKeySecret = accessKeySecret;
|
|
@@ -82,11 +84,11 @@ public class AliyunOSSUtil {
|
|
|
init();
|
|
|
}
|
|
|
|
|
|
- public static AliyunOSSUtil me(){
|
|
|
- return new AliyunOSSUtil();
|
|
|
+ public static AliyunOSS me(){
|
|
|
+ return new AliyunOSS();
|
|
|
}
|
|
|
- public static AliyunOSSUtil me(String endpoint,String accessKeyId, String accessKeySecret,String bucketName){
|
|
|
- return new AliyunOSSUtil(endpoint,accessKeyId,accessKeySecret,bucketName);
|
|
|
+ public static AliyunOSS me(String endpoint, String accessKeyId, String accessKeySecret, String bucketName){
|
|
|
+ return new AliyunOSS(endpoint,accessKeyId,accessKeySecret,bucketName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -105,7 +107,7 @@ public class AliyunOSSUtil {
|
|
|
ossClient.createBucket(bucketName);
|
|
|
return true;
|
|
|
}catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("创建存储空间异常: {}",e);
|
|
|
return false;
|
|
|
}finally {
|
|
|
destroy();
|
|
@@ -127,7 +129,7 @@ public class AliyunOSSUtil {
|
|
|
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content));
|
|
|
return true;
|
|
|
}catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("上传文件异常: {}",e);
|
|
|
return false;
|
|
|
}finally {
|
|
|
destroy();
|
|
@@ -205,7 +207,7 @@ public class AliyunOSSUtil {
|
|
|
ossClient.deleteObject(bucketName, objectName);
|
|
|
return true;
|
|
|
}catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("删除文件异常: {}",e);
|
|
|
return false;
|
|
|
}finally {
|
|
|
destroy();
|
|
@@ -233,7 +235,7 @@ public class AliyunOSSUtil {
|
|
|
ossClient.putObject(putObjectRequest);
|
|
|
return true;
|
|
|
}catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("上传文件异常: {}",e);
|
|
|
return false;
|
|
|
}finally {
|
|
|
destroy();
|
|
@@ -255,7 +257,7 @@ public class AliyunOSSUtil {
|
|
|
ossClient.getObject(new GetObjectRequest(bucketName, objectName), file);
|
|
|
return file;
|
|
|
}catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("获取文件异常: {}",e);
|
|
|
return null;
|
|
|
}finally {
|
|
|
destroy();
|