From 6f6166275aa03dbcd375f50116a3f2811cb6c35f Mon Sep 17 00:00:00 2001 From: hello-hao <923453645@qq.com> Date: Wed, 15 Mar 2023 17:35:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E5=AF=B9=E6=8E=A5S3?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=20style:=E8=B0=83=E6=95=B4=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=20docs:=E8=B0=83=E6=95=B4=E4=BA=86=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- pom.xml | 16 +- .../controller/AdminRootController.java | 2 +- .../java/cn/hellohao/quartz/job/FirstJob.java | 4 +- .../service/impl/InitializationStorage.java | 17 +- .../service/impl/KeysServiceImpl.java | 5 +- .../hellohao/service/impl/S3Imageupload.java | 167 ++++++++++ ...mageupload.java => UFileImageupload1.java} | 2 +- .../cn/hellohao/service/impl/deleImages.java | 4 +- src/main/java/cn/hellohao/utils/FirstRun.java | 22 +- .../java/cn/hellohao/utils/GetSource.java | 5 +- src/main/resources/application.properties | 2 +- src/main/resources/logback-spring.xml | 295 +++++++----------- 13 files changed, 328 insertions(+), 217 deletions(-) create mode 100644 src/main/java/cn/hellohao/service/impl/S3Imageupload.java rename src/main/java/cn/hellohao/service/impl/{UFileImageupload.java => UFileImageupload1.java} (99%) diff --git a/README.md b/README.md index e7c36c9..c28c2da 100644 --- a/README.md +++ b/README.md @@ -188,8 +188,8 @@ Core版:`本地`,`阿里OSS`,`又拍USS`,`七牛KODO`,`腾讯COS`,`网易NOS`, ### 反馈交流 **如果你遇到BUG欢迎反馈** - -欢迎加入Hellohao开发者交流群,群聊号码:**864800972** +- 前往论坛发帖反馈、求助:[Hellohao开发者交流论坛](http://bbs.hellohao.cn) +- 也可加QQ群探讨(目前已满):864800972 ### 捐赠开发者 diff --git a/pom.xml b/pom.xml index e417fbc..12f535e 100644 --- a/pom.xml +++ b/pom.xml @@ -90,11 +90,11 @@ spring-boot-starter-validation - - commons-logging - commons-logging - 1.2 - + + + + + com.github.pagehelper pagehelper-spring-boot-starter @@ -120,7 +120,11 @@ pebble 3.1.5 - + + com.amazonaws + aws-java-sdk-s3 + 1.12.334 + org.springframework.boot spring-boot-starter-mail diff --git a/src/main/java/cn/hellohao/controller/AdminRootController.java b/src/main/java/cn/hellohao/controller/AdminRootController.java index 6ec871e..b54d0c1 100644 --- a/src/main/java/cn/hellohao/controller/AdminRootController.java +++ b/src/main/java/cn/hellohao/controller/AdminRootController.java @@ -196,7 +196,7 @@ public class AdminRootController { } else if (key.getStorageType() == 7) { ret = FtpServiceImpl.Initialize(key); } else if (key.getStorageType() == 8) { - ret = UFileImageupload.Initialize(key); + ret = S3Imageupload.Initialize(key); } Long l = imgService.getsourcememory(keyId); jsonObject.put("isok", ret); diff --git a/src/main/java/cn/hellohao/quartz/job/FirstJob.java b/src/main/java/cn/hellohao/quartz/job/FirstJob.java index f06e116..113210c 100644 --- a/src/main/java/cn/hellohao/quartz/job/FirstJob.java +++ b/src/main/java/cn/hellohao/quartz/job/FirstJob.java @@ -32,7 +32,7 @@ public class FirstJob { @Autowired private USSImageupload ussImageupload; @Autowired - private UFileImageupload uFileImageupload; + private S3Imageupload s3Imageupload; @Autowired private FtpServiceImpl ftpService; @Autowired @@ -77,7 +77,7 @@ public class FirstJob { }else if(keys.getStorageType()==7){ firstJob.ftpService.delFTP(keys.getId(), images); }else if(keys.getStorageType()==8){ - firstJob.uFileImageupload.delUFile(keys.getId(), images); + firstJob.s3Imageupload.deleS3(keys.getId(), images); }else{ System.err.println("未获取到对象存储参数,上传失败。"); } diff --git a/src/main/java/cn/hellohao/service/impl/InitializationStorage.java b/src/main/java/cn/hellohao/service/impl/InitializationStorage.java index a040b07..506c703 100644 --- a/src/main/java/cn/hellohao/service/impl/InitializationStorage.java +++ b/src/main/java/cn/hellohao/service/impl/InitializationStorage.java @@ -30,7 +30,7 @@ public class InitializationStorage implements CommandLineRunner { private KeysMapper keysMapper; @Override - public void run(String... args) throws Exception { + public void run(String... args) { SubjectFilter.WEBHOST = allowedOrigins; String name = ManagementFactory.getRuntimeMXBean().getName(); String pid = name.split("@")[0]; @@ -41,21 +41,20 @@ public class InitializationStorage implements CommandLineRunner { List keylist = keysMapper.getKeys(); for (Keys key : keylist) { if(key.getStorageType()!=0 && key.getStorageType()!=null){ - int ret =0; if(key.getStorageType()==1){ - ret =NOSImageupload.Initialize(key); + NOSImageupload.Initialize(key); }else if (key.getStorageType()==2){ - ret =OSSImageupload.Initialize(key); + OSSImageupload.Initialize(key); }else if(key.getStorageType()==3){ - ret = USSImageupload.Initialize(key); + USSImageupload.Initialize(key); }else if(key.getStorageType()==4){ - ret = KODOImageupload.Initialize(key); + KODOImageupload.Initialize(key); }else if(key.getStorageType()==6){ - ret = COSImageupload.Initialize(key); + COSImageupload.Initialize(key); }else if(key.getStorageType()==7){ - ret = FtpServiceImpl.Initialize(key); + FtpServiceImpl.Initialize(key); }else if(key.getStorageType()==8){ - ret = UFileImageupload.Initialize(key); + S3Imageupload.Initialize(key); } } } diff --git a/src/main/java/cn/hellohao/service/impl/KeysServiceImpl.java b/src/main/java/cn/hellohao/service/impl/KeysServiceImpl.java index 95c1f6c..3a1fdc7 100644 --- a/src/main/java/cn/hellohao/service/impl/KeysServiceImpl.java +++ b/src/main/java/cn/hellohao/service/impl/KeysServiceImpl.java @@ -16,7 +16,6 @@ public class KeysServiceImpl implements KeysService { @Autowired private KeysMapper keysMapper; - @Autowired private NOSImageupload nOSImageupload; @Autowired @@ -30,7 +29,7 @@ public class KeysServiceImpl implements KeysService { @Autowired private FtpServiceImpl ftpService; @Autowired - private UFileImageupload uFileImageupload; + private S3Imageupload s3Imageupload; @Override @@ -67,7 +66,7 @@ public class KeysServiceImpl implements KeysService { }else if(key.getStorageType()==7){ ret = ftpService.Initialize(key); }else if(key.getStorageType()==8){ - ret = uFileImageupload.Initialize(key); + ret = s3Imageupload.Initialize(key); }else{ Print.Normal("为获取到存储参数,或者使用存储源是本地的。"); } diff --git a/src/main/java/cn/hellohao/service/impl/S3Imageupload.java b/src/main/java/cn/hellohao/service/impl/S3Imageupload.java new file mode 100644 index 0000000..8d0103c --- /dev/null +++ b/src/main/java/cn/hellohao/service/impl/S3Imageupload.java @@ -0,0 +1,167 @@ +package cn.hellohao.service.impl; + +import cn.hellohao.pojo.Images; +import cn.hellohao.pojo.Keys; +import cn.hellohao.pojo.ReturnImage; +import com.amazonaws.ClientConfiguration; +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.AWSCredentialsProvider; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.client.builder.AwsClientBuilder; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.Bucket; +import com.amazonaws.services.s3.model.DeleteObjectRequest; +import com.amazonaws.services.s3.model.ObjectListing; +import com.amazonaws.services.s3.model.PutObjectRequest; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.util.List; +import java.util.Map; + +/** + * @author Hellohao + * @version 1.0 + * @date 2023/3/15 14:04 + */ +@Service +public class S3Imageupload { + private static Logger logger = LoggerFactory.getLogger(S3Imageupload.class); + + static AmazonS3 AS3; + static Keys KEY; + public ReturnImage ImageuploadS3( + Map, File> fileMap, String username, Integer keyID) { + ReturnImage returnImage = new ReturnImage(); + try { + if (null == AS3 || null == AS3) { + logger.error("S3 = 存储源对象为空"); + returnImage.setCode("500"); + return returnImage; + } + } catch (Exception e) { + e.printStackTrace(); + returnImage.setCode("500"); + return returnImage; + } + File file = null; + try { + for (Map.Entry, File> entry : fileMap.entrySet()) { + String prefix = entry.getKey().get("prefix"); + String ShortUIDName = entry.getKey().get("name"); + file = entry.getValue(); + AS3.putObject( + new PutObjectRequest( + KEY.getBucketname(), + username + "/" + ShortUIDName + "." + prefix, + file)); + // S3Object object = s3.getObject( new GetObjectRequest( key.getBucketname(), + } + returnImage.setCode("200"); + } catch (Exception e) { + e.printStackTrace(); + returnImage.setCode("500"); + } + return returnImage; + } + + + public static Integer Initialize(Keys k) { + int ret = -1; + if (StringUtils.isBlank(k.getAccessKey()) + || StringUtils.isBlank(k.getAccessSecret()) + || StringUtils.isBlank(k.getRootPath()) + || StringUtils.isBlank(k.getEndpoint()) + || StringUtils.isBlank(k.getBucketname()) + || StringUtils.isBlank(k.getRequestAddress())) { + return -1; + } + try { + String regionStr = k.getRegion() == null ? "" : k.getRegion(); + ClientConfiguration config = new ClientConfiguration(); + config.setMaxConnections(500); + config.setConnectionTimeout(30); + // cn-north-1 us-east-1 + BasicAWSCredentials credentials = + new BasicAWSCredentials(k.getAccessKey(), k.getAccessSecret()); + AmazonS3 s3 = + AmazonS3ClientBuilder.standard() + .withClientConfiguration(config) + .withPathStyleAccessEnabled(false) + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withEndpointConfiguration( + new AwsClientBuilder.EndpointConfiguration( + k.getEndpoint(), regionStr)) + .build(); + ObjectListing objectListing = s3.listObjects(k.getBucketname()); + ret = 1; + AS3 = s3; + KEY = k; + } catch (Exception e) { + logger.warn("S3 Object " + k.getKeyname() + " Is null"); + ret = -1; + } + if(ret==-1){ + ret = Initialize_Tow(k); + if(ret>-1){ + //OK + } + } + return ret; + } + + + public static Integer Initialize_Tow(Keys k) { + int ret = -1; + try { + ClientConfiguration config = new ClientConfiguration(); + AwsClientBuilder.EndpointConfiguration endpointConfig = + new AwsClientBuilder.EndpointConfiguration(k.getEndpoint(), k.getRegion());//cn-north-1 + AWSCredentials awsCredentials = new BasicAWSCredentials(k.getAccessKey(),k.getAccessSecret()); + AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials); + AmazonS3 s3 = AmazonS3Client.builder() + .withEndpointConfiguration(endpointConfig) + .withClientConfiguration(config) + .withCredentials(awsCredentialsProvider) + .disableChunkedEncoding() + .withPathStyleAccessEnabled(true) + .build(); + List buckets = s3.listBuckets(); + boolean b = false; + for (Bucket bucket : buckets) { + if(k.getBucketname().equals(bucket.getName())){ + b = true; + } + } + if(b==false){ + throw new NullPointerException(); + } + ret=1; + AS3 = s3; + KEY = k; + } catch (Exception e) { + ret = -1; + } + return ret; + } + + public boolean deleS3(Integer keyID, Images images) { + boolean b = true; + try { + DeleteObjectRequest deleteObjectRequest = + new DeleteObjectRequest(KEY.getBucketname(), images.getImgname()); + AS3.deleteObject(deleteObjectRequest); + } catch (Exception e) { + e.printStackTrace(); + b = false; + } + return b; + } + +} diff --git a/src/main/java/cn/hellohao/service/impl/UFileImageupload.java b/src/main/java/cn/hellohao/service/impl/UFileImageupload1.java similarity index 99% rename from src/main/java/cn/hellohao/service/impl/UFileImageupload.java rename to src/main/java/cn/hellohao/service/impl/UFileImageupload1.java index 5f275fe..ba2d5bb 100644 --- a/src/main/java/cn/hellohao/service/impl/UFileImageupload.java +++ b/src/main/java/cn/hellohao/service/impl/UFileImageupload1.java @@ -15,7 +15,7 @@ import java.util.List; import java.util.Map; @Service -public class UFileImageupload { +public class UFileImageupload1 { static UpYun uFile; static Keys key; diff --git a/src/main/java/cn/hellohao/service/impl/deleImages.java b/src/main/java/cn/hellohao/service/impl/deleImages.java index 45cb863..8c1c444 100644 --- a/src/main/java/cn/hellohao/service/impl/deleImages.java +++ b/src/main/java/cn/hellohao/service/impl/deleImages.java @@ -24,7 +24,7 @@ public class deleImages { @Autowired private COSImageupload cosImageupload; @Autowired private KODOImageupload kodoImageupload; @Autowired private USSImageupload ussImageupload; - @Autowired private UFileImageupload uFileImageupload; + @Autowired private S3Imageupload s3Imageupload; @Autowired private FtpServiceImpl ftpService; @Autowired private ImgAndAlbumService imgAndAlbumService; @Autowired private ImgTempService imgTempService; @@ -60,7 +60,7 @@ public class deleImages { } else if (key.getStorageType() == 7) { isDele = ftpService.delFTP(key.getId(), image); } else if (key.getStorageType() == 8) { - isDele = uFileImageupload.delUFile(key.getId(), image); + isDele = s3Imageupload.deleS3(key.getId(), image); } else { System.err.println("未获取到对象存储参数,删除失败。"); } diff --git a/src/main/java/cn/hellohao/utils/FirstRun.java b/src/main/java/cn/hellohao/utils/FirstRun.java index f196646..b64f9a0 100644 --- a/src/main/java/cn/hellohao/utils/FirstRun.java +++ b/src/main/java/cn/hellohao/utils/FirstRun.java @@ -47,6 +47,13 @@ public class FirstRun implements InitializingBean { String uid = UUID.randomUUID().toString().replace("-", "").toLowerCase(); RunSqlScript.RunInsert("update user set uid='"+uid+"' where id = 1"); + Integer ret0 = + RunSqlScript.RunSelectCount(judgeTable + "'keys' and column_name = 'Region'"); + if (ret0 == 0) { + RunSqlScript.RunInsert(sql0); + Print.Normal("Add keys.Region||RootPath"); + } + Integer ret1 = RunSqlScript.RunSelectCount(sql1); if(ret1==0){ Print.Normal("In execution..."); @@ -83,11 +90,7 @@ public class FirstRun implements InitializingBean { RunSqlScript.RunInsert(sql8); Print.Normal("Stage 6"); } - Integer ret5 = RunSqlScript.RunSelectCount(judgeTable+"'config' and column_name = 'theme'"); - if(ret5==0){ - RunSqlScript.RunInsert(sql9); - Print.Normal("Stage 7"); - } + Integer ret6 = RunSqlScript.RunSelectCount(judgeTable+" 'user' and column_name = 'token'"); if(ret6==0){ RunSqlScript.RunInsert(sql12); @@ -105,12 +108,6 @@ public class FirstRun implements InitializingBean { RunSqlScript.RunInsert(instartConfdata); Print.Normal("Add table.confdata"); } -// Integer ret6 = RunSqlScript.RunSelectCount(judgeTable+" 'imgdata' and column_name = 'idname'"); -// if(ret6==0){ -// RunSqlScript.RunInsert(sql11); -// Print.Normal("Add imgdata.idname"); -// } - RunSqlScript.RunInsert("alter table tbed.`config` modify column `explain` text,modify column `links` text,modify column `notice` text,modify column `baidu` text"); // RunSqlScript.RunInsert(inddx_md5key); RunSqlScript.RunInsert("UPDATE tbed.`keys` SET `Endpoint` = '0' WHERE `id` = 8"); RunSqlScript.RunInsert("ALTER TABLE tbed.`user` MODIFY id int auto_increment;"); @@ -123,6 +120,8 @@ public class FirstRun implements InitializingBean { private String isTableName = "SELECT count(table_name) FROM information_schema.TABLES WHERE TABLE_SCHEMA='tbed' and table_name ="; private String judgeTable = "select count(*) from information_schema.columns where TABLE_SCHEMA='tbed' and table_name = "; //创建blacklist 2019-11-29 + private String sql0 = + "alter table `keys` add (Region varchar(255) DEFAULT null,RootPath varchar(255) DEFAULT '/');"; private String sql1 = "select count(*) from information_schema.columns where table_name = 'uploadconfig' and column_name = 'blacklist'"; private String sql2 = "alter table uploadconfig add blacklist varchar(500);"; //创建imgandalbum和album 添加imgdata表字段explain 2019-12-20 @@ -133,7 +132,6 @@ public class FirstRun implements InitializingBean { private String sql7 = "alter table album add userid int(10)"; //添加imgdata表字段md5key private String sql8 = "alter table imgdata add md5key varchar(5000)"; - private String sql9 = "ALTER TABLE config ADD theme int(4) DEFAULT '1' COMMENT '主题' "; // 图片标识名字段 private String sql11 = "alter table `imgdata` add idname varchar(255) DEFAULT '未命名' ;"; diff --git a/src/main/java/cn/hellohao/utils/GetSource.java b/src/main/java/cn/hellohao/utils/GetSource.java index fbcb238..370c415 100644 --- a/src/main/java/cn/hellohao/utils/GetSource.java +++ b/src/main/java/cn/hellohao/utils/GetSource.java @@ -5,7 +5,6 @@ import cn.hellohao.pojo.ReturnImage; import cn.hellohao.service.impl.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.util.Map; @@ -31,7 +30,7 @@ public class GetSource { @Autowired FtpServiceImpl ftpService; @Autowired - UFileImageupload uFileImageupload; + S3Imageupload s3Imageupload; public ReturnImage storageSource(Integer type, Map, File> fileMap, String userpath,Integer keyID){ ReturnImage returnImage = null; @@ -51,7 +50,7 @@ public class GetSource { }else if(type==7){ returnImage = ftpService.FtpUploadFile(fileMap, userpath,keyID); }else if(type==8){ - returnImage = uFileImageupload.ImageuploadUFile(fileMap, userpath,keyID); + returnImage = s3Imageupload.ImageuploadS3(fileMap, userpath,keyID); } else{ new StorageSourceInitException("GetSource类捕捉异常:未找到存储源"); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index cd535f6..62d749d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -25,7 +25,6 @@ spring.redis.password= spring.thymeleaf.suffix=.html mybatis.configuration.map-underscore-to-camel-case=true mybatis.mapper-locations=classpath:mapper/*.xml -logging.level.cn.hellohao.dao=debug spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource @@ -50,4 +49,5 @@ spring.datasource.druid.stat-view-servlet.login-username=hellohao@dealer spring.datasource.druid.stat-view-servlet.login-password=52BNmXPJL0rQQ3CU spring.datasource.druid.stat-view-servlet.allow=127.0.0.1 spring.datasource.druid.web-stat-filter.exclusions=false +log4j.logger.cn.hellohao.dao=DEBUG log4j2.formatMsgNoLookups=True \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index 789d843..3248a7f 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -1,197 +1,142 @@ - - - - - + + + + + + + - - - logback - + + + + + + + + + - - - - - - - - - - + - - - DEBUG - + + + + + + + + - ${CONSOLE_LOG_PATTERN} - - UTF-8 + + + + %blue(%d{yyyy-MM-dd HH:mm:ss,SSS}) [%cyan(%t)] [%yellow(%c)]-[%highlight(%p)] %m%n + - - - - - - - ${log.path}/log_debug.log - - - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n - UTF-8 - - - - - ${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log - - 100MB - - - 15 - - - - debug - ACCEPT - DENY - - - - - - - ${log.path}/log_info.log - - - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n - UTF-8 - - - - - ${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.log - - 100MB - - - 15 - - - - info - ACCEPT - DENY - - - - - - - ${log.path}/log_warn.log - - - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n - UTF-8 - - - - ${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log - - 100MB - - - 15 - - - - warn - ACCEPT - DENY - - - - - - - + + + ${log.path}/log_error.log - - - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n - UTF-8 - - - - ${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log - - 100MB - - - 15 - - + + + ERROR + + ACCEPT + + DENY + + + + + + ${log.log_dir}/error/%d{yyyy-MM-dd}/error_${log.log_name}-%i.log + + + ${log.maxHistory} + + ${log.maxSize} + true + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n + + + + + + + ${log.path}/log_info.log + + INFO ACCEPT DENY + + ${log.log_dir}/info/%d{yyyy-MM-dd}/info_${log.log_name}-%i.log + ${log.maxHistory} + ${log.maxSize} + true + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n + - - - - + + + ${log.path}/log_debug.log + + DEBUG + ACCEPT + DENY + + + ${log.log_dir}/debug/%d{yyyy-MM-dd}/debug_${log.log_name}-%i.log + ${log.maxHistory} + ${log.maxSize} + true + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n + + - + + + + ${log.log_dir}/app/%d{yyyy-MM-dd}/app_${log.log_name}-%i.log + ${log.maxHistory} + ${log.maxSize} + true + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n + + - - - - - - + + - - - - + + + + + + + + + + + - - - - - - - - - - - - - - + \ No newline at end of file