springboot 整合minio client 简单使用 视频流展示demo

springboot 整合minio client 简单使用 视频流展示demo

不好意思 本人没有强迫症 代码随便造 知识来源于百度,感谢百度合作伙伴以及各位网友demo的帮助
源码:https://gitee.com/yangqinglinzp/test/tree/master/minio-test

minio 中文手册 https://gitee.com/yangqinglinzp/test/tree/master/minio-test

英文网 : https://docs.min.io/

linux 安装就三句 比较简单

wget https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
./minio server /data

这里的/data 是指所有的文件放在/data 下面 /data作为所有文件的根目录 data这个文件夹需要自己去mkdir ,请建好
``

minio 有使用到桶的概念 可以理解为一个文件夹

关于启动 我用的是下面这个后台启动
nohup ./minio server /usr/software/minio/data> …/log/minio.log 2>&1 &

在这里插入图片描述
默认端口是9000 http://ip:9000 我的是在linux上启动的
http://192.168.3.89:9000 用户名和密码都是默认: minioadmin
进入buckets 里面我建立了一个文件夹test 上传一些文件比如视频和图片
在这里插入图片描述

在这里插入图片描述

环境算是搭建好了 现在看一下官网的文档开始撸代码

官网有案例: https://docs.min.io/docs/java-client-api-reference.html
在这里插入图片描述

目前的代码是2021-12-23日的具体代码写法看自己的版本使用。

配置文件pom依赖:在这里插入代码片

    <!--minio服务--    这个版本的okhttp3有点问题需要去掉重新引入--->
    <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>8.3.3</version>
        <exclusions>
            <exclusion>
                <artifactId>okhttp</artifactId>
                <groupId>com.squareup.okhttp3</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

</dependencies>

yml配置

# 密码和用户名都是默认  没改   官网还有严格的加密  需要的去看官网
minio:
  endpoint: http://192.168.3.89:9000
  accessKey: minioadmin
  secretKey: minioadmin




server:
  port: 9005

spring:
  # 配置文件上传大小限制
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB

简单的minio 的配置类


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

/**
 * description: minio配置类
 *
 * @author: weirx
 * @time: 2021/8/25 9:47
 */

@Component
@ConfigurationProperties(prefix = "minio")
public class MinioPropertiesConfig {

    /**
     * 端点
     */
    private String endpoint;
    /**
     * 用户名
     */
    private String accessKey;
    /**
     * 密码
     */
    private String secretKey;

    /**
     * 桶名称
     */
    private String bucketName;

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }




}


minio 做成bean



import io.minio.MinioClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;


/**
 * description: 获取配置文件信息
 *
 * @author: weirx
 * @time: 2021/8/25 9:50
 */
@Configuration
@EnableConfigurationProperties(MinioPropertiesConfig.class)
public class MinioConfig {

    @Resource
    private MinioPropertiesConfig minioPropertiesConfig;


    /**
     * 初始化 MinIO 客户端
     */
    @Bean
    public MinioClient minioClient() {
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minioPropertiesConfig.getEndpoint())
                .credentials(minioPropertiesConfig.getAccessKey(), minioPropertiesConfig.getSecretKey())
                .build();
        return minioClient;
    }
}
package com.qq.minio.controller;

import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;

@RestController
public class TestMinioController {


    @Autowired
    private MinioClient minioClient;

    @RequestMapping("/upload")
    public String test(@RequestParam String  name) {

        try {
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("test").build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("test").build());
            } else {
                System.out.println("Bucket 'test' already exists.");
            }

       
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("test")
                            .object(name)
                            .filename("E:/video/"+name)
                            .build());
         
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);

        }
        return "success";
    }

    //返回桶列表

    @RequestMapping("/test2")
    public String  test2(){

        try {
            // 列出所有存储桶
            List<Bucket> bucketList = null;
            try {
                bucketList = minioClient.listBuckets();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            for (Bucket bucket : bucketList) {
                System.out.println(bucket.creationDate() + ", " + bucket.name());
            }
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
        return "success";
    }


    @RequestMapping("/show")
    public void download(HttpServletResponse  response, @RequestParam String voideName){

        System.out.println(voideName);

        try {
            // 调用statObject()来判断对象是否存在。
            // 如果不存在, statObject()抛出异常,
            // 否则则代表对象存在。
            StatObjectResponse statObjectResponse = minioClient.statObject(
                    StatObjectArgs.builder().bucket("test").object("orange.mp4").build());

            // 获取"myobject"的输入流。
            // get object given the bucket and object name
            InputStream stream = minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket("test")
                            .object(voideName)

                            .build());




            //流转换
            IOUtils.copy(stream,response.getOutputStream());

            //设置返回类型
            response.addHeader("Content-Type", "audio/mpeg;charset=utf-8");
         //这里注释掉  要不然会报错
//            response.flushBuffer();

//            // 关闭流,此处为示例,流关闭最好放在finally块。
            stream.close();
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }finally {

        }
//
//        return "success";
    }




    @RequestMapping("/remove")
    public void remove(@RequestParam String bucketName, @RequestParam String filedName){




        try {

            List<Bucket> buckets = minioClient.listBuckets();

            for (Bucket bucket : buckets) {

                if (bucket.name().equals("test")){
                    continue;
                }
                minioClient.removeBucket( RemoveBucketArgs.builder().bucket(bucket.name()).build());

                System.out.println("delete the bucket:"+bucket.name()+" is success!! ");

            }
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }


    }


}



上述代码是一个小栗子,主要是测测服务器转为视频流播放到前端以及上下传文件。

这里贴一下前端的html 直接是html访问 比较简单 直接打开即可

<!DOCTYPE HTML>
<html>
<body>




<video width="1120" height="540" controls="controls" id="video" preload="auto"     >
    <source src="http://localhost:9005/show?voideName=orange.mp4"   type="video/mp4">
</video>

</body>
</html>

minio服务器上要有这些视频 通过服务器走minio客户端直接读视频流返回给html

在这里插入图片描述

这里已经完成视频从Linux服务器下载到后台服务器(具体看方法,上传速度还是很不错的),html去读服务器上的视频流。

minio的视频位置图片

在这里插入图片描述

### ADS127L11与STM32驱动程序实现方法 #### 一、硬件连接说明 为了使ADS127L11能够正常工作并与STM32进行通信,需按照特定的方式连接两者之间的信号线。通常情况下,SPI接口用于此目的,具体连线包括但不限于CS(片选)、DIN(数据输入)、DOUT(数据输出)以及SCLK(时钟)。此外,还需要注意电源和地线的正确连接。 #### 二、软件初始化设置 针对STM32平台,在启动阶段应对SPI外设完成必要的参数设定,比如波特率、模式选择等。这部分可通过STM32CubeMX工具自动生成基础代码框架,之后再根据实际需求调整细节部分[^3]。 ```c // 初始化SPI配置结构体并调用HAL库函数完成初始化过程 static void MX_SPI1_Init(void) { hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_MASTER; // 设置为主设备模式 hspi1.Init.Direction = SPI_DIRECTION_2LINES; // 双向传输方向 hspi1.Init.DataSize = SPI_DATASIZE_8BIT; // 数据大小为8位 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; // SCLK空闲状态低电平 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; // 第一个边沿采样 hspi1.Init.NSS = SPI_NSS_SOFT; // 软件控制NSS引脚 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; // 波特率预分频系数 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; // MSB先发送/接收 HAL_SPI_Init(&hspi1); } ``` #### 三、读取ADC转换结果 当准备就绪后,可以编写专门的功能函数用来获取来自ADS127L11的数据帧,并将其解析成有意义的信息形式——即电压值。考虑到该器件支持高速连续测量特性,建议采用DMA机制优化性能表现,减少CPU占用时间。 ```c uint32_t Read_ADC_Value(void){ uint8_t rx_buffer[3]; // 创建缓冲区存储接收到的数据 /* 启动一次完整的单通道读周期 */ HAL_GPIO_WritePin(CS_PORT, CS_PIN, GPIO_PIN_RESET); // 拉低CS激活从机 HAL_SPI_TransmitReceive_DMA(&hspi1, NULL, rx_buffer, 3); // 使用DMA方式收发数据包 while (__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_TXE) != RESET){} // 等待直到发送完毕 HAL_GPIO_WritePin(CS_PORT, CS_PIN, GPIO_PIN_SET); // 结束通讯释放总线使用权 /* 将三个字节拼合成最终的结果 */ return ((rx_buffer[0]<<16)|(rx_buffer[1]<<8)|rx_buffer[2]); } float Convert_to_Voltage(uint32_t raw_data){ const float VREF = 2.5f; // 设定参考电压值 return (raw_data * VREF / pow(2, 24)); // 计算真实物理量级表示法下的电压数值 } ``` #### 四、处理流程概述 整个系统的运作逻辑大致如下:首先由主机发出指令触发ADC内部开始新一轮的模数变换动作;随后等待一段时间让其充分稳定下来;接着利用上述提到的方法提取最新产生的样本点;最后经过简单的数学运算得到对应的模拟电信号强度描述。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值