|  |  |  | 
|---|
|  |  |  | package com.ruoyi.device.hiksdk.util.minio; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.ruoyi.common.utils.StringUtils; | 
|---|
|  |  |  | import com.ruoyi.common.utils.uuid.IdUtils; | 
|---|
|  |  |  | import com.ruoyi.device.hiksdk.config.MinioClientSingleton; | 
|---|
|  |  |  | import io.minio.*; | 
|---|
|  |  |  | 
|---|
|  |  |  | public static boolean exitsBucket(String bucketName) { | 
|---|
|  |  |  | boolean found = false; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(StringUtils.isEmpty(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build(); | 
|---|
|  |  |  | found = MinioClientSingleton.getMinioClient().bucketExists(bucketExistsArgs); | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static boolean createBucket(String bucketName) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(StringUtils.isEmpty(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /*创建桶*/ | 
|---|
|  |  |  | MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder().bucket(bucketName).build(); | 
|---|
|  |  |  | MinioClientSingleton.getMinioClient().makeBucket(makeBucketArgs); | 
|---|
|  |  |  | 
|---|
|  |  |  | log.error("minio生成失效url异常", ex.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | createBucket(bucketName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return url; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @修改人和其它信息 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String putObjectAndGetUrl(String bucketName, MultipartFile file) { | 
|---|
|  |  |  | if(!exitsBucket(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | createBucket(bucketName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //判断文件是否为空 | 
|---|
|  |  |  | if (null == file || 0 == file.getSize()) { | 
|---|
|  |  |  | log.error("上传minio文件服务器错误,上传文件为空"); | 
|---|
|  |  |  | 
|---|
|  |  |  | * @修改人和其它信息 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String putObjectAndGetUrl(String bucketName, String folder, MultipartFile file) { | 
|---|
|  |  |  | if(!exitsBucket(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | createBucket(bucketName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //判断文件是否为空 | 
|---|
|  |  |  | if (null == file || 0 == file.getSize()) { | 
|---|
|  |  |  | log.error("上传minio文件服务器错误,上传文件为空"); | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static boolean uploadObject(String bucketName, String objectName, InputStream stream, long size, String contextType) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(!exitsBucket(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | createBucket(bucketName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | PutObjectArgs putObjectArgs = PutObjectArgs.builder() | 
|---|
|  |  |  | .bucket(bucketName) | 
|---|
|  |  |  | .object(objectName) | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static boolean uploadObject(String bucketName, String objectName, File file, String contextType) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(!exitsBucket(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | createBucket(bucketName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | FileInputStream fileInputStream = new FileInputStream(file); | 
|---|
|  |  |  | PutObjectArgs putObjectArgs = PutObjectArgs.builder() | 
|---|
|  |  |  | .bucket(bucketName) | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static boolean uploadObject(String bucketName, String objectName, MultipartFile multipartFile, String contextType) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (bucketName.isEmpty()) { | 
|---|
|  |  |  | log.error("bucket名称为空"); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (objectName.isEmpty()) { | 
|---|
|  |  |  | log.error("对象名称为空"); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | if(!exitsBucket(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | createBucket(bucketName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | InputStream inputStream = multipartFile.getInputStream(); | 
|---|
|  |  |  | PutObjectArgs putObjectArgs = PutObjectArgs.builder() | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static boolean uploadObject(String bucketName, MultipartFile multipartFile, String contextType) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(!exitsBucket(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | createBucket(bucketName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (multipartFile == null) { | 
|---|
|  |  |  | log.error("上传文件为空"); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param localFilePathName | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static boolean uploadObject(String bulkName, String objectName, String localFilePathName, String contextType) { | 
|---|
|  |  |  | public static boolean uploadObject(String bucketName, String objectName, String localFilePathName, String contextType) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (!exitsBucket(bulkName)) { | 
|---|
|  |  |  | log.debug(bulkName + "不存在"); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | if(!exitsBucket(bucketName)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | createBucket(bucketName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | File file = new File(localFilePathName); | 
|---|
|  |  |  | if (!file.exists()) { | 
|---|
|  |  |  | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder() | 
|---|
|  |  |  | .bucket(bulkName) | 
|---|
|  |  |  | .bucket(bucketName) | 
|---|
|  |  |  | .object(objectName) | 
|---|
|  |  |  | .filename(localFilePathName) | 
|---|
|  |  |  | .contentType(contextType) | 
|---|