参考:

    文件存储服务。

    安装,可以设置key和secret,映射数据目录和配置目录

    1. docker run -p 9000:9000 --name minio \
    2. -d --restart=always \
    3. -e "MINIO_ACCESS_KEY=admin" \
    4. -e "MINIO_SECRET_KEY=admin123456" \
    5. -v /home/data:/data \
    6. -v /home/config:/root/.minio \
    7. minio/minio server /data

    通过ip:9000可以查看
    image.png

    配置

    1. vim /data/.minio.sys/config/config.json

    java代码

    1. import com.aliyun.oss.OSS;
    2. import com.aliyun.oss.OSSClientBuilder;
    3. import com.aliyun.oss.model.*;
    4. import io.minio.MinioClient;
    5. import io.minio.PutObjectOptions;
    6. import io.minio.errors.MinioException;
    7. import org.apache.commons.io.FileUtils;
    8. import java.io.*;
    9. import java.util.ArrayList;
    10. import java.util.List;
    11. /**
    12. * @Description
    13. * @Author 田云
    14. * @Date 2020/4/12 20:44
    15. * @Version 1.0
    16. */
    17. public class TestMinos {
    18. public static final String NAME = "在线训练-API.pdf";
    19. public static String endpoint = "http://10.20.222.191:9000/";
    20. public static String accessKeyId = "minioadmin";
    21. public static String accessKeySecret = "minioadmin";
    22. public static String bucketName = "test";
    23. public static void main(String[] args) throws Exception {
    24. //multiUpload("C:\\Users\\1\\Desktop\\在线训练-API.pdf");
    25. downloadFile(NAME, NAME);
    26. }
    27. public static void multiUpload(String path) throws Exception {
    28. try {
    29. // Create a minioClient with the MinIO Server name, Port, Access key and Secret key.
    30. MinioClient minioClient = new MinioClient(endpoint, accessKeyId, accessKeySecret);
    31. // Check if the bucket already exists.
    32. boolean isExist = minioClient.bucketExists(bucketName);
    33. if (isExist) {
    34. System.out.println("Bucket already exists.");
    35. } else {
    36. // Make a new bucket called asiatrip to hold a zip file of photos.
    37. minioClient.makeBucket(bucketName);
    38. }
    39. // Upload the zip file to the bucket with putObject
    40. File file = new File(path);
    41. long size = FileUtils.sizeOf(file);
    42. PutObjectOptions options = new PutObjectOptions(size, -1);
    43. minioClient.putObject(bucketName, file.getName(), new FileInputStream(file), options);
    44. System.out.println("/home/user/Photos/asiaphotos.zip is successfully uploaded as asiaphotos.zip to `asiatrip` bucket.");
    45. } catch (MinioException e) {
    46. System.out.println("Error occurred: " + e);
    47. }
    48. }
    49. public static void downloadFile(String remotePath, String localPath) throws IOException {
    50. try {
    51. MinioClient minioClient = new MinioClient(endpoint, accessKeyId, accessKeySecret);
    52. try (InputStream stream = minioClient.getObject(bucketName, remotePath);
    53. OutputStream outStream = new FileOutputStream(new File(localPath));) {
    54. byte[] buf = new byte[1024 * 8];
    55. while (true) {
    56. int read = 0;
    57. if (stream != null) {
    58. read = stream.read(buf);
    59. }
    60. if (read == -1) {
    61. break;
    62. }
    63. outStream.write(buf, 0, read);
    64. }
    65. byte[] b = new byte[1024];
    66. while ((stream.read(b)) != -1) {
    67. outStream.write(b);
    68. }
    69. } catch (Exception e) {
    70. e.printStackTrace();
    71. }
    72. } catch (Exception e) {
    73. throw new RuntimeException("下载异常");
    74. }
    75. }
    76. }