parent
45e78101a2
commit
602856682d
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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<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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue