使用minio给存储的对象添加过期时间

在一些场景中,储存的对象需要定时清理保证留出足够的磁盘空间,如果时linux的本地文件,需要采取定时任务清理,但是minio提供了这样的能力

环境

软件版本
docker24.0.4
minioRELEASE.2023-10-24T05-18-28Z (commit-id=97cc12fdc539361cf175ffc2f00480eec0836d82)

客户端(浏览器)

打开对应的桶
在这里插入图片描述
在这里添加

通过代码(这里以java为例)

配置类

@Configuration
public class MinioConfig {

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.accessKey}")
    private String accessKey;

    @Value("${minio.secretKey}")
    private String secretKey;

    @Bean
    public MinioClient minioClient() {
        MinioClient client = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
        try {
            client.setBucketLifecycle(
                    SetBucketLifecycleArgs
                            .builder()
                            .config(new LifecycleConfiguration(
                                    List.of(
                                            new LifecycleRule(Status.ENABLED,
                                                    null,
                                                    new Expiration((ResponseDate) null, 180, null),
                                                    new RuleFilter("/"),
                                                    "myDeleteRule",
                                                    null,
                                                    null,
                                                    null)
                                    )
                            ))
                            .bucket("vits")
                            .build()
            );
        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
                 InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException |
                 XmlParserException e) {
            throw new RuntimeException(e);
        }
        return client;
    }
}

上传工具类

@Slf4j
public class MinioUtils {

    public static String uploadFile(MinioClient minioClient, InputStream inputStream, String bucket, String filename) {
        try {
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("public").build());
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("public").build());
            }

            ObjectWriteResponse response = minioClient.putObject(
                    PutObjectArgs
                            .builder()
                            .bucket(bucket)
                            .object(filename)
                            .stream(inputStream, inputStream.available(), -1)
                            .contentType(InferStatusConstant.WAV_CONTENT_TYPE)
                            .build()
            );

            String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                            .bucket(bucket)
                            .expiry(7 * 24 * 60 * 60)
                            .object(filename)
                            .method(Method.GET)
                            .build());
            log.info("分享地址:" + url);
            return url;
        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
                 InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException |
                 XmlParserException e) {
            throw new RuntimeException(e);
        }
    }
}

测试类

@Test
public void uploadFileToMinio() {
    try (FileInputStream stream = new FileInputStream("/path/to/file")) {
        String url = MinioUtils.uploadFile(minioClient, stream, "public", "/path/to/file");
        System.out.println(url);
    } catch (Exception e) {

    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值