feat: initial iShare project code
This commit is contained in:
28
pigx-common/pigx-common-oss/pom.xml
Normal file
28
pigx-common/pigx-common-oss/pom.xml
Normal file
@@ -0,0 +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>5.2.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>pigx-common-oss</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<description>pigx 文件系统依赖</description>
|
||||
|
||||
<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>
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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 = "local";
|
||||
|
||||
/**
|
||||
* 本地文件配置信息
|
||||
*/
|
||||
private LocalFileProperties local;
|
||||
|
||||
/**
|
||||
* oss 文件配置信息
|
||||
*/
|
||||
private OssProperties oss;
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.oss;
|
||||
|
||||
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.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
/**
|
||||
* aws 自动配置类
|
||||
*
|
||||
* @author lengleng
|
||||
* @author 858695266
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class OssAutoConfiguration {
|
||||
|
||||
private final FileProperties properties;
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConditionalOnMissingBean(OssTemplate.class)
|
||||
@ConditionalOnProperty(name = "file.oss.enable", havingValue = "true")
|
||||
public FileTemplate ossTemplate() {
|
||||
return new OssTemplate(properties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(name = "file.oss.info", havingValue = "true")
|
||||
public OssEndpoint ossEndpoint(OssTemplate template) {
|
||||
return new OssEndpoint(template);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.oss;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* aws 配置信息
|
||||
*
|
||||
* @author lengleng
|
||||
* @author 858695266 配置文件添加: oss: enable: true endpoint: http://127.0.0.1:9000 #
|
||||
* pathStyleAccess 采用nginx反向代理或者AWS S3 配置成true,支持第三方云存储配置成false pathStyleAccess: false
|
||||
* access-key: lengleng secret-key: lengleng bucket-name: lengleng region: custom-domain:
|
||||
* https://oss.xxx.com/lengleng
|
||||
* <p>
|
||||
* bucket 设置公共读权限
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "oss")
|
||||
public class OssProperties {
|
||||
|
||||
/**
|
||||
* 对象存储服务的URL
|
||||
*/
|
||||
private String endpoint;
|
||||
|
||||
/**
|
||||
* 自定义域名
|
||||
*/
|
||||
private String customDomain;
|
||||
|
||||
/**
|
||||
* true path-style nginx 反向代理和S3默认支持 pathStyle {http://endpoint/bucketname} false
|
||||
* supports virtual-hosted-style 阿里云等需要配置为 virtual-hosted-style
|
||||
* 模式{http://bucketname.endpoint}
|
||||
*/
|
||||
private Boolean pathStyleAccess = true;
|
||||
|
||||
/**
|
||||
* 应用ID
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 区域
|
||||
*/
|
||||
private String region;
|
||||
|
||||
/**
|
||||
* Access key就像用户ID,可以唯一标识你的账户
|
||||
*/
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* Secret key是你账户的密码
|
||||
*/
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* 最大线程数,默认: 100
|
||||
*/
|
||||
private Integer maxConnections = 100;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* 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.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.file.oss.service.OssTemplate;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Cleanup;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* aws 对外提供服务端点
|
||||
*
|
||||
* @author lengleng
|
||||
* @author 858695266
|
||||
* <p>
|
||||
* oss.info
|
||||
*/
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/oss")
|
||||
@ConditionalOnProperty(name = "file.oss.info", havingValue = "true")
|
||||
public class OssEndpoint {
|
||||
|
||||
private final OssTemplate template;
|
||||
|
||||
/**
|
||||
* Bucket Endpoints
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PostMapping("/bucket/{bucketName}")
|
||||
public Bucket createBucker(@PathVariable String bucketName) {
|
||||
|
||||
template.createBucket(bucketName);
|
||||
return template.getBucket(bucketName).get();
|
||||
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@GetMapping("/bucket")
|
||||
public List<Bucket> getBuckets() {
|
||||
return template.getAllBuckets();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@GetMapping("/bucket/{bucketName}")
|
||||
public Bucket getBucket(@PathVariable String bucketName) {
|
||||
return template.getBucket(bucketName).orElseThrow(() -> new IllegalArgumentException("Bucket Name not found!"));
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@DeleteMapping("/bucket/{bucketName}")
|
||||
@ResponseStatus(HttpStatus.ACCEPTED)
|
||||
public void deleteBucket(@PathVariable String bucketName) {
|
||||
template.removeBucket(bucketName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Object Endpoints
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PostMapping("/object/{bucketName}")
|
||||
public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName) {
|
||||
String name = object.getOriginalFilename();
|
||||
@Cleanup
|
||||
InputStream inputStream = object.getInputStream();
|
||||
template.putObject(bucketName, name, inputStream, object.getSize(), object.getContentType());
|
||||
return template.getObjectInfo(bucketName, name);
|
||||
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@PostMapping("/object/{bucketName}/{objectName}")
|
||||
public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName,
|
||||
@PathVariable String objectName) {
|
||||
@Cleanup
|
||||
InputStream inputStream = object.getInputStream();
|
||||
template.putObject(bucketName, objectName, inputStream, object.getSize(), object.getContentType());
|
||||
return template.getObjectInfo(bucketName, objectName);
|
||||
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@GetMapping("/object/{bucketName}/{objectName}")
|
||||
public List<S3ObjectSummary> filterObject(@PathVariable String bucketName, @PathVariable String objectName) {
|
||||
|
||||
return template.getAllObjectsByPrefix(bucketName, objectName, true);
|
||||
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@GetMapping("/object/{bucketName}/{objectName}/{expires}")
|
||||
public Map<String, Object> getObject(@PathVariable String bucketName, @PathVariable String objectName,
|
||||
@PathVariable Integer expires) {
|
||||
Map<String, Object> responseBody = new HashMap<>(8);
|
||||
// Put Object info
|
||||
responseBody.put("bucket", bucketName);
|
||||
responseBody.put("object", objectName);
|
||||
responseBody.put("url", template.getObjectURL(bucketName, objectName, expires));
|
||||
responseBody.put("expires", expires);
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@ResponseStatus(HttpStatus.ACCEPTED)
|
||||
@DeleteMapping("/object/{bucketName}/{objectName}/")
|
||||
public void deleteObject(@PathVariable String bucketName, @PathVariable String objectName) {
|
||||
|
||||
template.removeObject(bucketName, objectName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* 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.oss.service;
|
||||
|
||||
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.model.*;
|
||||
import com.amazonaws.util.IOUtils;
|
||||
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;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* aws-s3 通用存储操作 支持所有兼容s3协议的云存储: {阿里云OSS,腾讯云COS,七牛云,京东云,minio 等}
|
||||
*
|
||||
* @author lengleng
|
||||
* @author 858695266
|
||||
* @date 2020/5/23 6:36 上午
|
||||
* @since 1.0
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class OssTemplate implements InitializingBean, FileTemplate {
|
||||
|
||||
private final FileProperties properties;
|
||||
|
||||
private AmazonS3 amazonS3;
|
||||
|
||||
/**
|
||||
* 创建bucket
|
||||
* @param bucketName bucket名称
|
||||
*/
|
||||
@SneakyThrows
|
||||
public void createBucket(String bucketName) {
|
||||
if (!amazonS3.doesBucketExistV2(bucketName)) {
|
||||
amazonS3.createBucket((bucketName));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部bucket
|
||||
* <p>
|
||||
*
|
||||
* @see <a href="http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/ListBuckets">AWS
|
||||
* API Documentation</a>
|
||||
*/
|
||||
@SneakyThrows
|
||||
public List<Bucket> getAllBuckets() {
|
||||
return amazonS3.listBuckets();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bucketName bucket名称
|
||||
* @see <a href="http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/ListBuckets">AWS
|
||||
* API Documentation</a>
|
||||
*/
|
||||
@SneakyThrows
|
||||
public Optional<Bucket> getBucket(String bucketName) {
|
||||
return amazonS3.listBuckets().stream().filter(b -> b.getName().equals(bucketName)).findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bucketName bucket名称
|
||||
* @see <a href=
|
||||
* "http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/DeleteBucket">AWS API
|
||||
* Documentation</a>
|
||||
*/
|
||||
@SneakyThrows
|
||||
public void removeBucket(String bucketName) {
|
||||
amazonS3.deleteBucket(bucketName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件前置查询文件
|
||||
* @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>
|
||||
*/
|
||||
@SneakyThrows
|
||||
public List<S3ObjectSummary> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
|
||||
ObjectListing objectListing = amazonS3.listObjects(bucketName, prefix);
|
||||
return new ArrayList<>(objectListing.getObjectSummaries());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件外链
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @param expires 过期时间 <=7
|
||||
* @return url
|
||||
* @see AmazonS3#generatePresignedUrl(String bucketName, String key, Date expiration)
|
||||
*/
|
||||
@SneakyThrows
|
||||
public String getObjectURL(String bucketName, String objectName, Integer expires) {
|
||||
Date date = new Date();
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(date);
|
||||
calendar.add(Calendar.DAY_OF_MONTH, expires);
|
||||
URL url = amazonS3.generatePresignedUrl(bucketName, objectName, calendar.getTime());
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @return 二进制流
|
||||
* @see <a href="http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/GetObject">AWS
|
||||
* API Documentation</a>
|
||||
*/
|
||||
@SneakyThrows
|
||||
public S3Object getObject(String bucketName, String objectName) {
|
||||
return amazonS3.getObject(bucketName, objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @param stream 文件流
|
||||
* @throws Exception
|
||||
*/
|
||||
public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
|
||||
putObject(bucketName, objectName, stream, stream.available(), "application/octet-stream");
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @param stream 文件流
|
||||
* @param contextType 文件类型
|
||||
* @throws Exception
|
||||
*/
|
||||
public void putObject(String bucketName, String objectName, InputStream stream, String contextType)
|
||||
throws Exception {
|
||||
putObject(bucketName, objectName, stream, stream.available(), contextType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @param stream 文件流
|
||||
* @param size 大小
|
||||
* @param contextType 类型
|
||||
* @throws Exception
|
||||
* @see <a href="http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/PutObject">AWS
|
||||
* API Documentation</a>
|
||||
*/
|
||||
public PutObjectResult putObject(String bucketName, String objectName, InputStream stream, long size,
|
||||
String contextType) throws Exception {
|
||||
// String fileName = getFileName(objectName);
|
||||
byte[] bytes = IOUtils.toByteArray(stream);
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
objectMetadata.setContentLength(size);
|
||||
objectMetadata.setContentType(contextType);
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
||||
// 上传
|
||||
return amazonS3.putObject(bucketName, objectName, byteArrayInputStream, objectMetadata);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件信息
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @see <a href="http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/GetObject">AWS
|
||||
* API Documentation</a>
|
||||
*/
|
||||
public S3Object getObjectInfo(String bucketName, String objectName) throws Exception {
|
||||
@Cleanup
|
||||
S3Object object = amazonS3.getObject(bucketName, objectName);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 文件名称
|
||||
* @throws Exception
|
||||
* @see <a href=
|
||||
* "http://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/DeleteObject">AWS API
|
||||
* Documentation</a>
|
||||
*/
|
||||
public void removeObject(String bucketName, String objectName) throws Exception {
|
||||
amazonS3.deleteObject(bucketName, objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
ClientConfiguration clientConfiguration = new ClientConfiguration();
|
||||
clientConfiguration.setMaxConnections(properties.getOss().getMaxConnections());
|
||||
|
||||
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
|
||||
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(properties.getOss().getPathStyleAccess()).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
com.pig4cloud.pigx.common.file.FileAutoConfiguration
|
||||
Reference in New Issue
Block a user