S3 Util 新增工具类
This commit is contained in:
@@ -2,6 +2,7 @@ package com.ai.da.common.utils;
|
|||||||
|
|
||||||
import com.ai.da.common.config.exception.BusinessException;
|
import com.ai.da.common.config.exception.BusinessException;
|
||||||
import com.ai.da.common.constant.CommonConstant;
|
import com.ai.da.common.constant.CommonConstant;
|
||||||
|
import io.netty.util.internal.StringUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -57,8 +58,8 @@ public class S3Util {
|
|||||||
private static S3Presigner s3Presigner;
|
private static S3Presigner s3Presigner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 获取S3客户端对象
|
|
||||||
* @return software.amazon.awssdk.services.s3.S3Client
|
* @return software.amazon.awssdk.services.s3.S3Client
|
||||||
|
* @description: 获取S3客户端对象
|
||||||
*/
|
*/
|
||||||
public synchronized S3Client getS3Client() {
|
public synchronized S3Client getS3Client() {
|
||||||
if (null == s3Client) {
|
if (null == s3Client) {
|
||||||
@@ -74,8 +75,8 @@ public class S3Util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 获取预签名对象
|
|
||||||
* @return software.amazon.awssdk.services.s3.presigner.S3Presigner
|
* @return software.amazon.awssdk.services.s3.presigner.S3Presigner
|
||||||
|
* @description: 获取预签名对象
|
||||||
*/
|
*/
|
||||||
public synchronized S3Presigner getS3PreSigner() {
|
public synchronized S3Presigner getS3PreSigner() {
|
||||||
if (null == s3Presigner) {
|
if (null == s3Presigner) {
|
||||||
@@ -92,6 +93,29 @@ public class S3Util {
|
|||||||
return s3Presigner;
|
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) {
|
public String upload(String bucketName, String path, MultipartFile file) {
|
||||||
S3Client s3Client = getS3Client();
|
S3Client s3Client = getS3Client();
|
||||||
try {
|
try {
|
||||||
@@ -113,11 +137,104 @@ public class S3Util {
|
|||||||
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
|
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
|
||||||
|
|
||||||
log.info("上传的位置:桶 - {},路径 - {}", bucketName, fileName);
|
log.info("上传的位置:桶 - {},路径 - {}", bucketName, fileName);
|
||||||
return fileName;
|
return bucketName + "/" + fileName;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("上传文件到S3失败 异常:{}", e.getMessage());
|
log.error("上传文件到S3失败 异常:{}", e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// todo
|
||||||
|
public String upload(String bucketName, String path, MultipartFile file, String copy) {
|
||||||
|
S3Client s3Client = getS3Client();
|
||||||
|
InputStream in = null;
|
||||||
|
try {
|
||||||
|
in = file.getInputStream();
|
||||||
|
|
||||||
|
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) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (in != null) {
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bucketName + "/" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputStream download(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);
|
||||||
|
return download(bucketName, objectName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputStream download(String bucketName, String objectName) {
|
||||||
|
try {
|
||||||
|
S3Client s3Client = getS3Client();
|
||||||
|
GetObjectRequest objectRequest = GetObjectRequest
|
||||||
|
.builder()
|
||||||
|
.key(objectName)
|
||||||
|
.bucket(bucketName)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
|
||||||
|
ResponseBytes<GetObjectResponse> objectAsBytes = s3Client.getObjectAsBytes(objectRequest);
|
||||||
|
byte[] data = objectAsBytes.asByteArray();
|
||||||
|
return new ByteArrayInputStream(data);
|
||||||
|
|
||||||
|
/*// Write the data to a local file.
|
||||||
|
File myFile = new File("files/images.png");
|
||||||
|
OutputStream os = new FileOutputStream(myFile);
|
||||||
|
os.write(data);
|
||||||
|
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("");
|
||||||
|
throw new BusinessException("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPreSignedUrl(String path, int expiry) {
|
public String getPreSignedUrl(String path, int expiry) {
|
||||||
@@ -137,20 +254,20 @@ public class S3Util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 生成预签名URL
|
* @param keyName key名称: test/2022/06/123.pdf
|
||||||
* @param keyName key名称: test/2022/06/123.pdf
|
|
||||||
* @param signatureDurationTime 有效期 单位:秒
|
* @param signatureDurationTime 有效期 单位:秒
|
||||||
* @return java.lang.String
|
* @return java.lang.String
|
||||||
|
* @description: 生成预签名URL
|
||||||
*/
|
*/
|
||||||
public String getPreSignatureUrl(String bucket, String keyName, Integer signatureDurationTime) {
|
public String getPreSignatureUrl(String bucket, String keyName, Integer signatureDurationTime) {
|
||||||
String preSignatureUrl = "";
|
String preSignatureUrl = "";
|
||||||
try {
|
try {
|
||||||
S3Presigner s3PreSigner = getS3PreSigner();
|
S3Presigner s3PreSigner = getS3PreSigner();
|
||||||
GetObjectRequest getObjectRequest =
|
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
|
||||||
GetObjectRequest.builder()
|
.bucket(bucket)
|
||||||
.bucket(bucket)
|
.key(keyName)
|
||||||
.key(keyName)
|
.build();
|
||||||
.build();
|
|
||||||
//设置预签名URL可访问时间
|
//设置预签名URL可访问时间
|
||||||
signatureDurationTime = Optional.ofNullable(signatureDurationTime)
|
signatureDurationTime = Optional.ofNullable(signatureDurationTime)
|
||||||
.map(item -> {
|
.map(item -> {
|
||||||
@@ -176,46 +293,56 @@ public class S3Util {
|
|||||||
return preSignatureUrl;
|
return preSignatureUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream download(String path) {
|
public boolean doesObjectExist(String bucketName, String objectName) {
|
||||||
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);
|
|
||||||
return download(bucketName, objectName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public InputStream download(String bucketName, String objectName){
|
|
||||||
try {
|
try {
|
||||||
S3Client s3Client = getS3Client();
|
S3Client s3Client = getS3Client();
|
||||||
GetObjectRequest objectRequest = GetObjectRequest
|
HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
|
||||||
.builder()
|
|
||||||
.key(objectName)
|
|
||||||
.bucket(bucketName)
|
.bucket(bucketName)
|
||||||
|
.key(objectName)
|
||||||
.build();
|
.build();
|
||||||
|
HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest);
|
||||||
// ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
|
return true;
|
||||||
ResponseBytes<GetObjectResponse> objectAsBytes = s3Client.getObjectAsBytes(objectRequest);
|
} catch (Exception e) {
|
||||||
byte[] data = objectAsBytes.asByteArray();
|
log.info("指定文件 {}/{} 不存在", bucketName, objectName);
|
||||||
return new ByteArrayInputStream(data);
|
// 如果发生异常,说明文件不存在或者出现了其他错误
|
||||||
|
return false;
|
||||||
/*// Write the data to a local file.
|
|
||||||
File myFile = new File("files/images.png");
|
|
||||||
OutputStream os = new FileOutputStream(myFile);
|
|
||||||
os.write(data);
|
|
||||||
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("");
|
|
||||||
throw new BusinessException("");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> listAllBucket(){
|
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();
|
S3Client s3Client = getS3Client();
|
||||||
ListBucketsResponse listBucketsResponse = s3Client.listBuckets();
|
ListBucketsResponse listBucketsResponse = s3Client.listBuckets();
|
||||||
List<Bucket> buckets = listBucketsResponse.buckets();
|
List<Bucket> buckets = listBucketsResponse.buckets();
|
||||||
|
|||||||
Reference in New Issue
Block a user