pom依赖
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.12.95</version>
</dependency>
S3Utils
import cn.hutool.core.util.ZipUtil;
import com.amazonaws.ClientConfiguration;
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.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import com.crm.common.config.S3Config;
import com.crm.common.enums.ConflictPolicy;
import com.crm.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
@Component
public class S3Utils {
private BasicAWSCredentials awsCreds = null;
private AmazonS3 s3 = null;
@Autowired
S3Config s3Config;
@PostConstruct
public void init() {
if (StringUtils.isNotBlank(s3Config.getAccessKey()) && StringUtils.isNotBlank(s3Config.getSecretKey())) {
ClientConfiguration config = new ClientConfiguration();
AwsClientBuilder.EndpointConfiguration endpointConfig =
new AwsClientBuilder.EndpointConfiguration(s3Config.getEndpoint(), "cn-north-1");
awsCreds = new BasicAWSCredentials(s3Config.getAccessKey(), s3Config.getSecretKey());
s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(endpointConfig)
.withClientConfiguration(config)
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
}
}
public String uploadFile(MultipartFile file, String moduleName) {
return uploadFile(file, ConflictPolicy.NEW, moduleName);
}
public String uploadFile(MultipartFile file, ConflictPolicy policy, String moduleName) {
if (isEmpty(file)) {
return null;
}
File localFile = null;
try {
String key = s3Config.getProject() + "/" + moduleName + "/" + file.getOriginalFilename();
localFile = File.createTempFile("temp", null);
file.transferTo(localFile);
String prefix = key.substring(0, key.lastIndexOf("."));
String suffix = key.substring(key.indexOf("."));
int maxNum = getMaxVersionNum(s3Config.getBucketName(), prefix, suffix);
if (maxNum != -1) {
switch (policy) {
case NEW:
key = prefix + "(" + (++maxNum) + ")" + suffix;
break;
case RETAIN:
return "文件已存在,根据冲突策略,文件不予替换";
case REPLACE:
default:
break;
}
}
PutObjectRequest request = new PutObjectRequest(s3Config.getBucketName(), key, localFile);
PutObjectResult putObjectResult = s3.putObject(request);
if (StringUtils.isNotEmpty(putObjectResult.getETag())) {
return key;
}
return null;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (localFile != null) {
localFile.delete();
}
}
return null;
}
private int getMaxVersionNum(String bucketName, String prefix, String suffix) {
ListObjectsRequest listRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(prefix).withMaxKeys(100);
ObjectListing objectListing = s3.listObjects(listRequest);
int value = -1;
for (S3ObjectSummary inst : objectListing.getObjectSummaries()) {
String indexStr = inst.getKey().replace(prefix, "").replace("(", "").replace(")", "").replace(suffix, "");
if (indexStr.length() == 0) {
indexStr = "0";
}
value = Math.max(value, Integer.parseInt(indexStr));
}
return value;
}
public void deleteObject(String key) {
if (StringUtils.isBlank(key)) {
throw new IllegalArgumentException("key can not be null");
}
s3.deleteObject(s3Config.getBucketName(), key);
}
public S3ObjectInputStream getFileInputStream(String key) {
S3Object object = s3.getObject(new GetObjectRequest(s3Config.getBucketName(), key));
return object.getObjectContent();
}
public void downloadFile(String key, OutputStream stream) {
InputStream input = getFileInputStream(key);
byte[] data = null;
try {
data = new byte[input.available()];
int len = 0;
while ((len = input.read(data)) != -1) {
stream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void downloadFile(String key, HttpServletResponse response) {
String fileName = key;
byte[] data = null;
OutputStream stream = null;
InputStream input = getFileInputStream(key);
if (key.contains("/")) {
String[] path = key.split("/");
fileName = path[path.length - 1];
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
try {
stream = response.getOutputStream();
data = new byte[input.available()];
int len = 0;
while ((len = input.read(data)) != -1) {
stream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void deleteFolder(String filePath, boolean deleteAll) {
ListObjectsV2Request objectsRequest = new ListObjectsV2Request();
objectsRequest.setBucketName(s3Config.getBucketName());
objectsRequest.setPrefix(filePath);
objectsRequest.setDelimiter(deleteAll ? "" : "/");
objectsRequest.setMaxKeys(1000);
ListObjectsV2Result listObjectsRequest = s3.listObjectsV2(objectsRequest);
List<S3ObjectSummary> objects = listObjectsRequest.getObjectSummaries();
String[] object_keys = new String[objects.size()];
for (int i = 0; i < objects.size(); i++) {
S3ObjectSummary item = objects.get(i);
object_keys[i] = item.getKey();
}
DeleteObjectsRequest dor = new DeleteObjectsRequest(s3Config.getBucketName()).withKeys(object_keys);
s3.deleteObjects(dor);
}
public boolean isEmpty(MultipartFile file) {
if (file == null || file.getSize() <= 0) {
return true;
}
return false;
}
public List<String> getFileKeys() {
List<String> keys = new LinkedList<>();
ListObjectsRequest listRequest = new ListObjectsRequest().withBucketName(s3Config.getBucketName());
try {
ObjectListing objects = s3.listObjects(listRequest);
while (true) {
List<S3ObjectSummary> summaries = objects.getObjectSummaries();
for (S3ObjectSummary summary : summaries) {
keys.add(summary.getKey());
}
if (objects.isTruncated()) {
objects = s3.listNextBatchOfObjects(objects);
} else {
break;
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return keys;
}
public void getBizFile(List<String> keys, File targetZipFile) {
InputStream[] inputStreams = keys.stream().map(this::getFileInputStream).toArray(InputStream[]::new);
String[] strings = keys.stream().map(key -> key.split("/")[key.split("/").length - 1]).toArray(String[]::new);
ZipUtil.zip(targetZipFile, strings, inputStreams);
}
public void downBizFile(List<String> keys, HttpServletResponse response) {
File file = new File(System.currentTimeMillis() + ".zip");
getBizFile(keys, file);
OutputStream toClient = null;
try {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
toClient = new BufferedOutputStream(response.getOutputStream());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (toClient != null) {
try {
toClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
file.delete();
}
}
}
相关配置类
public enum ConflictPolicy {
REPLACE, NEW, RETAIN
}
@Component
@ConfigurationProperties(prefix="aws.s3")
public class S3Config {
private String accessKey;
private String secretKey;
private String bucketName;
private String region;
private String project;
private String module;
private String endpoint;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
}
aws:
s3:
endpoint: https://s3-xxxxx.com
accessKey: xxxxx
secretKey: xxxx
bucketName: xxx
region: cn-north-1
project: xxx
module: dev