update 优化oss日志侦听器打印级别

This commit is contained in:
AprilWind
2026-01-05 14:39:40 +08:00
parent 8b97e7bc53
commit d634c2a292

View File

@@ -141,7 +141,8 @@ public class OssClient {
try { try {
// 构建上传请求对象 // 构建上传请求对象
FileUpload fileUpload = transferManager.uploadFile( FileUpload fileUpload = transferManager.uploadFile(
x -> x.putObjectRequest( x -> {
x.source(filePath).putObjectRequest(
y -> y.bucket(properties.getBucketName()) y -> y.bucket(properties.getBucketName())
.key(key) .key(key)
.contentMD5(StringUtils.isNotEmpty(md5Digest) ? md5Digest : null) .contentMD5(StringUtils.isNotEmpty(md5Digest) ? md5Digest : null)
@@ -149,10 +150,13 @@ public class OssClient {
// 用于设置对象的访问控制列表ACL。不同云厂商对ACL的支持和实现方式有所不同 // 用于设置对象的访问控制列表ACL。不同云厂商对ACL的支持和实现方式有所不同
// 因此根据具体的云服务提供商你可能需要进行不同的配置自行开启阿里云有acl权限配置腾讯云没有acl权限配置 // 因此根据具体的云服务提供商你可能需要进行不同的配置自行开启阿里云有acl权限配置腾讯云没有acl权限配置
//.acl(getAccessPolicy().getObjectCannedACL()) //.acl(getAccessPolicy().getObjectCannedACL())
.build()) .build()
.addTransferListener(LoggingTransferListener.create()) );
.source(filePath).build()); if (log.isDebugEnabled()) {
x.addTransferListener(LoggingTransferListener.create());
}
}
);
// 等待上传完成并获取上传结果 // 等待上传完成并获取上传结果
CompletedFileUpload uploadResult = fileUpload.completionFuture().join(); CompletedFileUpload uploadResult = fileUpload.completionFuture().join();
String eTag = uploadResult.response().eTag(); String eTag = uploadResult.response().eTag();
@@ -192,16 +196,21 @@ public class OssClient {
// 使用 transferManager 进行上传 // 使用 transferManager 进行上传
Upload upload = transferManager.upload( Upload upload = transferManager.upload(
x -> x.requestBody(body).addTransferListener(LoggingTransferListener.create()) x -> {
.putObjectRequest( x.requestBody(body).putObjectRequest(
y -> y.bucket(properties.getBucketName()) y -> y.bucket(properties.getBucketName())
.key(key) .key(key)
.contentType(contentType) .contentType(contentType)
// 用于设置对象的访问控制列表ACL。不同云厂商对ACL的支持和实现方式有所不同 // 用于设置对象的访问控制列表ACL。不同云厂商对ACL的支持和实现方式有所不同
// 因此根据具体的云服务提供商你可能需要进行不同的配置自行开启阿里云有acl权限配置腾讯云没有acl权限配置 // 因此根据具体的云服务提供商你可能需要进行不同的配置自行开启阿里云有acl权限配置腾讯云没有acl权限配置
//.acl(getAccessPolicy().getObjectCannedACL()) //.acl(getAccessPolicy().getObjectCannedACL())
.build()) .build()
.build()); );
if (log.isDebugEnabled()) {
x.addTransferListener(LoggingTransferListener.create());
}
}
);
// 将输入流写入请求体 // 将输入流写入请求体
body.writeInputStream(inputStream); body.writeInputStream(inputStream);
@@ -229,13 +238,17 @@ public class OssClient {
Path tempFilePath = FileUtils.createTempFile().toPath(); Path tempFilePath = FileUtils.createTempFile().toPath();
// 使用 S3TransferManager 下载文件 // 使用 S3TransferManager 下载文件
FileDownload downloadFile = transferManager.downloadFile( FileDownload downloadFile = transferManager.downloadFile(
x -> x.getObjectRequest( x -> {
x.destination(tempFilePath).getObjectRequest(
y -> y.bucket(properties.getBucketName()) y -> y.bucket(properties.getBucketName())
.key(removeBaseUrl(path)) .key(removeBaseUrl(path))
.build()) .build()
.addTransferListener(LoggingTransferListener.create()) );
.destination(tempFilePath) if (log.isDebugEnabled()) {
.build()); x.addTransferListener(LoggingTransferListener.create());
}
}
);
// 等待文件下载操作完成 // 等待文件下载操作完成
downloadFile.completionFuture().join(); downloadFile.completionFuture().join();
return tempFilePath; return tempFilePath;
@@ -244,8 +257,8 @@ public class OssClient {
/** /**
* 下载文件从 Amazon S3 到 输出流 * 下载文件从 Amazon S3 到 输出流
* *
* @param key 文件在 Amazon S3 中的对象键 * @param key 文件在 Amazon S3 中的对象键
* @param out 输出流 * @param out 输出流
* @param consumer 自定义处理逻辑 * @param consumer 自定义处理逻辑
* @throws OssException 如果下载失败,抛出自定义异常 * @throws OssException 如果下载失败,抛出自定义异常
*/ */
@@ -260,26 +273,24 @@ public class OssClient {
/** /**
* 下载文件从 Amazon S3 到 输出流 * 下载文件从 Amazon S3 到 输出流
* *
* @param key 文件在 Amazon S3 中的对象键 * @param key 文件在 Amazon S3 中的对象键
* @param contentLengthConsumer 文件大小消费者函数 * @param contentLengthConsumer 文件大小消费者函数
* @return 写出订阅器 * @return 写出订阅器
* @throws OssException 如果下载失败,抛出自定义异常 * @throws OssException 如果下载失败,抛出自定义异常
*/ */
public WriteOutSubscriber<OutputStream> download(String key, Consumer<Long> contentLengthConsumer) { public WriteOutSubscriber<OutputStream> download(String key, Consumer<Long> contentLengthConsumer) {
try { try {
// 构建下载请求 DownloadRequest.TypedBuilder<ResponsePublisher<GetObjectResponse>> typedBuilder = DownloadRequest.builder()
DownloadRequest<ResponsePublisher<GetObjectResponse>> publisherDownloadRequest = DownloadRequest.builder()
// 文件对象
.getObjectRequest(y -> y.bucket(properties.getBucketName())
.key(key)
.build())
.addTransferListener(LoggingTransferListener.create())
// 使用发布订阅转换器 // 使用发布订阅转换器
.responseTransformer(AsyncResponseTransformer.toPublisher()) .responseTransformer(AsyncResponseTransformer.toPublisher())
.build(); // 文件对象
.getObjectRequest(y -> y.bucket(properties.getBucketName()).key(key).build());
if (log.isDebugEnabled()) {
typedBuilder.addTransferListener(LoggingTransferListener.create());
}
// 使用 S3TransferManager 下载文件 // 使用 S3TransferManager 下载文件
Download<ResponsePublisher<GetObjectResponse>> publisherDownload = transferManager.download(publisherDownloadRequest); Download<ResponsePublisher<GetObjectResponse>> publisherDownload = transferManager.download(typedBuilder.build());
// 获取下载发布订阅转换器 // 获取下载发布订阅转换器
ResponsePublisher<GetObjectResponse> publisher = publisherDownload.completionFuture().join().result(); ResponsePublisher<GetObjectResponse> publisher = publisherDownload.completionFuture().join().result();
// 执行文件大小消费者函数 // 执行文件大小消费者函数
@@ -289,7 +300,7 @@ public class OssClient {
// 构建写出订阅器对象 // 构建写出订阅器对象
return out -> { return out -> {
// 创建可写入的字节通道 // 创建可写入的字节通道
try(WritableByteChannel channel = Channels.newChannel(out)){ try (WritableByteChannel channel = Channels.newChannel(out)) {
// 订阅数据 // 订阅数据
publisher.subscribe(byteBuffer -> { publisher.subscribe(byteBuffer -> {
while (byteBuffer.hasRemaining()) { while (byteBuffer.hasRemaining()) {
@@ -347,7 +358,7 @@ public class OssClient {
* *
* @param objectKey 对象KEY * @param objectKey 对象KEY
* @param expiredTime 链接授权到期时间 * @param expiredTime 链接授权到期时间
* @param metadata 元数据 * @param metadata 元数据
*/ */
public String createPresignedPutUrl(String objectKey, Duration expiredTime, Map<String, String> metadata) { public String createPresignedPutUrl(String objectKey, Duration expiredTime, Map<String, String> metadata) {
// 使用 AWS S3 预签名 URL 的生成器 获取上传文件对象的预签名 URL // 使用 AWS S3 预签名 URL 的生成器 获取上传文件对象的预签名 URL