From today on, roll up your sleeves and follow me
Tip: if you have any questions, please contact the private message, below the source code address, please take
preface
Using Spring Boot makes it very easy and quick to build projects. We don’t have to worry about compatibility between frameworks, suitable versions, etc. We can use anything we want, just add a configuration.
Tip: The following is the text of this article, the following case for reference
1. Technical introduction
1. What is Minio?
MinIO is an object storage service based on the Apache License V2.0. Compatible with Amazon S3 cloud storage service interface, it is suitable for storing large capacity of unstructured data, such as pictures, videos, log files, backup data, and container/VM images. An object file can be any size, ranging from a few KB to a maximum of 5T.
Two, the use of steps
1. Introduce maven library
The code is as follows (example) :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.41.</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.03.</version>
</dependency>
</dependencies>
Copy the code
2. Wrap Minio
IFile class specifying:
package com.hyh.minio;
/** * File interface class ** @author: heyuhua * @date: 2021/1/12 10:33 */
public interface IFile {
/** * upload ** @param filename filename */
void upload(String filename);
/** * upload ** @param filename filename * @param object save object filename */
void upload(String filename, String object);
}
Copy the code
File interface class:
package com.hyh.minio.service;
import com.hyh.minio.IFile;
/** * File interface ** @author: heyuhua * @date: 2021/1/12 10:53 */
public interface FileService extends IFile {
/** * upload ** @param filename filename * @param object object filename * @param bucket bucket */
void upload(String filename, String object, String bucket);
}
Copy the code
File interface implementation class:
package com.hyh.minio.service.impl;
import com.hyh.minio.service.FileService;
import com.hyh.utils.common.StringUtils;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/** * File interface service implementation ** @author: heyuhua * @date: 2021/1/12 10:53 */
@Service
public class FileServiceImpl implements FileService {
/** * Log */
private static final Logger LOG = LoggerFactory.getLogger(FileServiceImpl.class);
/** * Minio client */
@Autowired
private MinioClient minioClient;
/** * Default bucket name */
@Value("${minio.bucketName:}")
private String defaultBucketName;
@Override
public void upload(String filename) {
uploadObject(filename, null, defaultBucketName);
}
@Override
public void upload(String filename, String object) {
uploadObject(filename, object, defaultBucketName);
}
@Override
public void upload(String filename, String object, String bucket) {
uploadObject(filename, object, bucket);
}
/** * Upload ** @param filename * @param object * @param bucket */
private void uploadObject(String filename, String object, String bucket) {
if (StringUtils.isAnyBlank(filename, bucket))
return;
try {
// Bucket build
bucketBuild(bucket);
// The name of the file to save
object = StringUtils.isBlank(object) ? filename.substring(filename.lastIndexOf("/") > 0 ? filename.lastIndexOf("/") : filename.lastIndexOf("\ \")) : object;
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(bucket)
.object(object)
.filename(filename)
.build());
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException exception) {
LOG.error("uploadObject error", exception); }}/** * Bucket build ** @param bucketName */
private void bucketBuild(String bucketName) {
try {
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if(! found) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); LOG.info("Bucket " + bucketName + " make success.");
} else {
LOG.info("Bucket " + bucketName + " already exists."); }}catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException exception) {
LOG.error("bucketBuild error", exception); }}public String getDefaultBucketName(a) {
return defaultBucketName;
}
public void setDefaultBucketName(String defaultBucketName) {
this.defaultBucketName = defaultBucketName; }}Copy the code
Minio configuration class:
package com.hyh.minio.config;
import io.minio.MinioClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** * @Author: heyuhua * @Date: 2021/1/12 10:42 */
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
/** * endPoint is a URL, domain name, IPv4 or IPv6 address */
private String endpoint;
/** * Port */
private int port;
/** * AccessKeys are similar to user ids and are used to uniquely identify your account */
private String accessKey;
/**
* secretKey是你账户的密码
*/
private String secretKey;
/** * If true, HTTPS is used instead of HTTP. The default is true */
private Boolean secure;
/** * Default bucket */
private String bucketName;
/** * Configure directory */
private String configDir;
@Bean
public MinioClient getMinClient(a) {
return MinioClient.builder()
.endpoint(endpoint, port, secure)//http
.credentials(accessKey, secretKey)
.build();
}
public String getEndpoint(a) {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAccessKey(a) {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public String getSecretKey(a) {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public Boolean getSecure(a) {
return secure;
}
public void setSecure(Boolean secure) {
this.secure = secure;
}
public String getBucketName(a) {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
public String getConfigDir(a) {
return configDir;
}
public void setConfigDir(String configDir) {
this.configDir = configDir;
}
public int getPort(a) {
return port;
}
public void setPort(int port) {
this.port = port; }}Copy the code
Minio helper class encapsulates:
package com.hyh.minio.helper;
import com.hyh.minio.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
/** * Minio assistant ** @author: heyuhua * @date: 2021/1/12 10:54 */
@Component
public class MinioHelper {
/** * Log */
private static final Logger LOG = LoggerFactory.getLogger(MinioHelper.class);
/** * File interface service */
@Autowired
private FileService fileService;
/** * Upload ** @param filename */
public void upload(String filename) {
Assert.notNull(filename, "filename is null.");
fileService.upload(filename);
}
/** * Upload ** @param filename * @param object */
public void upload(String filename, String object) {
Assert.notNull(filename, "filename is null.");
Assert.notNull(object, "object is null.");
fileService.upload(filename, object);
}
/** * Upload ** @param filename * @param object * @param bucket */
public void upload(String filename, String object, String bucket) {
Assert.notNull(filename, "filename is null.");
Assert.notNull(object, "object is null.");
Assert.notNull(bucket, "bucket is null."); fileService.upload(filename, object, bucket); }}Copy the code
3. Configure the file
The code is as follows (example) :
server:
port: 8088
# minio configuration
minio:
endpoint: 39.10849.252.
port: 9000
accessKey: admin
secretKey: admin123
secure: false
bucketName: "hyh-bucket"
configDir: "/home/data/"
Copy the code
4. Unit testing
The test code is as follows (for example) :
package com.hyh.core.test;
import com.hyh.core.test.base.HyhTest;
import com.hyh.minio.helper.MinioHelper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/** * Minio Test * * @Author: heyuhua * @Date: 2021/1/12 11:54 */
public class MinioTest extends HyhTest {
@Autowired
private MinioHelper minioHelper;
@Test
public void testUpload(a) {
// Direct to your local path test
String filename = "E:\\home\\static\\img\\fsPic\\0c34de99ac6b4c56812e83c4eab13a6f.jpg";
String object = "hyh-test-name.jpg";
String bucket = "hyh-test-bucket";
minioHelper.upload(filename);
minioHelper.upload(filename, object);
minioHelper.upload(filename, object, bucket);
// Open the browser, go to http://ip:9000, and log in to the console to view the uploaded file
}
@Test
@Override
public void test(a) {
System.out.println("Minio test - - -"); }}Copy the code
conclusion
Does it feel simple? Follow me as I take you to uncover more advanced use of Minio source address: click here to see the source code.