|
|
|
@ -12,10 +12,7 @@ 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.CopyObjectRequest;
|
|
|
|
|
import software.amazon.awssdk.services.s3.model.CopyObjectResponse;
|
|
|
|
|
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
|
|
|
|
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
|
|
|
|
|
import software.amazon.awssdk.services.s3.model.*;
|
|
|
|
|
import util.IdWorker;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
@ -56,12 +53,14 @@ public class S3Service {
|
|
|
|
|
item.setImage(destImageSuffix);
|
|
|
|
|
copy(Constants.BUCKET, srcImage, destImage);
|
|
|
|
|
}
|
|
|
|
|
/*if (StringUtils.isNotBlank(tempLyric)) {
|
|
|
|
|
if (StringUtils.isNotBlank(tempLyric)) {
|
|
|
|
|
String srcLyric = tempLyric.substring(0, tempLyric.lastIndexOf(Constants.DOT)) + ".lyric";
|
|
|
|
|
if (checkFileExist(Constants.BUCKET, srcLyric)) {
|
|
|
|
|
String destLyricSuffix = format + srcLyric.substring(srcLyric.lastIndexOf(Constants.DOT));
|
|
|
|
|
String destLyric = Constants.MUSIC_KEY_PREFIX + destLyricSuffix;
|
|
|
|
|
copy(Constants.BUCKET, srcLyric, destLyric);
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -93,6 +92,27 @@ public class S3Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检测一个文件是否在s3上
|
|
|
|
|
* @param bucket
|
|
|
|
|
* @param key
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public boolean checkFileExist(String bucket, String key) {
|
|
|
|
|
try {
|
|
|
|
|
HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
|
|
|
|
|
.bucket(bucket)
|
|
|
|
|
.key(key)
|
|
|
|
|
.build();
|
|
|
|
|
s3Client.headObject(headObjectRequest);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
s3Client.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 歌词保存至s3
|
|
|
|
|
* @param bucket
|
|
|
|
@ -114,6 +134,8 @@ public class S3Service {
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return -1;
|
|
|
|
|
} finally {
|
|
|
|
|
s3Client.close();
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
@ -137,11 +159,14 @@ public class S3Service {
|
|
|
|
|
SdkHttpResponse sdkHttpResponse = putObjectResponse.sdkHttpResponse();
|
|
|
|
|
if (!sdkHttpResponse.isSuccessful()) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return -1;
|
|
|
|
|
} finally {
|
|
|
|
|
s3Client.close();
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -152,15 +177,22 @@ public class S3Service {
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public int copy(String bucket, String srcKey, String destKey) {
|
|
|
|
|
try {
|
|
|
|
|
CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder()
|
|
|
|
|
.sourceBucket(bucket).sourceKey(srcKey).destinationBucket(bucket).destinationKey(destKey).build();
|
|
|
|
|
CopyObjectResponse copyObjectResponse = s3Client.copyObject(copyObjectRequest);
|
|
|
|
|
SdkHttpResponse sdkHttpResponse = copyObjectResponse.sdkHttpResponse();
|
|
|
|
|
if(!sdkHttpResponse.isSuccessful()){
|
|
|
|
|
if (!sdkHttpResponse.isSuccessful()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
} catch(Exception e) {
|
|
|
|
|
return -1;
|
|
|
|
|
} finally {
|
|
|
|
|
s3Client.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void uploadAvatar(String bucket, String key, byte[] buffer) {
|
|
|
|
|
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
|
|
|
|
|