编程语言
首页 > 编程语言> > Java集成MinIO

Java集成MinIO

作者:互联网

官方文档地址:https://docs.min.io/cn/java-client-api-reference.html#setBucketPolicy

1.pom依赖

        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>3.0.10</version>
        </dependency>

2.代码测试

    /**
     * 获取文件地址
     *
     * @param id
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping(value = "/file")
    public CommonResult<String> uploadDepartmentFile(@RequestParam("ip") String ip,
                                                     @RequestParam("id") Integer id,
                                                     @RequestParam("startTime") Long startTime,
                                                     @RequestParam("endTime") Long endTime) {
        HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
        //IP参数
        HCNetSDK.NET_DVR_IPPARACFG m_strIpparaCfg;

        //进行初始化
        boolean initSuc = hCNetSDK.NET_DVR_Init();
        if (initSuc != true) {
            log.error("初始化失败");
            return new CommonResult<>(-2, "初始化失败", null);
        }
        log.info("初始化成功");

        //注册
        //设备ip地址
        String m_sDeviceIP = ip;
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        int iPort = port;
        NativeLong lUserID = hCNetSDK.NET_DVR_Login_V30(m_sDeviceIP,
                (short) iPort, userName, password, m_strDeviceInfo);

        //获取userId
        long userID = lUserID.longValue();
        if (userID == -1) {
            log.error("注册失败");
            return new CommonResult<>(-3, "注册失败", null);
        }
        log.info("注册成功");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String[] startTimes = simpleDateFormat.format(new Date(startTime)).split("-");
        String[] endTimes = simpleDateFormat.format(new Date(endTime)).split("-");

        //设置开始结束时间
        HCNetSDK.NET_DVR_TIME struStartTime;
        HCNetSDK.NET_DVR_TIME struStopTime;

        struStartTime = new HCNetSDK.NET_DVR_TIME();
        struStopTime = new HCNetSDK.NET_DVR_TIME();
        //开始时间
        struStartTime.dwYear = Integer.parseInt(startTimes[0]);
        struStartTime.dwMonth = Integer.parseInt(startTimes[1]);
        struStartTime.dwDay = Integer.parseInt(startTimes[2]);
        struStartTime.dwHour = Integer.parseInt(startTimes[3]);
        struStartTime.dwMinute = Integer.parseInt(startTimes[4]);
        struStartTime.dwSecond = Integer.parseInt(startTimes[5]);
        //结束时间
        struStopTime.dwYear = Integer.parseInt(endTimes[0]);
        struStopTime.dwMonth = Integer.parseInt(endTimes[1]);
        struStopTime.dwDay = Integer.parseInt(endTimes[2]);
        struStopTime.dwHour = Integer.parseInt(endTimes[3]);
        struStopTime.dwMinute = Integer.parseInt(endTimes[4]);
        struStopTime.dwSecond = Integer.parseInt(endTimes[5]);

        //获取通道号
        Integer m_iChanShowNum = id;
        String ipNumber = getIpNumber(ip);

        //设置文件名下载路径
        String sFileName = ipNumber + "_" + m_iChanShowNum + "_" + startTime + "_" + endTime + ".mp4";
        System.out.println(sFileName + "  --- " + simpleDateFormat.format(new Date(startTime)) + "  --- " + simpleDateFormat.format(new Date(endTime)));
        //下载
        NativeLong m_lLoadHandle = hCNetSDK.NET_DVR_GetFileByTime(lUserID, new NativeLong(m_iChanShowNum), struStartTime, struStopTime, sFileName);
        if (m_lLoadHandle.intValue() >= 0) {
            hCNetSDK.NET_DVR_PlayBackControl(m_lLoadHandle, HCNetSDK.NET_DVR_PLAYSTART, 0, null);
            while (true) {
                try {
                    IntByReference nPos = new IntByReference(0);
                    hCNetSDK.NET_DVR_PlayBackControl(m_lLoadHandle, HCNetSDK.NET_DVR_PLAYGETPOS, 0, nPos);
                    if (nPos.getValue() > 100) {
                        hCNetSDK.NET_DVR_StopGetFile(m_lLoadHandle);
                        m_lLoadHandle.setValue(-1);
                        break;
                    }
                    if (nPos.getValue() == 100) {
                        hCNetSDK.NET_DVR_StopGetFile(m_lLoadHandle);
                        m_lLoadHandle.setValue(-1);
                        break;
                    }
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    log.error("下载错误", e);
                    e.printStackTrace();
                    break;
                } catch (Exception e) {
                    log.error("下载错误", e);
                    break;
                }
            }
//            Timer timer = new Timer();//新建定时器
//            timer.schedule(new DownloadTask(hCNetSDK, m_lLoadHandle, timer), 0, 5000);//0秒后开始响应函数
        } else {
            return new CommonResult<>(-1, "下载失败", null);
        }

        try {
            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(minioIp, minioAccessKey, minioSecretKey);

            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists(ipNumber);
            if (!isExist) {
                // 创建一个名为asiatrip的存储桶,用于存储照片的zip文件。
                minioClient.makeBucket(ipNumber);
                try {
                    minioClient.setBucketPolicy(ipNumber, "*", PolicyType.READ_WRITE);
                } catch (InvalidObjectPrefixException e) {
                    e.printStackTrace();
                }
            }

            // 使用putObject上传一个文件到存储桶中。
            String objectName = "/" + m_iChanShowNum + "/" + startTimes[0] + "/" + startTimes[1] + "/" + startTimes[2] + "/" + sFileName;
            minioClient.putObject(ipNumber, objectName, new FileInputStream(sFileName), "video/mp4");
            String url = minioClient.getObjectUrl(ipNumber, objectName);
            System.out.println(url);
            File file = new File(sFileName);
            if (file.exists()) {
                file.delete();
            }
            return new CommonResult<>(url);
        } catch (InvalidEndpointException e) {
            e.printStackTrace();
        } catch (InvalidPortException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoResponseException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (InvalidArgumentException e) {
            e.printStackTrace();
        } catch (RegionConflictException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return new CommonResult<>(sFileName);
    }

3.测试

标签:集成,Java,MinIO,printStackTrace,Integer,catch,new,NET,DVR
来源: https://blog.csdn.net/qq_37598011/article/details/110424423