From 602856682de2ae7beaba68168f76c0de3f0ef2fd Mon Sep 17 00:00:00 2001
From: wangqing <408564126@qq.com>
Date: Wed, 24 Jan 2024 07:14:10 +0800
Subject: [PATCH] add s3 upload function for luoo_music
---
luoo_music/pom.xml | 6 +
.../com/luoo/music/config/AwsS3Config.java | 44 ++++++++
.../luoo/music/controller/S3Controller.java | 75 +++++++++++++
.../com/luoo/music/service/S3Service.java | 103 ++++++++++++++++++
4 files changed, 228 insertions(+)
create mode 100644 luoo_music/src/main/java/com/luoo/music/config/AwsS3Config.java
create mode 100644 luoo_music/src/main/java/com/luoo/music/controller/S3Controller.java
create mode 100644 luoo_music/src/main/java/com/luoo/music/service/S3Service.java
diff --git a/luoo_music/pom.xml b/luoo_music/pom.xml
index 0b9600d..0cce7b0 100644
--- a/luoo_music/pom.xml
+++ b/luoo_music/pom.xml
@@ -57,6 +57,12 @@
tika-core
1.27
+
+
+
+ software.amazon.awssdk
+ s3
+
app
diff --git a/luoo_music/src/main/java/com/luoo/music/config/AwsS3Config.java b/luoo_music/src/main/java/com/luoo/music/config/AwsS3Config.java
new file mode 100644
index 0000000..ba8e41a
--- /dev/null
+++ b/luoo_music/src/main/java/com/luoo/music/config/AwsS3Config.java
@@ -0,0 +1,44 @@
+package com.luoo.music.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;
+
+@Configuration
+public class AwsS3Config {
+
+ 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_music/src/main/java/com/luoo/music/controller/S3Controller.java b/luoo_music/src/main/java/com/luoo/music/controller/S3Controller.java
new file mode 100644
index 0000000..d9a3aa2
--- /dev/null
+++ b/luoo_music/src/main/java/com/luoo/music/controller/S3Controller.java
@@ -0,0 +1,75 @@
+package com.luoo.music.controller;
+
+
+import api.Result;
+import com.luoo.music.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 java.io.UnsupportedEncodingException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+
+@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();
+ }
+
+
+ /**
+ * 文件存储目录规划
+ *
+ * music 存放期刊和期刊歌曲 二级目录为期刊期刊号 三级目录存放期刊歌曲和封面图片和歌曲图片
+ *
+ * song 存放通用歌曲
+ *
+ * image存放图片
+ *
+ * img
+ *
+ * user/avatar/111.jpg
+ *
+ *
+ *
+ *
+ */
+ @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();
+ }
+
+ @PostMapping("/awsCopy")
+ public Result copy() {
+ s3Service.copy();
+ return Result.success();
+ }
+}
diff --git a/luoo_music/src/main/java/com/luoo/music/service/S3Service.java b/luoo_music/src/main/java/com/luoo/music/service/S3Service.java
new file mode 100644
index 0000000..a7a83a6
--- /dev/null
+++ b/luoo_music/src/main/java/com/luoo/music/service/S3Service.java
@@ -0,0 +1,103 @@
+package com.luoo.music.service;
+
+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.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+
+
+@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;
+ }
+
+
+
+ public int copy() {
+
+ String bucket = "indie"; //存储桶名
+ String sourceKey = "20240121/1.mp3"; //copy的源文件路径
+ String destinationKey = "20240121/2.mp3"; // copy的目的地路径
+ CopyObjectResponse copyObjectResponse = s3Client.copyObject(CopyObjectRequest.builder().sourceBucket(bucket).sourceKey(sourceKey).destinationBucket(bucket).destinationKey(destinationKey).build());
+ SdkHttpResponse sdkHttpResponse = copyObjectResponse.sdkHttpResponse();
+ if(!sdkHttpResponse.isSuccessful()){
+ return -1;
+ }
+ return 1;
+ }
+
+ public void uploadAvatar(String bucket, String key, byte[] buffer) {
+ PutObjectRequest putObjectRequest = PutObjectRequest.builder()
+ .bucket(bucket)
+ .key(key)
+ .build();
+ RequestBody requestBody = RequestBody.fromInputStream(new ByteArrayInputStream(buffer), buffer.length);
+ s3Client.putObject(putObjectRequest, requestBody);
+ }
+
+}