Implement lab6 observability stack

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-11-16 21:14:03 +03:00
co-authored by Copilot
parent 470d9d6f1e
commit 3fffaa9966
26 changed files with 1887 additions and 31 deletions
+53
View File
@@ -0,0 +1,53 @@
package ru.mtusi.lab6;
import io.minio.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
final class S3Store {
private final MinioClient client;
private final String bucket;
private final Path localRoot;
S3Store() {
String endpoint = env("S3_ENDPOINT", "http://minio:9000");
localRoot = endpoint.startsWith("file:") ? Path.of(endpoint.substring("file:".length())) : null;
client = localRoot == null ? MinioClient.builder().endpoint(endpoint)
.credentials(env("S3_ACCESS_KEY", "messenger"), env("S3_SECRET_KEY", "messenger-secret")).build() : null;
bucket = env("S3_BUCKET", "attachments");
if (localRoot != null) {
try { Files.createDirectories(localRoot.resolve(bucket)); } catch (IOException e) { throw new IllegalStateException(e); }
return;
}
try {
if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build()))
client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
} catch (Exception e) { throw new IllegalStateException("object storage initialization failed", e); }
}
void put(String key, byte[] bytes, String type) {
if (localRoot != null) {
try { Path target=localRoot.resolve(bucket).resolve(key); Files.createDirectories(target.getParent()); Files.write(target, bytes); return; }
catch (IOException e) { throw ApiException.unavailable(); }
}
try { client.putObject(PutObjectArgs.builder().bucket(bucket).object(key)
.stream(new ByteArrayInputStream(bytes), bytes.length, -1).contentType(type).build()); }
catch (Exception e) { throw ApiException.unavailable(); }
}
InputStream get(String key) {
if (localRoot != null) {
try { return Files.newInputStream(localRoot.resolve(bucket).resolve(key)); } catch (IOException e) { throw ApiException.notFound(); }
}
try { return client.getObject(GetObjectArgs.builder().bucket(bucket).object(key).build()); }
catch (Exception e) { throw ApiException.notFound(); }
}
void remove(String key) {
if (localRoot != null) {
try { Files.deleteIfExists(localRoot.resolve(bucket).resolve(key)); return; } catch (IOException e) { throw ApiException.unavailable(); }
}
try { client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(key).build()); }
catch (Exception e) { throw ApiException.unavailable(); }
}
private static String env(String n, String f) { String v=System.getenv(n); return v==null||v.isBlank()?f:v.trim(); }
}