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