1. Windows環境でのMinIOインストール
1.1 MinIO概要
MinIOはGolang製のオープンソースオブジェクトストレージソリューションで、軽量ながらも高いパフォーマンスを提供します。
- 公式サイト: MinIO公式サイト
- 日本語ドキュメント: 旧バージョンのため公式英語ドキュメントを推奨
オブジェクトストレージとは?
大容量のファイル保存に適したクラウドストレージサービスで、スケーラビリティとコスト効率に優れています。
1.2 インストール手順
ダウンロード
- Windows版: 公式ダウンロード
- インストール先例:
C:\Program Files\MinIO
起動方法
minio.exe server D:\minio-data
管理者権限のコマンドプロンプトで実行し、初期ログインはminioadmin/minioadminを使用します。
バケット作成
初期状態では空のバケットが作成され、アクセス権限はprivateです。Web管理画面でpublicに変更可能です。
2. Spring Bootとの連携
2.1 依存関係設定
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.7</version>
</dependency>
2.2 設定ファイル
server:
port: 8080
spring:
servlet:
multipart:
max-file-size: 200MB
max-request-size: 500MB
enabled: true
minio:
endpoint: http://127.0.0.1:9000
accesskey: minioadmin
secretKey: minioadmin
2.3 フロントエンド実装
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">アップロード</button>
</form>
2.4 設定クラス
@Configuration
@ConfigurationProperties(prefix = "minio")
@Data
public class MinioProperties {
private String endpoint;
private String accessKey;
private String secretKey;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
2.5 ユーティリティクラス
@Component
@Slf4j
public class MinioService {
private final MinioClient client;
public MinioService(MinioClient client) {
this.client = client;
}
public void createBucket(String bucket) throws Exception {
if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {
client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
}
}
public String uploadFile(String bucket, MultipartFile file, String objectKey) throws Exception {
try (InputStream stream = file.getInputStream()) {
client.putObject(
PutObjectArgs.builder()
.bucket(bucket)
.object(objectKey)
.stream(stream, stream.available(), -1)
.contentType(file.getContentType())
.build()
);
return generatePreSignedUrl(bucket, objectKey, 7);
}
}
private String generatePreSignedUrl(String bucket, String objectKey, int expireDays) throws Exception {
return client.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucket)
.object(objectKey)
.expiry(expireDays, TimeUnit.DAYS)
.build()
);
}
}
2.6 コントローラ実装
@RestController
@RequestMapping("/api")
public class FileController {
private final MinioService minioService;
public FileController(MinioService minioService) {
this.minioService = minioService;
}
@PostMapping("/upload")
public String handleUpload(@RequestParam("file") MultipartFile file) throws Exception {
String filename = UUID.randomUUID() + file.getOriginalFilename();
minioService.uploadFile("uploads", file, filename);
return "Success";
}
}
3. メディアタイプ一覧
| 形式 | Content-Type |
|---|---|
| HTML | text/html |
| テキスト | text/plain |
| JSON | application/json |
| JPEG画像 | image/jpeg |
| PDF文書 | application/pdf |
4. 拡張ユーティリティ機能
public boolean isObjectExists(String bucket, String objectKey) {
try {
client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build());
return true;
} catch (Exception e) {
return false;
}
}
public void deleteObject(String bucket, String objectKey) throws Exception {
client.removeObject(RemoveObjectArgs.builder()
.bucket(bucket)
.object(objectKey)
.build());
}