cn.structure.starter.minio.http.MinioEndpoint Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2018-2025, freelance All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the freelance.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author:
*/
package cn.structure.starter.minio.http;
import cn.structure.starter.minio.service.MinioTemplate;
import cn.structure.starter.minio.vo.MinioItem;
import io.minio.StatObjectResponse;
import io.minio.messages.Bucket;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
*
* minio 对外提供服务端点
*
*
* @author chuck
* @version 1.0.1
* @since 2021/7/17 14:06
*/
@RestController
@AllArgsConstructor
@RequestMapping("/api/minio")
public class MinioEndpoint {
private final MinioTemplate template;
/**
* Bucket Endpoints
*/
@SneakyThrows
@PostMapping("/bucket/{bucketName}")
public Bucket createBucket(@PathVariable String bucketName) {
template.createBucket(bucketName);
return Optional
.of(template.getBucket(bucketName))
.get()
.orElse(null);
}
@SneakyThrows
@GetMapping("/bucket")
public List getBuckets() {
return template.getAllBuckets();
}
@SneakyThrows
@GetMapping("/bucket/{bucketName}")
public Bucket getBucket(@PathVariable String bucketName) {
return template.getBucket(bucketName).orElseThrow(() -> new IllegalArgumentException("Bucket Name not found!"));
}
@SneakyThrows
@DeleteMapping("/bucket/{bucketName}")
@ResponseStatus(HttpStatus.ACCEPTED)
public void deleteBucket(@PathVariable String bucketName) {
template.removeBucket(bucketName);
}
/**
* Object Endpoints
*/
@SneakyThrows
@PostMapping("/object/{bucketName}")
public StatObjectResponse createObject(@RequestBody MultipartFile object, @PathVariable String bucketName) {
String name = object.getOriginalFilename();
template.putObject(bucketName, name, object.getInputStream(), object.getSize(), object.getContentType());
return template.getObjectInfo(bucketName, name);
}
@SneakyThrows
@PostMapping("/object/{bucketName}/{objectName}")
public StatObjectResponse createObject(@RequestBody MultipartFile object, @PathVariable String bucketName, @PathVariable String objectName) {
template.putObject(bucketName, objectName, object.getInputStream(), object.getSize(), object.getContentType());
return template.getObjectInfo(bucketName, objectName);
}
@SneakyThrows
@GetMapping("/object/{bucketName}/{objectName}")
public List filterObject(@PathVariable String bucketName, @PathVariable String objectName) {
return template.getAllObjectsByPrefix(bucketName, objectName, true);
}
@SneakyThrows
@GetMapping("/object/{bucketName}/{objectName}/{expires}")
public Map getObject(@PathVariable String bucketName, @PathVariable String objectName, @PathVariable Integer expires) {
Map responseBody = new HashMap<>(8);
// Put Object info
responseBody.put("bucket", bucketName);
responseBody.put("object", objectName);
responseBody.put("url", template.getObjectURL(bucketName, objectName, expires));
responseBody.put("expires", expires);
return responseBody;
}
@SneakyThrows
@ResponseStatus(HttpStatus.ACCEPTED)
@DeleteMapping("/object/{bucketName}/{objectName}/")
public void deleteObject(@PathVariable String bucketName, @PathVariable String objectName) {
template.removeObject(bucketName, objectName);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy