update 优化 OSS私有桶的临时URL获取方法

This commit is contained in:
疯狂的狮子Li
2025-01-17 17:10:03 +08:00
parent 20614184ec
commit 0f197c1934
2 changed files with 6 additions and 6 deletions

View File

@@ -21,7 +21,6 @@ import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3Configuration; import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.crt.S3CrtHttpConfiguration; import software.amazon.awssdk.services.s3.crt.S3CrtHttpConfiguration;
import software.amazon.awssdk.services.s3.model.GetObjectResponse; import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import software.amazon.awssdk.services.s3.presigner.S3Presigner; import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.transfer.s3.S3TransferManager; import software.amazon.awssdk.transfer.s3.S3TransferManager;
import software.amazon.awssdk.transfer.s3.model.*; import software.amazon.awssdk.transfer.s3.model.*;
@@ -285,13 +284,13 @@ public class OssClient {
/** /**
* 获取私有URL链接 * 获取私有URL链接
* *
* @param objectKey 对象KEY * @param objectKey 对象KEY
* @param second 授权时间 * @param expiredTime 链接授权到期时间
*/ */
public String getPrivateUrl(String objectKey, Integer second) { public String getPrivateUrl(String objectKey, Duration expiredTime) {
// 使用 AWS S3 预签名 URL 的生成器 获取对象的预签名 URL // 使用 AWS S3 预签名 URL 的生成器 获取对象的预签名 URL
URL url = presigner.presignGetObject( URL url = presigner.presignGetObject(
x -> x.signatureDuration(Duration.ofSeconds(second)) x -> x.signatureDuration(expiredTime)
.getObjectRequest( .getObjectRequest(
y -> y.bucket(properties.getBucketName()) y -> y.bucket(properties.getBucketName())
.key(objectKey) .key(objectKey)

View File

@@ -32,6 +32,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@@ -255,7 +256,7 @@ public class SysOssServiceImpl implements ISysOssService {
OssClient storage = OssFactory.instance(oss.getService()); OssClient storage = OssFactory.instance(oss.getService());
// 仅修改桶类型为 private 的URL临时URL时长为120s // 仅修改桶类型为 private 的URL临时URL时长为120s
if (AccessPolicyType.PRIVATE == storage.getAccessPolicy()) { if (AccessPolicyType.PRIVATE == storage.getAccessPolicy()) {
oss.setUrl(storage.getPrivateUrl(oss.getFileName(), 120)); oss.setUrl(storage.getPrivateUrl(oss.getFileName(), Duration.ofSeconds(120)));
} }
return oss; return oss;
} }