mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
feat:新增对接S3协议
style:调整日志框架 docs:调整了文档
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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("未获取到对象存储参数,上传失败。");
|
||||
}
|
||||
|
||||
@@ -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<Keys> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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("为获取到存储参数,或者使用存储源是本地的。");
|
||||
}
|
||||
|
||||
@@ -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<Map<String, String>, 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<Map<String, String>, 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<Bucket> 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;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -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;
|
||||
|
||||
@@ -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("未获取到对象存储参数,删除失败。");
|
||||
}
|
||||
|
||||
@@ -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 '未命名' ;";
|
||||
|
||||
|
||||
@@ -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<Map<String, String>, 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类捕捉异常:未找到存储源");
|
||||
|
||||
Reference in New Issue
Block a user