S3 Util 新增工具类

This commit is contained in:
2024-06-26 18:00:47 +08:00
parent 0759452381
commit a5b435b80f

View File

@@ -2,6 +2,7 @@ package com.ai.da.common.utils;
import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.constant.CommonConstant;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -57,8 +58,8 @@ public class S3Util {
private static S3Presigner s3Presigner;
/**
* @description: 获取S3客户端对象
* @return software.amazon.awssdk.services.s3.S3Client
* @description: 获取S3客户端对象
*/
public synchronized S3Client getS3Client() {
if (null == s3Client) {
@@ -74,8 +75,8 @@ public class S3Util {
}
/**
* @description: 获取预签名对象
* @return software.amazon.awssdk.services.s3.presigner.S3Presigner
* @description: 获取预签名对象
*/
public synchronized S3Presigner getS3PreSigner() {
if (null == s3Presigner) {
@@ -92,6 +93,29 @@ public class S3Util {
return s3Presigner;
}
public String uploadImageFromBase64(String bucketName, String base64Image, String imageType) {
S3Client s3Client = getS3Client();
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
String fileName = UUID.randomUUID() + "." + imageType; // or any other image format
try (InputStream in = new ByteArrayInputStream(imageBytes)) {
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.contentType("image/png")
.contentLength((long) in.available())
.key(fileName)
// .acl(ObjectCannedACL.PUBLIC_READ)
.build();
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(in, in.available()));
log.info("上传的位置:桶 - {},路径 - {}", bucketName, fileName);
return bucketName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
return null; // or throw an exception
}
}
public String upload(String bucketName, String path, MultipartFile file) {
S3Client s3Client = getS3Client();
try {
@@ -113,67 +137,40 @@ public class S3Util {
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
log.info("上传的位置:桶 - {},路径 - {}", bucketName, fileName);
return fileName;
return bucketName + "/" + fileName;
} catch (Exception e) {
log.error("上传文件到S3失败 异常:{}", e.getMessage());
}
return null;
}
public String getPreSignedUrl(String path, int expiry) {
if (LocalCacheUtils.getPresignedUrlCache(path) != null) {
return LocalCacheUtils.getPresignedUrlCache(path);
} else {
if (!path.contains("/")) {
throw new BusinessException("The path is error!");
}
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String fileName = path.substring(index + 1);
String preSignedUrl = getPreSignatureUrl(bucketName, fileName, expiry);
LocalCacheUtils.setPresignedUrlCache(path, preSignedUrl);
return preSignedUrl;
}
}
/**
* @description: 生成预签名URL
* @param keyName key名称: test/2022/06/123.pdf
* @param signatureDurationTime 有效期 单位:秒
* @return java.lang.String
*/
public String getPreSignatureUrl(String bucket, String keyName, Integer signatureDurationTime) {
String preSignatureUrl = "";
// todo
public String upload(String bucketName, String path, MultipartFile file, String copy) {
S3Client s3Client = getS3Client();
InputStream in = null;
try {
S3Presigner s3PreSigner = getS3PreSigner();
GetObjectRequest getObjectRequest =
GetObjectRequest.builder()
.bucket(bucket)
.key(keyName)
.build();
//设置预签名URL可访问时间
signatureDurationTime = Optional.ofNullable(signatureDurationTime)
.map(item -> {
if (item.intValue() > CommonConstant.Numbers.NUMBER_10080) {
item = CommonConstant.Numbers.NUMBER_10080;
}
return item;
})
.orElse(CommonConstant.Numbers.NUMBER_10);
GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(signatureDurationTime))
.getObjectRequest(getObjectRequest)
.build();
in = file.getInputStream();
PresignedGetObjectRequest presignedGetObjectRequest =
s3PreSigner.presignGetObject(getObjectPresignRequest);
preSignatureUrl = String.valueOf(presignedGetObjectRequest.url());
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.contentType(file.getContentType())
.contentLength(file.getSize())
.key(path)
// .acl(ObjectCannedACL.PUBLIC_READ)
.build();
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
} catch (Exception e) {
log.error("生成预签名URL失败异常{}", e.getMessage());
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return preSignatureUrl;
}
}
return bucketName + "/" + path;
}
public InputStream download(String path) {
@@ -207,7 +204,6 @@ public class S3Util {
System.out.println("Successfully obtained bytes from an S3 object");
os.close();
return null;*/
// return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
log.error("");
@@ -215,6 +211,137 @@ public class S3Util {
}
}
public void deleteObject(String path) {
if (!path.contains("/")) {
throw new BusinessException("The path is error!");
}
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String objectName = path.substring(index + 1);
deleteObject(bucketName, objectName);
}
public void deleteObject(String bucketName, String objectName) {
try {
S3Client s3Client = getS3Client();
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest
.builder()
.key(objectName)
.bucket(bucketName)
.build();
s3Client.deleteObject(deleteObjectRequest);
log.info("Object " + objectName + " successfully removed from bucket " + bucketName);
} catch (Exception e) {
e.printStackTrace();
log.info("Error while removing object " + objectName + " from bucket " + bucketName + ": " + e.getMessage());
}
}
public String getPreSignedUrl(String path, int expiry) {
if (LocalCacheUtils.getPresignedUrlCache(path) != null) {
return LocalCacheUtils.getPresignedUrlCache(path);
} else {
if (!path.contains("/")) {
throw new BusinessException("The path is error!");
}
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String fileName = path.substring(index + 1);
String preSignedUrl = getPreSignatureUrl(bucketName, fileName, expiry);
LocalCacheUtils.setPresignedUrlCache(path, preSignedUrl);
return preSignedUrl;
}
}
/**
* @param keyName key名称: test/2022/06/123.pdf
* @param signatureDurationTime 有效期 单位:秒
* @return java.lang.String
* @description: 生成预签名URL
*/
public String getPreSignatureUrl(String bucket, String keyName, Integer signatureDurationTime) {
String preSignatureUrl = "";
try {
S3Presigner s3PreSigner = getS3PreSigner();
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucket)
.key(keyName)
.build();
//设置预签名URL可访问时间
signatureDurationTime = Optional.ofNullable(signatureDurationTime)
.map(item -> {
if (item.intValue() > CommonConstant.Numbers.NUMBER_10080) {
item = CommonConstant.Numbers.NUMBER_10080;
}
return item;
})
.orElse(CommonConstant.Numbers.NUMBER_10);
GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(signatureDurationTime))
.getObjectRequest(getObjectRequest)
.build();
PresignedGetObjectRequest presignedGetObjectRequest =
s3PreSigner.presignGetObject(getObjectPresignRequest);
preSignatureUrl = String.valueOf(presignedGetObjectRequest.url());
} catch (Exception e) {
log.error("生成预签名URL失败异常{}", e.getMessage());
}
return preSignatureUrl;
}
public boolean doesObjectExist(String bucketName, String objectName) {
try {
S3Client s3Client = getS3Client();
HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
.bucket(bucketName)
.key(objectName)
.build();
HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest);
return true;
} catch (Exception e) {
log.info("指定文件 {}/{} 不存在", bucketName, objectName);
// 如果发生异常,说明文件不存在或者出现了其他错误
return false;
}
}
public String base64UploadToPath(String base64, String bucketName, String path){
S3Client s3Client = getS3Client();
String[] parts = base64.split(",");
String imageType = parts[0].split("/")[1].split(";")[0];
String base64Data = parts[1];
byte[] imageBytes = Base64.getDecoder().decode(base64Data);
String fileName;
if (!StringUtil.isNullOrEmpty(path)){
fileName = path + "." + imageType; // or any other image format
}else {
fileName = UUID.randomUUID() + "." + imageType;
}
try (InputStream in = new ByteArrayInputStream(imageBytes)) {
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.contentType("image/" + imageType)
.contentLength((long) in.available())
.key(fileName)
// .acl(ObjectCannedACL.PUBLIC_READ)
.build();
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(in, in.available()));
log.info("上传的位置:桶 - {},路径 - {}", bucketName, fileName);
return bucketName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
return null; // or throw an exception
}
}
public List<String> listAllBucket() {
S3Client s3Client = getS3Client();
ListBucketsResponse listBucketsResponse = s3Client.listBuckets();