S3Util 工具类修改

This commit is contained in:
2024-07-03 16:43:22 +08:00
parent a5b435b80f
commit d2129459fe
20 changed files with 131 additions and 126 deletions

View File

@@ -17,7 +17,7 @@ public enum CreditsEventsEnum {
INIT_YEARLY("init_yearly", "6000"),
INIT_MONTHLY("init_monthly", "5000"),
INIT_TRIAL("init_trial", "100"),
INIT_WEEKLY("init_weekly","10000"),
INIT_WEEKLY("init_weekly","6000"),
// SUPER_RESOLUTION("Super Resolution","30"),
SUPER_RESOLUTION("Super Resolution","10"),

View File

@@ -1,20 +1,24 @@
package com.ai.da.common.task;
import com.ai.da.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@Slf4j
public class AccountTask {
@Resource
private AccountService accountService;
/** 每周日晚上刷新 年付用户、月付用户的积分 */
// @Scheduled(cron = "59 59 23 ? * SUN")
@Scheduled(cron = "59 59 23 ? * SUN")
// @Scheduled(cron = "59 59 23 * * ?")
public void refreshCreditsMonthly(){
log.info("每周日晚115959刷新付费用户积分为 6000");
accountService.refreshCreditsWeekly();
}

View File

@@ -373,7 +373,7 @@ public class MinioUtil {
* @param expiry 过期时间(单位:分)
* @return 文件的临时URL如果出现异常则返回null
*/
public String getPresignedUrl(String bucketName, String fileName, int expiry) {
public String getPreSignedUrl(String bucketName, String fileName, int expiry) {
try {
return minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
@@ -389,7 +389,7 @@ public class MinioUtil {
}
}
public String getPresignedUrl(String path, int expiry) {
public String getPreSignedUrl(String path, int expiry) {
if (LocalCacheUtils.getPresignedUrlCache(path) != null) {
return LocalCacheUtils.getPresignedUrlCache(path);
} else {
@@ -399,7 +399,7 @@ public class MinioUtil {
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String fileName = path.substring(index + 1);
String presignedUrl = getPresignedUrl(bucketName, fileName, expiry);
String presignedUrl = getPreSignedUrl(bucketName, fileName, expiry);
LocalCacheUtils.setPresignedUrlCache(path, presignedUrl);
return presignedUrl;
}
@@ -422,7 +422,7 @@ public class MinioUtil {
fileName.append("/");
}
}
return getPresignedUrl(bucketName, String.valueOf(fileName), expiry);
return getPreSignedUrl(bucketName, String.valueOf(fileName), expiry);
}
public boolean doesObjectExist(String bucketName, String objectName) {

View File

@@ -104,11 +104,11 @@ public class S3Util {
.contentType("image/png")
.contentLength((long) in.available())
.key(fileName)
// .acl(ObjectCannedACL.PUBLIC_READ)
.acl(ObjectCannedACL.PUBLIC_READ)
.build();
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(in, in.available()));
log.info("上传的位置:桶 - {},路径 - {}", bucketName, fileName);
log.info("uploadImageFromBase64 上传的位置:桶 - {},路径 - {}", bucketName, fileName);
return bucketName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
@@ -132,18 +132,19 @@ public class S3Util {
.contentType(file.getContentType())
.contentLength(file.getSize())
.key(fileName)
// .acl(ObjectCannedACL.PUBLIC_READ)
.acl(ObjectCannedACL.PUBLIC_READ)
.build();
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
log.info("上传的位置:桶 - {},路径 - {}", bucketName, fileName);
log.info("upload 上传的位置:桶 - {},路径 - {}", bucketName, fileName);
return bucketName + "/" + fileName;
} catch (Exception e) {
log.error("上传文件到S3失败 异常:{}", e.getMessage());
return null;
}
}
// todo
// todo
public String upload(String bucketName, String path, MultipartFile file, String copy) {
S3Client s3Client = getS3Client();
InputStream in = null;
@@ -155,10 +156,10 @@ public class S3Util {
.contentType(file.getContentType())
.contentLength(file.getSize())
.key(path)
// .acl(ObjectCannedACL.PUBLIC_READ)
.acl(ObjectCannedACL.PUBLIC_READ)
.build();
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
log.info("upload-copy 上传的位置:桶 - {},路径 - {}", bucketName, path);
} catch (Exception e) {
e.printStackTrace();
} finally {
@@ -195,6 +196,7 @@ public class S3Util {
// ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
ResponseBytes<GetObjectResponse> objectAsBytes = s3Client.getObjectAsBytes(objectRequest);
byte[] data = objectAsBytes.asByteArray();
log.info("download 下载图片位置:桶 - {},路径 - {}", bucketName, objectName);
return new ByteArrayInputStream(data);
/*// Write the data to a local file.
@@ -263,6 +265,9 @@ public class S3Util {
String preSignatureUrl = "";
try {
S3Presigner s3PreSigner = getS3PreSigner();
S3Client s3Client = getS3Client();
setObjectAcl(s3Client, bucket, keyName, ObjectCannedACL.PUBLIC_READ);
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucket)
.key(keyName)
@@ -286,13 +291,20 @@ public class S3Util {
s3PreSigner.presignGetObject(getObjectPresignRequest);
preSignatureUrl = String.valueOf(presignedGetObjectRequest.url());
} catch (Exception e) {
log.error("生成预签名URL失败异常{}", e.getMessage());
}
return preSignatureUrl;
}
public static void setObjectAcl(S3Client s3, String bucketName, String keyName, ObjectCannedACL acl) {
PutObjectAclRequest aclRequest = PutObjectAclRequest.builder()
.bucket(bucketName)
.key(keyName)
.acl(acl)
.build();
s3.putObjectAcl(aclRequest);
}
public boolean doesObjectExist(String bucketName, String objectName) {
try {
S3Client s3Client = getS3Client();
@@ -309,7 +321,7 @@ public class S3Util {
}
}
public String base64UploadToPath(String base64, String bucketName, String path){
public String base64UploadToPath(String base64, String bucketName, String path) {
S3Client s3Client = getS3Client();
String[] parts = base64.split(",");
String imageType = parts[0].split("/")[1].split(";")[0];
@@ -317,9 +329,9 @@ public class S3Util {
byte[] imageBytes = Base64.getDecoder().decode(base64Data);
String fileName;
if (!StringUtil.isNullOrEmpty(path)){
if (!StringUtil.isNullOrEmpty(path)) {
fileName = path + "." + imageType; // or any other image format
}else {
} else {
fileName = UUID.randomUUID() + "." + imageType;
}
@@ -329,11 +341,11 @@ public class S3Util {
.contentType("image/" + imageType)
.contentLength((long) in.available())
.key(fileName)
// .acl(ObjectCannedACL.PUBLIC_READ)
.acl(ObjectCannedACL.PUBLIC_READ)
.build();
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(in, in.available()));
log.info("上传的位置:桶 - {},路径 - {}", bucketName, fileName);
log.info("base64UploadToPath 上传的位置:桶 - {},路径 - {}", bucketName, fileName);
return bucketName + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();