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

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

环境

软件 版本
docker 24.0.4
minio RELEASE.2023-10-24T05-18-28Z (commit-id=97cc12fdc539361cf175ffc2f00480eec0836d82)

客户端(浏览器)

打开对应的桶
使用minio给存储的对象添加过期时间_第1张图片
在这里添加

通过代码(这里以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) {

    }
}

你可能感兴趣的:(springboot,开发语言,java,spring,boot,后端)