| | |
| | | import software.amazon.awssdk.services.s3.S3Configuration; |
| | | import software.amazon.awssdk.services.s3.crt.S3CrtHttpConfiguration; |
| | | 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.transfer.s3.S3TransferManager; |
| | | import software.amazon.awssdk.transfer.s3.model.*; |
| | |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.time.Duration; |
| | | import java.util.function.Consumer; |
| | | |
| | | /** |
| | | * S3 存储协议 所有兼容S3协议的云厂商均支持 |
| | |
| | | .serviceConfiguration(config) |
| | | .build(); |
| | | |
| | | checkBucket(); |
| | | } catch (Exception e) { |
| | | if (e instanceof OssException) { |
| | | throw e; |
| | | } |
| | | throw new OssException("配置错误! 请检查系统配置:[" + e.getMessage() + "]"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查桶是否存在 |
| | | * |
| | | * @throws OssException 当创建存储桶时发生异常时抛出 |
| | | */ |
| | | public void checkBucket() { |
| | | String bucketName = properties.getBucketName(); |
| | | try { |
| | | // 尝试获取存储桶的信息 |
| | | client.headBucket(x -> x.bucket(bucketName).build()).join(); |
| | | } catch (Exception ex) { |
| | | if (ex.getCause() instanceof NoSuchBucketException) { |
| | | throw new OssException("Bucket桶是不存在的,请核对配置信息:[" + ex.getMessage() + "]"); |
| | | } else { |
| | | throw new OssException("判断Bucket是否存在失败,请核对配置信息:[" + ex.getMessage() + "]"); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | * |
| | | * @param key 文件在 Amazon S3 中的对象键 |
| | | * @param out 输出流 |
| | | * @param consumer 自定义处理逻辑 |
| | | * @return 输出流中写入的字节数(长度) |
| | | * @throws OssException 如果下载失败,抛出自定义异常 |
| | | */ |
| | | public long download(String key, OutputStream out) { |
| | | public void download(String key, OutputStream out, Consumer<Long> consumer) { |
| | | try { |
| | | // 构建下载请求 |
| | | DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder() |
| | |
| | | Download<ResponseInputStream<GetObjectResponse>> responseFuture = transferManager.download(downloadRequest); |
| | | // 输出到流中 |
| | | try (ResponseInputStream<GetObjectResponse> responseStream = responseFuture.completionFuture().join().result()) { // auto-closeable stream |
| | | return responseStream.transferTo(out); // 阻塞调用线程 blocks the calling thread |
| | | if (consumer != null) { |
| | | consumer.accept(responseStream.response().contentLength()); |
| | | } |
| | | responseStream.transferTo(out); // 阻塞调用线程 blocks the calling thread |
| | | } |
| | | } catch (Exception e) { |
| | | throw new OssException("文件下载失败,错误信息:[" + e.getMessage() + "]"); |
| | |
| | | /** |
| | | * 获取私有URL链接 |
| | | * |
| | | * @param objectKey 对象KEY |
| | | * @param second 授权时间 |
| | | * @param objectKey 对象KEY |
| | | * @param expiredTime 链接授权到期时间 |
| | | */ |
| | | public String getPrivateUrl(String objectKey, Integer second) { |
| | | public String getPrivateUrl(String objectKey, Duration expiredTime) { |
| | | // 使用 AWS S3 预签名 URL 的生成器 获取对象的预签名 URL |
| | | URL url = presigner.presignGetObject( |
| | | x -> x.signatureDuration(Duration.ofSeconds(second)) |
| | | x -> x.signatureDuration(expiredTime) |
| | | .getObjectRequest( |
| | | y -> y.bucket(properties.getBucketName()) |
| | | .key(objectKey) |