Skip to content

Commit

Permalink
add: 添加 minio 简单测试工程及相关脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
mianshenglee committed Mar 31, 2020
1 parent 4efa60f commit 0b2a2ee
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 0 deletions.
31 changes: 31 additions & 0 deletions minio-simple-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### VS Code ###
.vscode/
54 changes: 54 additions & 0 deletions minio-simple-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>me.mason.demo</groupId>
<artifactId>minio-simple-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>minio-simple-demo</name>
<description>temp test Demo project for MinIO</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>6.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
121 changes: 121 additions & 0 deletions minio-simple-demo/scripts/minio-serviced.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/bin/sh
#
#
# chkconfig: 2345 67 92
# description: minio service

#执行程序启动所使用的系统用户
RUNNING_USER=root

###################################
#环境变量及程序执行参数
###################################
MINIO_HOME=/opt/minio
MINIO_DATA_HOME=/opt/min-data
MINIO_LOGFILE=${MINIO_HOME}/minio.log
MINIO_HOST=127.0.0.1
MINIO_PORT=9000
#accesskey and secretkey
ACCESS_KEY=minio
SECRET_KEY=minio123

#初始化psid变量(全局)
psid=0

checkpid() {
pidps=`ps -ef|grep ${MINIO_DATA_HOME}|grep -v grep`

if [ -n "${pidps}" ]; then
psid=`echo ${pidps} | awk '{print $2}'`
else
psid=0
fi
}

###################################
#(函数)启动程序
###################################
start() {
checkpid

if [ ${psid} -ne 0 ]; then
echo "================================"
echo "warn: minio already started! (pid=${psid})"
echo "================================"
else
echo -n "Starting minio, data path: ${MINIO_DATA_HOME} ..."
START_CMD="MINIO_ACCESS_KEY=${ACCESS_KEY} MINIO_SECRET_KEY=${SECRET_KEY} nohup ${MINIO_HOME}/minio server --address "${MINIO_HOST}:${MINIO_PORT}" ${MINIO_DATA_HOME} > ${MINIO_LOGFILE} 2>&1 &"

su - ${RUNNING_USER} -c "${START_CMD}"
checkpid
if [ ${psid} -ne 0 ]; then
echo "(pid=$psid) [OK]"
else
echo "[Failed]"
fi
fi
}

###################################
#(函数)停止程序
###################################
stop() {
checkpid

if [ ${psid} -ne 0 ]; then
echo -n "Stopping minio ...(pid=${psid}) "
su - ${RUNNING_USER} -c "kill -9 ${psid}"
if [ $? -eq 0 ]; then
echo "[OK]"
else
echo "[Failed]"
fi

checkpid
if [ ${psid} -ne 0 ]; then
stop
fi
else
echo "================================"
echo "warn: minio is not running"
echo "================================"
fi
}

###################################
#(函数)检查程序运行状态
###################################
status() {
checkpid

if [ ${psid} -ne 0 ]; then
echo "minio is running! (pid=${psid})"
else
echo "minio is not running"
fi
}

###################################
#读取脚本的第一个参数($1),进行判断
#参数取值范围:{start|stop|restart|status}
#如参数不在指定范围之内,则打印帮助信息
###################################
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'status')
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.mason.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestdemoApplication {

public static void main(String[] args) {
SpringApplication.run(TestdemoApplication.class, args);
}

}
145 changes: 145 additions & 0 deletions minio-simple-demo/src/main/java/me/mason/demo/minio/FileManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package me.mason.demo.minio;

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.errors.*;
import io.minio.messages.Item;
import org.xmlpull.v1.XmlPullParserException;

import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;

/**
* MinIO文件处理类
*
* @author: mason
* @since: 2020/3/30
**/
public class FileManager {

/**
* 上传文件
*
* @param objectName
* @param filePath
* @throws XmlPullParserException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws IOException
*/
public void uploadFile(MinioClient minioClient, String bucketName, String objectName, String filePath) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
try {
// Check if the bucket already exists.
boolean isExist = minioClient.bucketExists(bucketName);
if (!isExist) {
// Make a new bucket to hold file
minioClient.makeBucket(bucketName);
}
// Upload file to the bucket with putObject
minioClient.putObject(bucketName, objectName, filePath, null, null, null, null);
System.out.println("put " + filePath + " to " + bucketName + ",OK");
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}

/**
* 下载文件
*
* @param objectName
* @param downloadPath
* @throws XmlPullParserException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws IOException
*/
public void downloadFile(MinioClient minioClient, String bucketName, String objectName, String downloadPath) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
File file = new File(downloadPath);
try (OutputStream out = new FileOutputStream(file)) {
InputStream inputStream = minioClient.getObject(bucketName, objectName);
byte[] tempbytes = new byte[100];
int byteread = 0;
while ((byteread = inputStream.read(tempbytes)) != -1) {
out.write(tempbytes, 0, byteread);
}

System.out.println("get " + objectName + " to " + downloadPath + ",OK");
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}

/**
* 删除文件
*
* @param minioClient
* @param objectName
*/
public void deleteFile(MinioClient minioClient, String bucketName, String objectName) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidArgumentException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException {
minioClient.removeObject(bucketName, objectName);
}

/**
* 罗列文件
* @param minioClient
* @param bucketName
* @throws XmlPullParserException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws IOException
*/
public void listFile(MinioClient minioClient, String bucketName) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
try {
Iterable<Result<Item>> results = minioClient.listObjects(bucketName);
Iterator<Result<Item>> iterator = results.iterator();
while (iterator.hasNext()) {
Item item = iterator.next().get();
System.out.println(item.objectName() + ", " + item.objectSize() + "B");
}
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}

/**
* 查看文件信息
* @param minioClient
* @param bucketName
* @param objectName
* @throws XmlPullParserException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws IOException
*/
public void getObjectStat(MinioClient minioClient, String bucketName, String objectName) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException {
try {
ObjectStat objectStat = minioClient.statObject(bucketName, objectName);
System.out.println(objectStat);
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}

/**
* 检查文件是否存在MinIO
*
* @param minioClient
* @param bucketName
* @param objectName
* @return
*/
public boolean checkFileExist(MinioClient minioClient, String bucketName, String objectName) {
boolean found = false;
try {
minioClient.statObject(bucketName, objectName);
found = true;
} catch (Exception e) {
e.printStackTrace();
}

return found;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading

0 comments on commit 0b2a2ee

Please sign in to comment.