安装部署minio

服务器安装minio

1.进行下载

下载地址:

GNU/Linux

https://dl.min.io/server/minio/release/linux-amd64/minio

2.新建minio安装目录,执行如下命令

mkdir -p /home/minio/data

把二进制文件上传到安装目录后,执行:

1

2

3

4

chmod +x minio    //给予权限

export MINIO_ACCESS_KEY=minioadmin   //创建账号

export MINIO_SECRET_KEY=minioadmin   //创建密码

./minio server /home/minio/data        //启动

 后台启动,并打印日志

1

nohup ./minio server /home/minio/data > /home/minio/data/minio.log &

默认的端口为:9000

自定义端口方式:自定义启动端口号以及控制台端口号,不设置则控制台会自动配置其他端口号,非常不方便

1

nohup ./minio server --address :9000 --console-address :9001 /home/minio/data > /home/minio/data/minio.log &

查看状态

1

ps -ef|grep minio

二、进行访问,并设置桶

1.访问

地址:http://127.0.0.1:9000

 输入账号密码后:

进行创建桶,名字自取,创建完成后服务器home/minio/data下也会创建这个文件目录

 进行设置:

必须将规则设置成readwrite ,才可进行读取文件,否则只存或者只能读。

 三、springboot进行实现

1.引入依赖

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!-- minio 相关依赖 -->

<dependency>

    <groupId>io.minio</groupId>

    <artifactId>minio</artifactId>

    <version>3.0.10</version>

</dependency>

<!-- alibaba的fastjson -->

<dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>fastjson</artifactId>

    <version>1.2.51</version>

</dependency>

<!-- thymeleaf模板引擎 -->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

2.在 application.yml 文件中加入 MinIO 服务器的相关信息

1

2

3

4

5

# minio 文件存储配置信息

minio:

  endpoint: http://127.0.0.1:9000

  accesskey: minioadmin

  secretKey: minioadmin

3.创建实体类

这一步,我们将配置文件中 minio 的配置信息通过注解的方式注入到 MinioProp 这个实体中,方便后面我们使用

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

  

/**

 * minio 属性值

 */

@Data

@Component

@ConfigurationProperties(prefix = "minio")

public class MinioProp {

    /**

     * 连接url

     */

    private String endpoint;

    /**

     * 用户名

     */

    private String accesskey;

    /**

     * 密码

     */

    private String secretKey;

}

4.创建核心配置类

通过注入 MinIO 服务器的相关配置信息,得到 MinioClient 对象,我们上传文件依赖此对象

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

import io.minio.MinioClient;

import io.minio.errors.InvalidEndpointException;

import io.minio.errors.InvalidPortException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

  

/**

 * minio 核心配置类

 */

@Configuration

@EnableConfigurationProperties(MinioProp.class)

public class MinioConfig {

  

    @Autowired

    private MinioProp minioProp;

  

    /**

     * 获取 MinioClient

     *

     * @return

     * @throws InvalidPortException

     * @throws InvalidEndpointException

     */

    @Bean

    public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {

        return new MinioClient(minioProp.getEndpoint(), minioProp.getAccesskey(), minioProp.getSecretKey());

    }

}

5.上传工具类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

import com.alibaba.fastjson.JSONObject;

import com.zyxx.email.common.redis.RedisUtil;

import com.zyxx.email.utils.DateUtils;

import io.minio.MinioClient;

import lombok.SneakyThrows;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.web.multipart.MultipartFile;

  

@Slf4j

@Component

public class MinioUtils {

  

    @Autowired

    private MinioClient client;

    @Autowired

    private MinioProp minioProp;

  

    /**

     * 创建bucket

     *

     * @param bucketName bucket名称

     */

    @SneakyThrows

    public void createBucket(String bucketName) {

        if (!client.bucketExists(bucketName)) {

            client.makeBucket(bucketName);

        }

    }

  

    /**

     * 上传文件

     *

     * @param file       文件

     * @param bucketName 存储桶

     * @return

     */

    public JSONObject uploadFile(MultipartFile file, String bucketName) throws Exception {

        JSONObject res = new JSONObject();

        res.put("code", 0);

        // 判断上传文件是否为空

        if (null == file || 0 == file.getSize()) {

            res.put("msg", "上传文件不能为空");

            return res;

        }

        try {

            // 判断存储桶是否存在

            createBucket(bucketName);

            // 文件名

            String originalFilename = file.getOriginalFilename();

            // 新的文件名 = 存储桶名称_时间戳.后缀名

            String fileName = bucketName + "_" + System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));

            // 开始上传

            client.putObject(bucketName, fileName, file.getInputStream(), file.getContentType());

            res.put("code", 1);

            res.put("msg", minioProp.getEndpoint() + "/" + bucketName + "/" + fileName);

            return res;

        catch (Exception e) {

            log.error("上传文件失败:{}", e.getMessage());

        }

        res.put("msg", "上传失败");

        return res;

    }

}

6.controller接口

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

import com.alibaba.fastjson.JSONObject;

import com.zyxx.email.common.minio.MinioUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

  

import javax.servlet.http.HttpServletRequest;

  

@Controller

public class MinioController {

  

    @Autowired

    private MinioUtils minioUtils;

  

  

    /**

     * 上传

     *

     * @param file

     * @param request

     * @return

     */

    @PostMapping("/upload")

    @ResponseBody

    public String upload(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {

        JSONObject res = null;

        try {

            res = minioUtils.uploadFile(file, "product");

        } catch (Exception e) {

            e.printStackTrace();

            res.put("code", 0);

            res.put("msg", "上传失败");

        }

        return res.toJSONString();

    }

}

删除文件:

1

2

3

4

5

6

7

8

9

10

11

//文件删除

@DeleteMapping

public String delete(String name) {

    try {

        MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);

        minioClient.removeObject(BUCKETNAME, name);

    } catch (Exception e) {

        return "删除失败"+e.getMessage();

    }

    return "删除成功";

}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值