溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載

發(fā)布時(shí)間:2023-05-10 14:48:47 來源:億速云 閱讀:203 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載”文章能幫助大家解決問題。

    1. docker安裝minio步驟

    第一步 查鏡像

    docker search minio

    第二步 拉鏡像

    docker pull minio/minio

    第三步 啟動(dòng)容器

    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

    第四步 登錄界面

    • http//:ip+9000

    • ACCESS_KEY:damin

    • SECRET_KEY:admin123456

    docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載

    2. minio實(shí)現(xiàn)文件上傳、刪除、下載

    項(xiàng)目結(jié)構(gòu)

    docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載

    pom依賴:

    <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>2.1.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>io.minio</groupId>
                <artifactId>minio</artifactId>
                <version>3.0.10</version>
            </dependency>
        </dependencies>

    啟動(dòng)類:

    @SpringBootApplication
    @EnableDiscoveryClient
    public class CfUploadApplication {
        public static void main(String[] args) {
            SpringApplication.run(CfUploadApplication.class,args);
        }
    }

    yml配置文件:

    server:
      port: 8002
    spring:
      application:
        name: upload-service
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848
      servlet:
        multipart:
          enabled: true #開啟文件上傳
          max-file-size: 500MB
          max-request-size: 500MB
    minio:
      endpoint: http://localhost:9000 #Minio服務(wù)所在地址
      bucketName: cheung #存儲(chǔ)桶名稱
      accessKey: admin #訪問的key
      secretKey: admin123456 #訪問的秘鑰
    
    logging:
      level:
        com.heima: debug

    controller代碼:

    package com.cheung.upload.controller;
    
    import io.minio.MinioClient;
    import io.minio.policy.PolicyType;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URLEncoder;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @RestController
    @RequestMapping("file")
    public class UploadController {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);
    
        @Value("${minio.endpoint}")
        private String ENDPOINT;
        @Value("${minio.bucketName}")
        private String BUCKETNAME;
        @Value("${minio.accessKey}")
        private String ACCESSKEY;
        @Value("${minio.secretKey}")
        private String SECRETKEY;
    
        //文件創(chuàng)建
        @PostMapping("/upload")
        public String upload(MultipartFile file) {
            String s = null;
            try {
                MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);
                //存入bucket不存在則創(chuàng)建,并設(shè)置為只讀
                if (!minioClient.bucketExists(BUCKETNAME)) {
                    minioClient.makeBucket(BUCKETNAME);
                    minioClient.setBucketPolicy(BUCKETNAME, "*.*", PolicyType.READ_ONLY);
                }
                String filename = file.getOriginalFilename();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                // 文件存儲(chǔ)的目錄結(jié)構(gòu)
                String objectName = sdf.format(new Date()) + "/" + filename;
                // 存儲(chǔ)文件
                minioClient.putObject(BUCKETNAME, objectName, file.getInputStream(), file.getContentType());
                LOGGER.info("文件上傳成功!");
                s = ENDPOINT + "/" + BUCKETNAME + "/" + objectName;
            } catch (Exception e) {
                LOGGER.info("上傳發(fā)生錯(cuò)誤: {}!", e.getMessage());
            }
            return s;
        }
    
        //文件刪除
        @DeleteMapping("/delete")
        public String delete(String name) {
            try {
                MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);
                minioClient.removeObject(BUCKETNAME, name);
            } catch (Exception e) {
                return "刪除失敗" + e.getMessage();
            }
            return "刪除成功";
        }
    
    
        //文件下載
        @GetMapping("/download")
        public void downloadFiles(@RequestParam("filename") String filename, HttpServletResponse httpResponse) {
    
            try {
    
                MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);
                InputStream object = minioClient.getObject(BUCKETNAME, filename);
                byte buf[] = new byte[1024];
                int length = 0;
                httpResponse.reset();
    
                httpResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
                httpResponse.setContentType("application/octet-stream");
                httpResponse.setCharacterEncoding("utf-8");
                OutputStream outputStream = httpResponse.getOutputStream();
                while ((length = object.read(buf)) > 0) {
                    outputStream.write(buf, 0, length);
                }
                outputStream.close();
            } catch (Exception ex) {
                LOGGER.info("導(dǎo)出失?。?quot;, ex.getMessage());
            }
        }
    
    }

    上傳文件

    使用postman進(jìn)行測試

    docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載

    docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載

    刪除文件

    docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載

    下載文件

    docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載

    關(guān)于“docker怎么安裝minio及實(shí)現(xiàn)文件上傳、刪除、下載”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI