From 0a3dfb3ef85805d17b3b3f92e41a9e9b04daeb7d Mon Sep 17 00:00:00 2001 From: 648607640 <648607640@qq.com> Date: Wed, 17 Jan 2024 16:57:40 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E9=98=BF=E9=87=8Cos?= =?UTF-8?q?s=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/file/handler/AliossFileSystemHandler.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java diff --git a/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java b/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java new file mode 100644 index 000000000..38dbb08d6 --- /dev/null +++ b/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java @@ -0,0 +1,5 @@ +package neatlogic.module.framework.file.handler; + +public class AliossFileSystemHandler { + +} -- Gitee From 2d240d7e193a14552bffd17fc75ce99df27f3d9f Mon Sep 17 00:00:00 2001 From: 648607640 <648607640@qq.com> Date: Wed, 17 Jan 2024 17:35:06 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E9=98=BF=E9=87=8Cos?= =?UTF-8?q?s=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/handler/AliossFileSystemHandler.java | 117 +++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java b/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java index 38dbb08d6..06ce0f278 100644 --- a/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java +++ b/src/main/java/neatlogic/module/framework/file/handler/AliossFileSystemHandler.java @@ -1,5 +1,120 @@ package neatlogic.module.framework.file.handler; -public class AliossFileSystemHandler { +import com.aliyun.oss.ClientConfiguration; +import com.aliyun.oss.OSSClient; +import com.aliyun.oss.common.auth.CredentialsProvider; +import com.aliyun.oss.common.auth.DefaultCredentialProvider; +import com.aliyun.oss.model.ObjectMetadata; +import neatlogic.framework.common.config.Config; +import neatlogic.framework.exception.file.FilePathIllegalException; +import neatlogic.framework.exception.file.FileStorageMediumHandlerNotFoundException; +import neatlogic.framework.file.core.IFileStorageHandler; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; + +@Component +public class AliossFileSystemHandler implements InitializingBean, IFileStorageHandler { + + public static final String NAME = "ALIOSS"; + + private OSSClient ossClient; + + @Override + public String getName() { + return NAME; + } + + @Override + public void afterPropertiesSet() throws Exception { + if(StringUtils.isNotBlank(Config.ALIOSS_URL())) { + // 创建ClientConfiguration。ClientConfiguration是OSSClient的配置类,可配置代理、连接超时、最大连接数等参数。 + ClientConfiguration conf = new ClientConfiguration(); + // 设置OSSClient允许打开的最大HTTP连接数,默认为1024个。 + conf.setMaxConnections(1024); + // 设置Socket层传输数据的超时时间,默认为50000毫秒。 + conf.setSocketTimeout(50000); + // 设置建立连接的超时时间,默认为50000毫秒。 + conf.setConnectionTimeout(50000); + // 设置从连接池中获取连接的超时时间(单位:毫秒),默认不超时。 + conf.setConnectionRequestTimeout(1000); + // 设置连接空闲超时时间。超时则关闭连接,默认为60000毫秒。 + conf.setIdleConnectionTime(60000); + // 设置失败请求重试次数,默认为3次。 + conf.setMaxErrorRetry(5); + CredentialsProvider credentialsProvider = new DefaultCredentialProvider(Config.ALIOSS_ACCESSKEY(), Config.ALIOSS_SECRETKEY()); + this.ossClient = new OSSClient(Config.ALIOSS_URL(), credentialsProvider, conf); + } + } + + /** + * @param tenantUuid 租户uuid + * @param inputStream 输入流 + * @param contentType contentType + * @return 附件路径 + */ + @Override + public String saveData(String tenantUuid, InputStream inputStream, String fileId, String contentType, String fileType) throws Exception { + if (ossClient == null) { + throw new FileStorageMediumHandlerNotFoundException("alioss"); + } + // 检查存储桶是否已经存在 + boolean bucketExists = ossClient.doesBucketExist(Config.MINIO_BUCKET()); + if (!bucketExists) { + // 创建一个名为bucketName的存储桶,用于存储照片等zip文件。 + ossClient.createBucket(Config.ALIOSS_BUCKET()); + } + SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm"); + String finalPath = "/" + tenantUuid + "/upload/" + fileType + "/" + format.format(new Date()) + "/" + fileId; + // 使用putObject上传一个文件到存储桶中 + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType(contentType); + ossClient.putObject(Config.ALIOSS_BUCKET(), finalPath, inputStream, metadata); +// fileVo.setPath("minio:" + finalPath); + return AliossFileSystemHandler.NAME.toLowerCase() + ":" + finalPath; + } + + @Override + public InputStream getData(String path) throws Exception { + if (ossClient == null) { + throw new FileStorageMediumHandlerNotFoundException("alioss"); + } + return ossClient.getObject(Config.ALIOSS_BUCKET(), path.replaceAll(NAME.toLowerCase() + ":", "")).getObjectContent(); + } + + @Override + public void deleteData(String filePath) throws Exception { + if (ossClient == null) { + throw new FileStorageMediumHandlerNotFoundException("alioss"); + } + if (StringUtils.isNotBlank(filePath) && filePath.startsWith(NAME.toLowerCase() + ":")) { + String path = filePath.replaceAll(NAME.toLowerCase() + ":", ""); + ossClient.deleteObject(Config.ALIOSS_BUCKET(), path); + } else { + throw new FilePathIllegalException(filePath); + } + } + + @Override + public long getDataLength(String filePath) throws Exception { + if (ossClient == null) { + throw new FileStorageMediumHandlerNotFoundException("alioss"); + } + return ossClient.getObjectMetadata(Config.ALIOSS_BUCKET(), filePath.replaceAll(NAME.toLowerCase() + ":", "")).getContentLength(); + } + + @Override + public boolean isExit(String filePath) throws Exception { + if (ossClient == null) { + throw new FileStorageMediumHandlerNotFoundException("alioss"); + } + ossClient.doesObjectExist(Config.ALIOSS_BUCKET(), filePath.replaceAll(NAME.toLowerCase() + ":", "")); + return true; + } + } -- Gitee From fafcb0c6c11f873687dd45a0981cc093638bb1ec Mon Sep 17 00:00:00 2001 From: 648607640 <648607640@qq.com> Date: Wed, 17 Jan 2024 17:35:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E9=98=BF=E9=87=8Cos?= =?UTF-8?q?s=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/common/config/Config.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/neatlogic/framework/common/config/Config.java b/src/main/java/neatlogic/framework/common/config/Config.java index b0b638c16..751484abd 100644 --- a/src/main/java/neatlogic/framework/common/config/Config.java +++ b/src/main/java/neatlogic/framework/common/config/Config.java @@ -76,6 +76,11 @@ public class Config { private static String JMS_URL; + private static String ALIOSS_URL; + private static String ALIOSS_BUCKET; + private static String ALIOSS_ACCESSKEY; + private static String ALIOSS_SECRETKEY; + private static String MINIO_URL; private static String MINIO_BUCKET; @@ -265,6 +270,22 @@ public class Config { return LOGIN_FAILED_TIMES_CAPTCHA; } + public static String ALIOSS_URL() { + return ALIOSS_URL; + } + + public static String ALIOSS_ACCESSKEY() { + return ALIOSS_ACCESSKEY; + } + + public static String ALIOSS_SECRETKEY() { + return ALIOSS_SECRETKEY; + } + + public static String ALIOSS_BUCKET() { + return ALIOSS_BUCKET; + } + public static String MINIO_URL() { return MINIO_URL; } @@ -510,6 +531,11 @@ public class Config { JMS_URL = prop.getProperty("jms.url", "tcp://localhost:8161"); + MINIO_URL = prop.getProperty("alioss.url"); + MINIO_ACCESSKEY = prop.getProperty("alioss.accesskey", "aliossadmin"); + MINIO_SECRETKEY = prop.getProperty("alioss.secretkey", "aliossadmin"); + MINIO_BUCKET = prop.getProperty("alioss.bucket", "neatlogic"); + MINIO_URL = prop.getProperty("minio.url"); MINIO_ACCESSKEY = prop.getProperty("minio.accesskey", "minioadmin"); MINIO_SECRETKEY = prop.getProperty("minio.secretkey", "minioadmin"); -- Gitee From 56fa766fb7a1483bf8376c2aeacfdb5ec6a69262 Mon Sep 17 00:00:00 2001 From: 648607640 <648607640@qq.com> Date: Wed, 17 Jan 2024 17:36:49 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E9=98=BF=E9=87=8Cos?= =?UTF-8?q?s=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/neatlogic/framework/common/config/Config.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/neatlogic/framework/common/config/Config.java b/src/main/java/neatlogic/framework/common/config/Config.java index 751484abd..0691272b1 100644 --- a/src/main/java/neatlogic/framework/common/config/Config.java +++ b/src/main/java/neatlogic/framework/common/config/Config.java @@ -531,10 +531,10 @@ public class Config { JMS_URL = prop.getProperty("jms.url", "tcp://localhost:8161"); - MINIO_URL = prop.getProperty("alioss.url"); - MINIO_ACCESSKEY = prop.getProperty("alioss.accesskey", "aliossadmin"); - MINIO_SECRETKEY = prop.getProperty("alioss.secretkey", "aliossadmin"); - MINIO_BUCKET = prop.getProperty("alioss.bucket", "neatlogic"); + ALIOSS_URL = prop.getProperty("alioss.url"); + ALIOSS_ACCESSKEY = prop.getProperty("alioss.accesskey", "aliossadmin"); + ALIOSS_SECRETKEY = prop.getProperty("alioss.secretkey", "aliossadmin"); + ALIOSS_BUCKET = prop.getProperty("alioss.bucket", "neatlogic"); MINIO_URL = prop.getProperty("minio.url"); MINIO_ACCESSKEY = prop.getProperty("minio.accesskey", "minioadmin"); -- Gitee