What is MinIo
Minio is an open source lightweight file server under Apcche. It is based on object storage, and the protocol is based on Apache License v2.0. Open source can be used for business.
Minio is mainly used to store unstructured data, such as files, pictures, photos, log files, various backup files, etc. according to the description of the official website, the file size ranges from several KB to 5TB.
Minio provides a very convenient and friendly interface, and the documents are also very rich. For details, please refer to its documents: https://docs.min.io/cn/
Why choose MinIo
In the previous development, FASTDFS, a distributed file service, and OSS object storage of alicloud were used for storage. However, OSS is too expensive and FASTDFS is too cumbersome to build and configure. Today, we recommend a high-performance object storage service MinIo, which is easy to use.
MinIO is a high-performance object storage, compatible with Amazon S3 interface, fully considering the needs and experience of developers; supports distributed storage, with high scalability and availability; simple deployment but rich functions. Official documents are also very detailed. It has many different deployment modes (stand-alone deployment, distributed deployment).
The reason why MinIO is easy to use is that its startup, operation and configuration are very simple. You can install and run it by docker, or you can download the binary file and run it by script.
install
One click installation of docker is recommended:
docker run -it -p 9000:9000 --name minio \ -d --restart=always \ -e "MINIO_ACCESS_KEY=admin" \ -e "MINIO_SECRET_KEY=admin123456" \ -v /mnt/minio/data:/data \ -v /mnt/minio/config:/root/.minio \ minio/minio server /data
be careful:
- Key must be greater than 8 bits, otherwise creation will fail
- File directory and configuration file must be mapped to the host, you know
Integrating Nginx
server{ listen 80; server_name minio.javakf.com.cn; location /{ proxy_set_header Host $http_host; proxy_pass http://localhost:9000; } location ~ /\.ht { deny all; } }
In this way, access the configured address through the browser and use the specified MINIO_ACCESS_KEY and Minio_ SECRET_ Just log in with key.
After a brief look, the function is OK. It supports creating buckets, uploading, deleting, sharing and downloading files, and setting read-write permissions for buckets.
integration
Minio supports access to JavaScript, Java, Python, Golang and other languages. Here, we choose the most familiar Java language and use the most popular framework, SpringBoot.
pom.xml introduce
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.0.2</version> </dependency>
application.properties to configure
# MinIo file server min.io.endpoint = http://minio.javakf.com.cn min.io.accessKey = admin min.io.secretKey = admin123456
MinIoProperties.java Configure entities
@Data @ConfigurationProperties(prefix = "min.io") public class MinIoProperties { private String endpoint; private String accessKey; private String secretKey; }
Tools
@Component @Configuration @EnableConfigurationProperties({ MinIoProperties.class }) public class MinIoUtils { private MinIoProperties minIo; public MinIoUtils(MinIoProperties minIo) { this.minIo = minIo; } private static MinioClient instance; @PostConstruct public void init() { try { instance = new MinioClient(minIo.getEndpoint(), minIo.getAccessKey(), minIo.getSecretKey()); } catch (InvalidPortException e) { e.printStackTrace(); } catch (InvalidEndpointException e) { e.printStackTrace(); } } /** * Determine whether the bucket exists * * @param bucketName * @return */ public static boolean bucketExists(String bucketName) { try { return instance.bucketExists(bucketName); } catch (Exception e) { e.printStackTrace(); } return false; } /** * Create bucket * * @param bucketName */ public static void makeBucket(String bucketName) { try { boolean isExist = instance.bucketExists(bucketName); if (!isExist) { instance.makeBucket(bucketName); } } catch (Exception e) { e.printStackTrace(); } } /** * File upload * * @param bucketName * @param objectName * @param filename */ public static void putObject(String bucketName, String objectName, String filename) { try { instance.putObject(bucketName, objectName, filename, null); } catch (Exception e) { e.printStackTrace(); } } /** * File upload * * @param bucketName * @param objectName * @param stream */ public static void putObject(String bucketName, String objectName, InputStream stream) { try { instance.putObject(bucketName, objectName, stream, null); } catch (Exception e) { e.printStackTrace(); } } /** * Delete file * * @param bucketName * @param objectName */ public static void removeObject(String bucketName, String objectName) { try { instance.removeObject(bucketName, objectName); } catch (Exception e) { e.printStackTrace(); } } // Omit various cruds }
Currently, the SDK does not support folder creation. If you want to create a folder, you can only upload and create it by file.
minIoUtils.putObject("javakf","test/1.jpg","C:\\1.jpg");
An instance can only have one account. If you want to use multiple accounts, you need to create multiple instances. In addition, minio also supports single host, multi disk and distributed deployment, but for most single application, single application is enough.