参考:
文件存储服务。
安装,可以设置key和secret,映射数据目录和配置目录
docker run -p 9000:9000 --name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=admin" \
-e "MINIO_SECRET_KEY=admin123456" \
-v /home/data:/data \
-v /home/config:/root/.minio \
minio/minio server /data
通过ip:9000可以查看
配置
vim /data/.minio.sys/config/config.json
java代码
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.MinioException;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Description
* @Author 田云
* @Date 2020/4/12 20:44
* @Version 1.0
*/
public class TestMinos {
public static final String NAME = "在线训练-API.pdf";
public static String endpoint = "http://10.20.222.191:9000/";
public static String accessKeyId = "minioadmin";
public static String accessKeySecret = "minioadmin";
public static String bucketName = "test";
public static void main(String[] args) throws Exception {
//multiUpload("C:\\Users\\1\\Desktop\\在线训练-API.pdf");
downloadFile(NAME, NAME);
}
public static void multiUpload(String path) throws Exception {
try {
// Create a minioClient with the MinIO Server name, Port, Access key and Secret key.
MinioClient minioClient = new MinioClient(endpoint, accessKeyId, accessKeySecret);
// Check if the bucket already exists.
boolean isExist = minioClient.bucketExists(bucketName);
if (isExist) {
System.out.println("Bucket already exists.");
} else {
// Make a new bucket called asiatrip to hold a zip file of photos.
minioClient.makeBucket(bucketName);
}
// Upload the zip file to the bucket with putObject
File file = new File(path);
long size = FileUtils.sizeOf(file);
PutObjectOptions options = new PutObjectOptions(size, -1);
minioClient.putObject(bucketName, file.getName(), new FileInputStream(file), options);
System.out.println("/home/user/Photos/asiaphotos.zip is successfully uploaded as asiaphotos.zip to `asiatrip` bucket.");
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}
public static void downloadFile(String remotePath, String localPath) throws IOException {
try {
MinioClient minioClient = new MinioClient(endpoint, accessKeyId, accessKeySecret);
try (InputStream stream = minioClient.getObject(bucketName, remotePath);
OutputStream outStream = new FileOutputStream(new File(localPath));) {
byte[] buf = new byte[1024 * 8];
while (true) {
int read = 0;
if (stream != null) {
read = stream.read(buf);
}
if (read == -1) {
break;
}
outStream.write(buf, 0, read);
}
byte[] b = new byte[1024];
while ((stream.read(b)) != -1) {
outStream.write(b);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
throw new RuntimeException("下载异常");
}
}
}