diff --git a/luoo_user/pom.xml b/luoo_user/pom.xml index cc80ab2..b901e16 100644 --- a/luoo_user/pom.xml +++ b/luoo_user/pom.xml @@ -74,6 +74,20 @@ com.h2database h2 + + + + + + + + + + + software.amazon.awssdk + s3 + + app diff --git a/luoo_user/src/main/java/com/luoo/user/config/AwsS3Config.java b/luoo_user/src/main/java/com/luoo/user/config/AwsS3Config.java new file mode 100644 index 0000000..9d61b3e --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/config/AwsS3Config.java @@ -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; + } +} diff --git a/luoo_user/src/main/java/com/luoo/user/config/ThreadPoolConfig.java b/luoo_user/src/main/java/com/luoo/user/config/ThreadPoolConfig.java new file mode 100644 index 0000000..324d28c --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/config/ThreadPoolConfig.java @@ -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; + } +} \ No newline at end of file diff --git a/luoo_user/src/main/java/com/luoo/user/controller/S3Controller.java b/luoo_user/src/main/java/com/luoo/user/controller/S3Controller.java new file mode 100644 index 0000000..1fa5a1b --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/controller/S3Controller.java @@ -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(); + } +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/S3Service.java b/luoo_user/src/main/java/com/luoo/user/service/S3Service.java new file mode 100644 index 0000000..e6fbd6c --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/service/S3Service.java @@ -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 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 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; + } +} diff --git a/pom.xml b/pom.xml index d0cb7f9..8700a8a 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,15 @@ swagger-bootstrap-ui ${swagger-bootstrap-ui.version} + + + + software.amazon.awssdk + bom + 2.20.29 + pom + import +