parent
6d657ded4c
commit
7453da0c75
@ -0,0 +1,53 @@
|
|||||||
|
package com.luoo.user.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||||
|
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||||
|
import software.amazon.awssdk.regions.Region;
|
||||||
|
import software.amazon.awssdk.services.s3.S3Client;
|
||||||
|
import software.amazon.awssdk.services.s3.S3Configuration;
|
||||||
|
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class AwsS3Config {
|
||||||
|
@Value("aws.accessKeyId")
|
||||||
|
private String accessKeyId;
|
||||||
|
|
||||||
|
@Value("aws.secretAccessKey")
|
||||||
|
private String secretAccessKey;
|
||||||
|
|
||||||
|
@Value("aws.endpointUrl")
|
||||||
|
private String endpointUrl;
|
||||||
|
|
||||||
|
private static final Region region = Region.of("cn-east-1");
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public S3Client s3Client(){
|
||||||
|
AwsBasicCredentials awsBasicCredentials = AwsBasicCredentials.create("GLwHmLTZ05Kw9RyCGJXnIkua", "ynOBIqdNXH5HBgrVA29DTn4cUSh1wAI");
|
||||||
|
S3Configuration s3Config = S3Configuration.builder().pathStyleAccessEnabled(true).build();
|
||||||
|
S3Client s3 = S3Client.builder()
|
||||||
|
.endpointOverride(URI.create("https://s3.bitiful.net/"))
|
||||||
|
.credentialsProvider(StaticCredentialsProvider.create(awsBasicCredentials))
|
||||||
|
.region(region)
|
||||||
|
.serviceConfiguration(s3Config)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return s3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public S3Presigner s3Presigner(){
|
||||||
|
S3Configuration s3Config = S3Configuration.builder().pathStyleAccessEnabled(true).build();
|
||||||
|
S3Presigner presigner = S3Presigner.builder()
|
||||||
|
.endpointOverride(URI.create("https://s3.bitiful.net/"))
|
||||||
|
.region(region)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return presigner;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.luoo.user.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对象存储线程池配置类
|
||||||
|
*/
|
||||||
|
@EnableAsync
|
||||||
|
@Configuration
|
||||||
|
public class ThreadPoolConfig {
|
||||||
|
|
||||||
|
@Bean("awsThreadPoolExecutor")
|
||||||
|
public ThreadPoolTaskExecutor awsThreadPoolExecutor(){
|
||||||
|
// cpu参数
|
||||||
|
int cpuCount = Runtime.getRuntime().availableProcessors();
|
||||||
|
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
|
||||||
|
// 核心线程数
|
||||||
|
taskExecutor.setCorePoolSize(cpuCount);
|
||||||
|
// 最大线程数
|
||||||
|
taskExecutor.setMaxPoolSize(cpuCount * 2);
|
||||||
|
// 任务队列容量
|
||||||
|
taskExecutor.setQueueCapacity(128);
|
||||||
|
// 空闲队列存活时间
|
||||||
|
taskExecutor.setKeepAliveSeconds(20);
|
||||||
|
// 线程前缀
|
||||||
|
taskExecutor.setThreadNamePrefix("awsTaskExecutor-");
|
||||||
|
// 拒绝策略
|
||||||
|
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||||
|
// 任务完成后自动关闭线程池
|
||||||
|
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
|
||||||
|
// 初始化
|
||||||
|
taskExecutor.initialize();
|
||||||
|
|
||||||
|
return taskExecutor;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.luoo.user.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import api.Result;
|
||||||
|
import com.luoo.user.service.S3Service;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@CrossOrigin
|
||||||
|
public class S3Controller {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private S3Service s3Service;
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/awstest")
|
||||||
|
public Result test() throws UnsupportedEncodingException {
|
||||||
|
|
||||||
|
// s3Service.listObjects()
|
||||||
|
List list = s3Service.list();
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/awsUpload")
|
||||||
|
public Result upload(MultipartFile file) {
|
||||||
|
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
||||||
|
// String fileName = UUID.randomUUID().toString().trim().replaceAll("-", "");
|
||||||
|
|
||||||
|
|
||||||
|
String filePath = sdf.format(new Date()) + "/" + file.getOriginalFilename();
|
||||||
|
try{
|
||||||
|
int code = s3Service.singleUpload("indie", filePath, file);
|
||||||
|
|
||||||
|
} catch (Exception ex){
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
package com.luoo.user.service;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import software.amazon.awssdk.core.sync.RequestBody;
|
||||||
|
import software.amazon.awssdk.http.SdkHttpResponse;
|
||||||
|
import software.amazon.awssdk.services.s3.S3Client;
|
||||||
|
|
||||||
|
import software.amazon.awssdk.services.s3.model.*;
|
||||||
|
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class S3Service {
|
||||||
|
@Resource
|
||||||
|
private S3Client s3Client;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private S3Presigner s3Presigner;
|
||||||
|
|
||||||
|
//
|
||||||
|
// public ListObjectsResponse listObjects(){
|
||||||
|
// ListObjectsResponse indie = s3Client.listObjects( ListObjectsV2Request.builder().bucket("indie"));
|
||||||
|
// return indie;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
public List<S3Object> list() throws UnsupportedEncodingException {
|
||||||
|
// if(StringUtil.isEmpty(bucket)) return ResultUtil.resultFail("参数错误");
|
||||||
|
|
||||||
|
ListObjectsV2Request.Builder builder = ListObjectsV2Request.builder();
|
||||||
|
// 设置bucket
|
||||||
|
builder.bucket("indie");
|
||||||
|
|
||||||
|
|
||||||
|
ListObjectsV2Request listObjReq = builder.build();
|
||||||
|
ListObjectsV2Response listObjRes = s3Client.listObjectsV2(listObjReq);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
List<S3Object> s3ObjectList = listObjRes.contents();
|
||||||
|
|
||||||
|
|
||||||
|
return s3ObjectList;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异步完整上传不分片
|
||||||
|
* @param bucket bucket
|
||||||
|
* @param key 对象路径
|
||||||
|
* @param file 文件对象
|
||||||
|
*/
|
||||||
|
@Async("awsThreadPoolExecutor")
|
||||||
|
public int singleUpload(String bucket, String key, MultipartFile file) throws IOException {
|
||||||
|
Long startTime = System.currentTimeMillis() / 1000;
|
||||||
|
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
|
||||||
|
.bucket(bucket)
|
||||||
|
.key(key)
|
||||||
|
.build();
|
||||||
|
RequestBody requestBody = RequestBody.fromInputStream(file.getInputStream(), file.getSize());
|
||||||
|
PutObjectResponse putObjectResponse = s3Client.putObject(putObjectRequest, requestBody);
|
||||||
|
SdkHttpResponse sdkHttpResponse = putObjectResponse.sdkHttpResponse();
|
||||||
|
if(!sdkHttpResponse.isSuccessful()){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
long endTime = System.currentTimeMillis() / 1000;
|
||||||
|
// log.info("上传文件(" + key + ")总计耗费时间为:" + (endTime - startTime) + " 秒");
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue