1、Bucket、Object
- Bucket is the logical space for storing Objects. The data between each Bucket is isolated from each other. For users, it is equivalent to the top-level folder where files are stored;
- Object is a basic object stored in MinIO, which is equivalent to a file for users;
2. Common APIs
- bucketExists(): used to check whether the specified bucket exists, return a boolean value, indicating whether the bucket exists;
- makeBucket(): used to create a new bucket, and the name of the bucket needs to be specified;
- listBuckets(): is used to list all buckets that the user has permission to access and return a list of buckets;
- removeBucket(): used to delete an existing bucket (bucket). An exception will be thrown if the delete fails;
- putObject(): used to upload files to the specified bucket;
- statObject(): used to check the status of the specified object (file) and determine whether it exists;
- getPresignedObjectUrl(): Used to generate a signed URL of an object (file) so that it can be accessed over HTTP;
- getObject(): used to download files from the specified bucket;
- listObjects(): Used to list all objects (files) in the specified bucket;
- removeObject(): used to delete objects in the specified bucket, and you need to specify the bucket name and object key;
3. Integrate SpringBoot
Introducing POM package:
<dependencies> <!-- minio --> <dependency> <groupId></groupId> <artifactId>minio</artifactId> <version>8.5.9</version> </dependency> </dependencies>
3.1. Yaml configuration
minio: # Connection address endpoint: http://127.0.0.1:9000 # username accessKey: admin # password secretKey: 123456789 # Set a shared bucket. The specific name can be set in the MinIo background and can be accessed directly. Format: http://1ip:port/bucketName/fileName publicBucket: public-test # Set up the private bucket. The specific name can be set in the MinIo background. You need to obtain the signature link through the getPresignedObjectUrl method. privateBucket: private-test
3.2. MinIo configuration
MinIOInfoConfig:
@Data @Component @ConfigurationProperties(prefix = "minio") public class MinIOInfoConfig { /** * Connection address */ private String endpoint; /** * username */ private String accessKey; /** * password */ private String secretKey; /** * Private bucket */ private String privateBucket; /** * Public bucket */ private String publicBucket; }
MinioConfig:
import ; import ; import ; import ; @Configuration public class MinioConfig { @Resource private MinIOInfoConfig minIOInfoConfig; @Bean public MinioClient minioClient() { //Chain programming, build MinioClient object return () .endpoint(()) .credentials((), ()) .build(); } }
3.3. Bucket operation
Before uploading the file, you need to do it firstBucket
The creation operation can be created directly to the MinIo background or through the API interface. The code example is as follows
@Service public class MinIOService { @Resource private MinioClient minioClient; public void bucket() { try { String bucketName = "test-bucket"; // Determine whether it exists boolean bucketExists = (().bucket(bucketName).build()); ("bucketExists1 = " + bucketExists); // Create (().bucket(bucketName).build()); //Judge again bucketExists = (().bucket(bucketName).build()); ("bucketExists2 = " + bucketExists); // Query list List<Bucket> bucketList = (); List<String> list = ().map(Bucket::name).toList(); ("bucketList = " + list); // delete (().bucket(bucketName).build()); //Judge again bucketExists = (().bucket(bucketName).build()); ("bucketExists3 = " + bucketExists); } catch (Exception e) { throw new RuntimeException(e); } } }
3.4. Object operation
1. Upload the file
@Service public class MinIOService { @Resource private MinioClient minioClient; @Resource private MinIOInfoConfig minIOInfoConfig; public void upload(MultipartFile file) { try { String originalFilename = (); // Upload file (() .bucket(()) .object(originalFilename) .stream((), (), -1) .build() ); // Determine whether the file exists // Get the access address } catch (Exception e) { throw new RuntimeException(e); } } }
2. Get the file status (whether it exists)
@Service public class MinIOService { @Resource private MinioClient minioClient; @Resource private MinIOInfoConfig minIOInfoConfig; public void fileState(String fileName) { try { StatObjectResponse response = (() .bucket(()) .object(fileName) .build()); ("response = " + response); } catch (Exception e) { ("The file does not exist"); } } }
3. Generate a signed public access connection
1. PassgetPresignedObjectUrl
Method, generate a URL with expiration time and signature. This address can be provided to third-party shared access or upload objects without login, forBucket
For private situations.
2. For shared files, you can usehttp://1ip:port/bucketName/fileName
Direct access to format.
@Service public class MinIOService { @Resource private MinioClient minioClient; @Resource private MinIOInfoConfig minIOInfoConfig; public String getPresignedObjectUrl(String fileName) { try { String presignedObjectUrl = (() .bucket(()) .object(fileName) // Set the expiration time, 3 minutes .expiry(3, ) .method() .build()); (presignedObjectUrl); return presignedObjectUrl; } catch (Exception e) { return "Failed to get link"; } } }
4. Download file stream
1. PassgetObject()
The method can directly obtain the file stream and download the file stream directly through the browser;
2. The advantage of using this method is that it can be transmitted from the front-end in business.File Id
, the server passesFile Id
QueryedFile name
Then call MinIO's API interface to obtain the file flow, which can realize the system's own horizontal permission management of files.
@Service public class MinIOService { @Resource private MinioClient minioClient; @Resource private MinIOInfoConfig minIOInfoConfig; public void getObjectByStream(String fileName, HttpServletResponse response) { try { GetObjectResponse getObjectResponse = (() .bucket(()) .object(fileName) .build()); // Convert to stream (()); } catch (Exception e) { ("Failed to obtain file"); } } }
5. Get the file list
@Service public class MinIOService { @Resource private MinioClient minioClient; @Resource private MinIOInfoConfig minIOInfoConfig; public void listObjects() { try { Iterable<Result<Item>> listObjects = (() .bucket(()) // File name starting with xx // .prefix("/") .build()); (itemResult -> { try { Item item = (); ("File Name:" + ()); } catch (Exception e) { throw new RuntimeException(e); } }); } catch (Exception e) { ("Failed to obtain file"); } } }
6. Delete file information
@Service public class MinIOService { @Resource private MinioClient minioClient; @Resource private MinIOInfoConfig minIOInfoConfig; public void removeObject(String fileName) { try { // Single deletion (() .bucket(()) .object(fileName) .build()); // Batch Delete List<DeleteObject> list = new ArrayList<>(); (new DeleteObject(fileName)); (() .bucket(()) .objects(list) .build()); } catch (Exception e) { ("Delete file failed"); } } }
This is the end of this article about SpringBoot integrating Minio (supporting public and private buckets). For more related SpringBoot integrating Minio content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!