mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 20:49:00 +08:00
✨ Introducing new features. close #3825 common-oss 支持 local file 模式
This commit is contained in:
1 parent
b8835cddc4
commit
26f9c1d324
15 files changed
+470
-67
No files matched your search
@@ -1,24 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common</artifactId>
|
||||
<version>4.3.0</version>
|
||||
</parent>
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common</artifactId>
|
||||
<version>4.3.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>pigx-common-oss</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>pigx-common-oss</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<description>pigx 文件系统依赖</description>
|
||||
<description>pigx 文件系统依赖</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-s3</artifactId>
|
||||
<version>${aws.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-s3</artifactId>
|
||||
<version>${aws.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.file;
|
||||
|
||||
import com.pig4cloud.pigx.common.file.core.FileProperties;
|
||||
import com.pig4cloud.pigx.common.file.local.LocalFileAutoConfiguration;
|
||||
import com.pig4cloud.pigx.common.file.oss.OssAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* aws 自动配置类
|
||||
*
|
||||
* @author lengleng
|
||||
* @author 858695266
|
||||
*/
|
||||
@Import({ LocalFileAutoConfiguration.class, OssAutoConfiguration.class })
|
||||
@EnableConfigurationProperties({ FileProperties.class })
|
||||
public class FileAutoConfiguration {
|
||||
|
||||
}
|
||||
Executable
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.file.core;
|
||||
|
||||
import com.pig4cloud.pigx.common.file.local.LocalFileProperties;
|
||||
import com.pig4cloud.pigx.common.file.oss.OssProperties;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 文件 配置信息
|
||||
*
|
||||
* @author lengleng
|
||||
* <p>
|
||||
* bucket 设置公共读权限
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "file")
|
||||
public class FileProperties {
|
||||
|
||||
/**
|
||||
* 默认的存储桶名称
|
||||
*/
|
||||
private String bucketName = "payerp";
|
||||
|
||||
/**
|
||||
* 本地文件配置信息
|
||||
*/
|
||||
private LocalFileProperties local;
|
||||
|
||||
/**
|
||||
* oss 文件配置信息
|
||||
*/
|
||||
private OssProperties oss;
|
||||
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.pig4cloud.pigx.common.file.core;
|
||||
|
||||
import com.amazonaws.services.s3.model.Bucket;
|
||||
import com.amazonaws.services.s3.model.S3Object;
|
||||
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件操作模板
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2022/4/19
|
||||
*/
|
||||
public interface FileTemplate extends InitializingBean {
|
||||
|
||||
/**
|
||||
* 创建bucket
|
||||
* @param bucketName bucket名称
|
||||
*/
|
||||
void createBucket(String bucketName);
|
||||
|
||||
/**
|
||||
* 获取全部bucket
|
||||
* <p>
|
||||
*
|
||||
* API Documentation</a>
|
||||
*/
|
||||
List<Bucket> getAllBuckets();
|
||||
|
||||
/**
|
||||
* @param bucketName bucket名称
|
||||
* @see <a href= Documentation</a>
|
||||
*/
|
||||
void removeBucket(String bucketName);
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @param stream 文件流
|
||||
* @param contextType 文件类型
|
||||
* @throws Exception
|
||||
*/
|
||||
void putObject(String bucketName, String objectName, InputStream stream, String contextType) throws Exception;
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @param stream 文件流
|
||||
* @param contextType 文件类型
|
||||
* @throws Exception
|
||||
*/
|
||||
void putObject(String bucketName, String objectName, InputStream stream) throws Exception;
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @return 二进制流 API Documentation</a>
|
||||
*/
|
||||
S3Object getObject(String bucketName, String objectName);
|
||||
|
||||
void removeObject(String bucketName, String objectName) throws Exception;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
default void afterPropertiesSet() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件前置查询文件
|
||||
* @param bucketName bucket名称
|
||||
* @param prefix 前缀
|
||||
* @param recursive 是否递归查询
|
||||
* @return S3ObjectSummary 列表
|
||||
* @see <a href="http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/ListObjects">AWS
|
||||
* API Documentation</a>
|
||||
*/
|
||||
List<S3ObjectSummary> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive);
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.file.local;
|
||||
|
||||
import com.pig4cloud.pigx.common.file.core.FileProperties;
|
||||
import com.pig4cloud.pigx.common.file.core.FileTemplate;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* aws 自动配置类
|
||||
*
|
||||
* @author lengleng
|
||||
* @author 858695266
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class LocalFileAutoConfiguration {
|
||||
|
||||
private final FileProperties properties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(LocalFileTemplate.class)
|
||||
@ConditionalOnProperty(name = "file.local.enable", havingValue = "true", matchIfMissing = true)
|
||||
public FileTemplate localFileTemplate() {
|
||||
return new LocalFileTemplate(properties);
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.file.local;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 本地文件 配置信息
|
||||
*
|
||||
* @author lengleng
|
||||
* <p>
|
||||
* bucket 设置公共读权限
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "local")
|
||||
public class LocalFileProperties {
|
||||
|
||||
/**
|
||||
* 是否开启
|
||||
*/
|
||||
private boolean enable;
|
||||
|
||||
/**
|
||||
* 默认路径
|
||||
*/
|
||||
private String basePath;
|
||||
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package com.pig4cloud.pigx.common.file.local;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.amazonaws.services.s3.model.Bucket;
|
||||
import com.amazonaws.services.s3.model.S3Object;
|
||||
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
||||
import com.pig4cloud.pigx.common.file.core.FileProperties;
|
||||
import com.pig4cloud.pigx.common.file.core.FileTemplate;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 本地文件读取模式
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2022/4/19
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class LocalFileTemplate implements FileTemplate {
|
||||
|
||||
private final FileProperties properties;
|
||||
|
||||
/**
|
||||
* 创建bucket
|
||||
* @param bucketName bucket名称
|
||||
*/
|
||||
@Override
|
||||
public void createBucket(String bucketName) {
|
||||
FileUtil.mkdir(properties.getLocal().getBasePath() + FileUtil.FILE_SEPARATOR + bucketName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部bucket
|
||||
* <p>
|
||||
* <p>
|
||||
* API Documentation</a>
|
||||
*/
|
||||
@Override
|
||||
public List<Bucket> getAllBuckets() {
|
||||
return Arrays.stream(FileUtil.ls(properties.getLocal().getBasePath())).filter(FileUtil::isDirectory)
|
||||
.map(dir -> new Bucket(dir.getName())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bucketName bucket名称
|
||||
* @see <a href= Documentation</a>
|
||||
*/
|
||||
@Override
|
||||
public void removeBucket(String bucketName) {
|
||||
FileUtil.del(properties.getLocal().getBasePath() + FileUtil.FILE_SEPARATOR + bucketName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @param stream 文件流
|
||||
* @param contextType 文件类型
|
||||
*/
|
||||
@Override
|
||||
public void putObject(String bucketName, String objectName, InputStream stream, String contextType) {
|
||||
// 当 Bucket 不存在时创建
|
||||
String dir = properties.getLocal().getBasePath() + FileUtil.FILE_SEPARATOR + bucketName;
|
||||
if (!FileUtil.isDirectory(properties.getLocal().getBasePath() + FileUtil.FILE_SEPARATOR + bucketName)) {
|
||||
createBucket(bucketName);
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
File file = FileUtil.file(dir + FileUtil.FILE_SEPARATOR + objectName);
|
||||
FileUtil.writeFromStream(stream, file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @return 二进制流 API Documentation</a>
|
||||
*/
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public S3Object getObject(String bucketName, String objectName) {
|
||||
String dir = properties.getLocal().getBasePath() + FileUtil.FILE_SEPARATOR + bucketName;
|
||||
S3Object s3Object = new S3Object();
|
||||
s3Object.setObjectContent(FileUtil.getInputStream(dir + FileUtil.FILE_SEPARATOR + objectName));
|
||||
return s3Object;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bucketName
|
||||
* @param objectName
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void removeObject(String bucketName, String objectName) throws Exception {
|
||||
String dir = properties.getLocal().getBasePath() + FileUtil.FILE_SEPARATOR + bucketName;
|
||||
FileUtil.del(dir + FileUtil.FILE_SEPARATOR + objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @param stream 文件流
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
|
||||
putObject(bucketName, objectName, stream, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件前置查询文件
|
||||
* @param bucketName bucket名称
|
||||
* @param prefix 前缀
|
||||
* @param recursive 是否递归查询
|
||||
* @return S3ObjectSummary 列表
|
||||
* @see <a href="http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/ListObjects">AWS
|
||||
* API Documentation</a>
|
||||
*/
|
||||
@Override
|
||||
public List<S3ObjectSummary> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
|
||||
String dir = properties.getLocal().getBasePath() + FileUtil.FILE_SEPARATOR + bucketName;
|
||||
|
||||
return Arrays.stream(FileUtil.ls(dir)).filter(file -> file.getName().startsWith(prefix)).map(file -> {
|
||||
S3ObjectSummary summary = new S3ObjectSummary();
|
||||
summary.setKey(file.getName());
|
||||
return new S3ObjectSummary();
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
+11
-9
@@ -15,15 +15,17 @@
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.oss;
|
||||
package com.pig4cloud.pigx.common.file.oss;
|
||||
|
||||
import com.pig4cloud.pigx.common.oss.http.OssEndpoint;
|
||||
import com.pig4cloud.pigx.common.oss.service.OssTemplate;
|
||||
import com.pig4cloud.pigx.common.file.core.FileProperties;
|
||||
import com.pig4cloud.pigx.common.file.core.FileTemplate;
|
||||
import com.pig4cloud.pigx.common.file.oss.http.OssEndpoint;
|
||||
import com.pig4cloud.pigx.common.file.oss.service.OssTemplate;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
/**
|
||||
* aws 自动配置类
|
||||
@@ -32,20 +34,20 @@ import org.springframework.context.annotation.Bean;
|
||||
* @author 858695266
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@EnableConfigurationProperties({ OssProperties.class })
|
||||
public class OssAutoConfiguration {
|
||||
|
||||
private final OssProperties properties;
|
||||
private final FileProperties properties;
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean(OssTemplate.class)
|
||||
@ConditionalOnProperty(name = "oss.enable", havingValue = "true", matchIfMissing = true)
|
||||
public OssTemplate ossTemplate() {
|
||||
@ConditionalOnProperty(name = "file.oss.enable", havingValue = "true")
|
||||
public FileTemplate ossTemplate() {
|
||||
return new OssTemplate(properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "oss.info", havingValue = "true")
|
||||
@ConditionalOnProperty(name = "file.oss.info", havingValue = "true")
|
||||
public OssEndpoint ossEndpoint(OssTemplate template) {
|
||||
return new OssEndpoint(template);
|
||||
}
|
||||
+1
-6
@@ -15,7 +15,7 @@
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.oss;
|
||||
package com.pig4cloud.pigx.common.file.oss;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@@ -72,11 +72,6 @@ public class OssProperties {
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* 默认的存储桶名称
|
||||
*/
|
||||
private String bucketName = "payerp";
|
||||
|
||||
/**
|
||||
* 最大线程数,默认: 100
|
||||
*/
|
||||
+2
-2
@@ -15,12 +15,12 @@
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.oss.http;
|
||||
package com.pig4cloud.pigx.common.file.oss.http;
|
||||
|
||||
import com.amazonaws.services.s3.model.Bucket;
|
||||
import com.amazonaws.services.s3.model.S3Object;
|
||||
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
||||
import com.pig4cloud.pigx.common.oss.service.OssTemplate;
|
||||
import com.pig4cloud.pigx.common.file.oss.service.OssTemplate;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Cleanup;
|
||||
import lombok.SneakyThrows;
|
||||
+10
-9
@@ -15,7 +15,7 @@
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.oss.service;
|
||||
package com.pig4cloud.pigx.common.file.oss.service;
|
||||
|
||||
import com.amazonaws.ClientConfiguration;
|
||||
import com.amazonaws.auth.AWSCredentials;
|
||||
@@ -27,7 +27,8 @@ import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3Client;
|
||||
import com.amazonaws.services.s3.model.*;
|
||||
import com.amazonaws.util.IOUtils;
|
||||
import com.pig4cloud.pigx.common.oss.OssProperties;
|
||||
import com.pig4cloud.pigx.common.file.core.FileProperties;
|
||||
import com.pig4cloud.pigx.common.file.core.FileTemplate;
|
||||
import lombok.Cleanup;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
@@ -47,9 +48,9 @@ import java.util.*;
|
||||
* @since 1.0
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class OssTemplate implements InitializingBean {
|
||||
public class OssTemplate implements InitializingBean, FileTemplate {
|
||||
|
||||
private final OssProperties ossProperties;
|
||||
private final FileProperties properties;
|
||||
|
||||
private AmazonS3 amazonS3;
|
||||
|
||||
@@ -220,16 +221,16 @@ public class OssTemplate implements InitializingBean {
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
ClientConfiguration clientConfiguration = new ClientConfiguration();
|
||||
clientConfiguration.setMaxConnections(ossProperties.getMaxConnections());
|
||||
clientConfiguration.setMaxConnections(properties.getOss().getMaxConnections());
|
||||
|
||||
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
|
||||
ossProperties.getEndpoint(), ossProperties.getRegion());
|
||||
AWSCredentials awsCredentials = new BasicAWSCredentials(ossProperties.getAccessKey(),
|
||||
ossProperties.getSecretKey());
|
||||
properties.getOss().getEndpoint(), properties.getOss().getRegion());
|
||||
AWSCredentials awsCredentials = new BasicAWSCredentials(properties.getOss().getAccessKey(),
|
||||
properties.getOss().getSecretKey());
|
||||
AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
|
||||
this.amazonS3 = AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)
|
||||
.withClientConfiguration(clientConfiguration).withCredentials(awsCredentialsProvider)
|
||||
.disableChunkedEncoding().withPathStyleAccessEnabled(ossProperties.getPathStyleAccess()).build();
|
||||
.disableChunkedEncoding().withPathStyleAccessEnabled(properties.getOss().getPathStyleAccess()).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.pig4cloud.pigx.common.oss.OssAutoConfiguration
|
||||
com.pig4cloud.pigx.common.file.FileAutoConfiguration
|
||||
+10
-10
@@ -26,8 +26,8 @@ import com.pig4cloud.pigx.admin.api.entity.SysFile;
|
||||
import com.pig4cloud.pigx.admin.mapper.SysFileMapper;
|
||||
import com.pig4cloud.pigx.admin.service.SysFileService;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.oss.OssProperties;
|
||||
import com.pig4cloud.pigx.common.oss.service.OssTemplate;
|
||||
import com.pig4cloud.pigx.common.file.core.FileProperties;
|
||||
import com.pig4cloud.pigx.common.file.core.FileTemplate;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -51,9 +51,9 @@ import java.util.Map;
|
||||
@AllArgsConstructor
|
||||
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
|
||||
|
||||
private final OssProperties ossProperties;
|
||||
private final FileTemplate fileTemplate;
|
||||
|
||||
private final OssTemplate minioTemplate;
|
||||
private final FileProperties properties;
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
@@ -64,12 +64,12 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
|
||||
public R uploadFile(MultipartFile file) {
|
||||
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
|
||||
Map<String, String> resultMap = new HashMap<>(4);
|
||||
resultMap.put("bucketName", ossProperties.getBucketName());
|
||||
resultMap.put("bucketName", properties.getBucketName());
|
||||
resultMap.put("fileName", fileName);
|
||||
resultMap.put("url", String.format("/admin/sys-file/%s/%s", ossProperties.getBucketName(), fileName));
|
||||
resultMap.put("url", String.format("/admin/sys-file/%s/%s", properties.getBucketName(), fileName));
|
||||
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
minioTemplate.putObject(ossProperties.getBucketName(), fileName, inputStream, file.getContentType());
|
||||
fileTemplate.putObject(properties.getBucketName(), fileName, inputStream, file.getContentType());
|
||||
// 文件管理数据记录,收集管理追踪文件
|
||||
fileLog(file, fileName);
|
||||
}
|
||||
@@ -88,7 +88,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
|
||||
*/
|
||||
@Override
|
||||
public void getFile(String bucket, String fileName, HttpServletResponse response) {
|
||||
try (S3Object s3Object = minioTemplate.getObject(bucket, fileName)) {
|
||||
try (S3Object s3Object = fileTemplate.getObject(bucket, fileName)) {
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IoUtil.copy(s3Object.getObjectContent(), response.getOutputStream());
|
||||
}
|
||||
@@ -107,7 +107,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean deleteFile(Long id) {
|
||||
SysFile file = this.getById(id);
|
||||
minioTemplate.removeObject(ossProperties.getBucketName(), file.getFileName());
|
||||
fileTemplate.removeObject(properties.getBucketName(), file.getFileName());
|
||||
return this.removeById(id);
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
|
||||
sysFile.setOriginal(file.getOriginalFilename());
|
||||
sysFile.setFileSize(file.getSize());
|
||||
sysFile.setType(FileUtil.extName(file.getOriginalFilename()));
|
||||
sysFile.setBucketName(ossProperties.getBucketName());
|
||||
sysFile.setBucketName(properties.getBucketName());
|
||||
this.save(sysFile);
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -2,8 +2,9 @@ package com.pig4cloud.pigx.ureport.config;
|
||||
|
||||
import com.bstek.ureport.UReportPropertyPlaceholderConfigurer;
|
||||
import com.bstek.ureport.provider.report.ReportProvider;
|
||||
import com.pig4cloud.pigx.common.oss.OssProperties;
|
||||
import com.pig4cloud.pigx.common.oss.service.OssTemplate;
|
||||
import com.pig4cloud.pigx.common.file.core.FileProperties;
|
||||
import com.pig4cloud.pigx.common.file.core.FileTemplate;
|
||||
import com.pig4cloud.pigx.common.file.oss.service.OssTemplate;
|
||||
import com.pig4cloud.pigx.ureport.processor.UReportPropertyPlaceholderConfigurerPlus;
|
||||
import com.pig4cloud.pigx.ureport.provider.DfsReportProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
@@ -26,8 +27,8 @@ import java.util.Locale;
|
||||
public class UreportExtConfig {
|
||||
|
||||
@Bean
|
||||
public ReportProvider dfsReportProvider(OssTemplate ossTemplate, OssProperties properties) {
|
||||
return new DfsReportProvider(ossTemplate, properties);
|
||||
public ReportProvider dfsReportProvider(FileTemplate template, FileProperties properties) {
|
||||
return new DfsReportProvider(template, properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
+8
-8
@@ -4,8 +4,8 @@ import com.amazonaws.services.s3.model.S3Object;
|
||||
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
||||
import com.bstek.ureport.provider.report.ReportFile;
|
||||
import com.bstek.ureport.provider.report.ReportProvider;
|
||||
import com.pig4cloud.pigx.common.oss.OssProperties;
|
||||
import com.pig4cloud.pigx.common.oss.service.OssTemplate;
|
||||
import com.pig4cloud.pigx.common.file.core.FileProperties;
|
||||
import com.pig4cloud.pigx.common.file.core.FileTemplate;
|
||||
import lombok.Cleanup;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -30,20 +30,20 @@ public class DfsReportProvider implements ReportProvider {
|
||||
*/
|
||||
private static final String PREFIX = "oss_";
|
||||
|
||||
private final OssTemplate ossTemplate;
|
||||
private final FileTemplate fileTemplate;
|
||||
|
||||
private final OssProperties properties;
|
||||
private final FileProperties properties;
|
||||
|
||||
@Override
|
||||
public InputStream loadReport(String file) {
|
||||
S3Object object = ossTemplate.getObject(properties.getBucketName(), file);
|
||||
S3Object object = fileTemplate.getObject(properties.getBucketName(), file);
|
||||
return object.getObjectContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteReport(String file) {
|
||||
try {
|
||||
ossTemplate.removeObject(properties.getBucketName(), file);
|
||||
fileTemplate.removeObject(properties.getBucketName(), file);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("文件删除失败 ", e);
|
||||
@@ -52,7 +52,7 @@ public class DfsReportProvider implements ReportProvider {
|
||||
|
||||
@Override
|
||||
public List<ReportFile> getReportFiles() {
|
||||
List<S3ObjectSummary> objectSummaryList = ossTemplate.getAllObjectsByPrefix(properties.getBucketName(), PREFIX,
|
||||
List<S3ObjectSummary> objectSummaryList = fileTemplate.getAllObjectsByPrefix(properties.getBucketName(), PREFIX,
|
||||
true);
|
||||
|
||||
return objectSummaryList.stream()
|
||||
@@ -66,7 +66,7 @@ public class DfsReportProvider implements ReportProvider {
|
||||
try {
|
||||
@Cleanup
|
||||
InputStream inputStream = IOUtils.toInputStream(content);
|
||||
ossTemplate.putObject(properties.getBucketName(), file, inputStream);
|
||||
fileTemplate.putObject(properties.getBucketName(), file, inputStream);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("文件上传失败", e);
|
||||
|
||||
Reference in new issue
Block a user