package com.qlinker.storage; import com.qlinker.config.QLinkerProperties; import com.qlinker.init.BootstrapCoordinator; import com.qlinker.init.InstanceIdentity; import io.minio.BucketExistsArgs; import io.minio.GetObjectArgs; import io.minio.GetPresignedObjectUrlArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; import io.minio.PutObjectArgs; import io.minio.RemoveObjectArgs; import io.minio.StatObjectArgs; import io.minio.StatObjectResponse; import io.minio.http.Method; import java.io.InputStream; import java.time.Duration; /** * 本实例专属 bucket 的对象存储访问。 * *
bucket 名来自持久化的实例身份,不接受外部传入;object key 一律由系统生成, * 不得由用户输入拼接(AGENTS.md 7.3)。访问 URL 使用短时效预签名,避免长期公开对象。
* *bucket 名在初始化完成后才确定,因此这里延迟解析:首次使用时才向初始化流程索取身份, * 未初始化时抛出明确异常,而不是连到一个默认 bucket。
*/ public class StorageService { private final MinioClient client; private final BootstrapCoordinator coordinator; private final Duration presignTtl; private volatile String bucket; public StorageService(MinioClient client, BootstrapCoordinator coordinator, QLinkerProperties properties) { QLinkerProperties.Storage storage = properties.getStorage(); if (storage.getEndpoint() == null || storage.getEndpoint().isBlank()) { throw new IllegalStateException("缺少 qlinker.storage.endpoint:对象存储地址未配置"); } this.client = client; this.coordinator = coordinator; this.presignTtl = properties.getMedia().getPresignTtl(); } public String bucket() { String current = bucket; if (current != null) { return current; } InstanceIdentity identity = coordinator.ensureReady(); bucket = identity.bucketName(); return bucket; } /** 校验 bucket 可访问;不可访问时抛出,由健康检查与初始化流程上报。 */ public void verifyAccessible() throws Exception { client.bucketExists(BucketExistsArgs.builder().bucket(bucket()).build()); } /** 初始化时确保 bucket 存在。已存在则跳过,保证初始化幂等。 */ public void ensureBucket() throws Exception { String name = bucket(); if (!client.bucketExists(BucketExistsArgs.builder().bucket(name).build())) { client.makeBucket(MakeBucketArgs.builder().bucket(name).build()); } } public void put(String objectKey, InputStream data, long size, String contentType) throws Exception { client.putObject(PutObjectArgs.builder() .bucket(bucket()) .object(objectKey) .stream(data, size < 0 ? -1 : size, size < 0 ? 16L * 1024 * 1024 : -1) .contentType(contentType == null ? "application/octet-stream" : contentType) .build()); } public InputStream get(String objectKey) throws Exception { return client.getObject(GetObjectArgs.builder().bucket(bucket()).object(objectKey).build()); } public boolean exists(String objectKey) { try { client.statObject(StatObjectArgs.builder().bucket(bucket()).object(objectKey).build()); return true; } catch (Exception e) { return false; } } public StatObjectResponse stat(String objectKey) throws Exception { return client.statObject(StatObjectArgs.builder().bucket(bucket()).object(objectKey).build()); } public void remove(String objectKey) throws Exception { client.removeObject(RemoveObjectArgs.builder().bucket(bucket()).object(objectKey).build()); } /** 生成短时效只读 URL。对象 key 由系统控制,不接受外部拼接。 */ public String presignedGetUrl(String objectKey) throws Exception { return client.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(bucket()) .object(objectKey) .expiry((int) Math.max(60, presignTtl.toSeconds())) .build()); } }