当前位置:   article > 正文

spring boot整合minio,7版本和8版本的_minio依赖版本号

minio依赖版本号

1、添加依赖
1.1、7版本的

<dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>7.0.2</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
注:2.7版本要添加如下依赖,一定要放在minio依赖的前面
  • 1
<dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.0</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

1.2、8版本则用如下依赖

<dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>8.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>com.squareup.okhttp3</groupId>
                    <artifactId>okhttp</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.11.0</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2、配置文件连接minio配置

minio.url=http://localhost:9000
minio.accessKey=minioadmin
minio.secretKey=minioadmin
minio.bucketName=xxxxx
  • 1
  • 2
  • 3
  • 4

3、minio配置类
7版本的如下

package com.iot.zmtestboot.minio;

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Component
@RequiredArgsConstructor
public class MinioTemplate implements InitializingBean {
    /**
     * minio地址+端口号
     */
    @Value("${minio.url}")
    private  String url;
    /**
     * minio用户名
     */
    @Value("${minio.accessKey}")
    private  String accessKey;
    /**
     * minio密码
     */
    @Value("${minio.secretKey}")
    private  String secretKey;
    /**
     * 文件桶的名称
     */
    @Value("${minio.bucketName}")
    private String bucketName;

    private MinioClient minioClient;

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.hasText(url, "Minio url 为空");
        Assert.hasText(accessKey, "Minio accessKey为空");
        Assert.hasText(secretKey, "Minio secretKey为空");
        this.minioClient = new MinioClient(url, accessKey, secretKey);
    }

    /**
     * 创建bucket
     *
     * @param bucketName bucket名称
     */
    @SneakyThrows
    public void createBucket(String bucketName) {
        if (!minioClient.bucketExists(bucketName)) {
            minioClient.makeBucket(bucketName);
        }
    }

    /**
     * 获取全部bucket
     * <p>
     * https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
     */
    @SneakyThrows
    public List<Bucket> getAllBuckets() {
        return minioClient.listBuckets();
    }

    /**
     * 根据bucketName获取信息
     *
     * @param bucketName bucket名称
     */
    @SneakyThrows
    public Optional<Bucket> getBucket(String bucketName) {
        return minioClient.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
    }

    /**
     * 根据bucketName删除信息
     *
     * @param bucketName bucket名称
     */
    @SneakyThrows
    public void removeBucket(String bucketName) {
        minioClient.removeBucket(bucketName);
    }

    /**
     * 根据文件前置查询文件
     *
     * @param bucketName bucket名称
     * @param prefix     前缀
     * @param recursive  是否递归查询
     * @return MinioItem 列表
     */
    @SneakyThrows
    public List getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
        List<Item> list = new ArrayList<>();
        Iterable<Result<Item>> objectsIterator = minioClient.listObjects(bucketName, prefix, recursive);
        if (objectsIterator != null) {
            Iterator<Result<Item>> iterator = objectsIterator.iterator();
            if (iterator != null) {
                while (iterator.hasNext()) {
                    Result<Item> result = iterator.next();
                    Item item = result.get();
                    list.add(item);
                }
            }
        }

        return list;
    }

    /**
     * 获取文件外链
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @param expires    过期时间 <=7
     * @return url
     */
    @SneakyThrows
    public String getObjectURL(String bucketName, String objectName, Integer expires) {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        return minioClient.presignedGetObject(bucketName, objectName, expires);
//        return minioClient.presignedGetObject(bucketName, objectName);
    }

//    /**
//     * 获取文件路径
//     * @param bucketName
//     * @param fileName
//     * @return
//     */
//    @SneakyThrows
//    public String getObjectLocal(String bucketName, String fileName) {
//        if (bucketName!="") {
//            bucketName = this.bucketName;
//        }
//        return minioClient.getObjectUrl(bucketName, fileName);
//    }



    /**
     * 获取文件
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @return 二进制流
     */
    @SneakyThrows
    public InputStream getObject(String bucketName, String objectName) {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        return minioClient.getObject(bucketName, objectName);
    }

    /**
     * 获取文件
     * @param bucketName
     * @param objectName
     * @return
     */
    @SneakyThrows
    public ObjectStat statObject(String bucketName, String objectName) {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        return minioClient.statObject(bucketName, objectName);
    }

    /**
     * 上传文件
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @param stream     文件流
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
     */
    public void putObject(String bucketName, String objectName,InputStream stream,int length,String suffix) throws Exception {
        String contentType="application/octet-stream";
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        //判断桶是否存在
        boolean isExist = minioClient.bucketExists(bucketName);
        if(isExist) {
            System.out.println("Bucket already exists.");
        } else {
            // 创建一个名为asiatrip的存储桶,用于存储照片的zip文件。
            createBucket(bucketName);
        }
        if(suffix.equals("jpg") || suffix.equals("JPG")){
            contentType="image/jpg";
        }
        else if(suffix.equals("png") || suffix.equals("PNG")){
            contentType="image/png";
        }
        else if(suffix.equals("jpeg") || suffix.equals("JPEG")){
            contentType="image/jpeg";
        }
        else if(suffix.equals("svg") || suffix.equals("SVG")){
            contentType="image/svg";
        }
        else if(suffix.equals("gif") || suffix.equals("GIF")){
            contentType="image/gif";
        }
        PutObjectOptions filePutObjectOptions= new PutObjectOptions(stream.available(), length);
        filePutObjectOptions.setContentType(contentType);
        minioClient.putObject(bucketName, objectName, stream, filePutObjectOptions);
    }

    /**
     * 获取文件信息, 如果抛出异常则说明文件不存在
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
     */
    public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        return minioClient.statObject(bucketName, objectName);
    }

    /**
     * 删除文件
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
     */
    public void removeObject(String bucketName, String objectName) throws Exception {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        minioClient.removeObject(bucketName, objectName);
    }
}

  • 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
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255

如果是8版本的

package com.example.answer_system.config;

import com.example.answer_system.utils.BusinessException;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
@Slf4j
public class MinioTemplate implements InitializingBean {
    /**
     * minio地址+端口号
     */
    @Value("${minio.url}")
    private  String url;
    /**
     * minio用户名
     */
    @Value("${minio.accessKey}")
    private  String accessKey;
    /**
     * minio密码
     */
    @Value("${minio.secretKey}")
    private  String secretKey;
    /**
     * 文件桶的名称
     */
    @Value("${minio.bucketName}")
    private String bucketName;

    private MinioClient minioClient;

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.hasText(url, "Minio url 为空");
        Assert.hasText(accessKey, "Minio accessKey为空");
        Assert.hasText(secretKey, "Minio secretKey为空");
//        this.minioClient = new MinioClient(url, accessKey, secretKey);
        this.minioClient = MinioClient.builder().endpoint(url).credentials(accessKey,secretKey).build();
    }

    /**
     * 创建bucket
     *
     * @param bucketName bucket名称
     */
    @SneakyThrows
    public void createBucket(String bucketName) {
//        if (!minioClient.bucketExists(bucketName)) {
//            minioClient.makeBucket(bucketName);
//        }
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 获取全部bucket
     * <p>
     * https://docs.minio.io/cn/java-client-api-reference.html#listBuckets
     */
    @SneakyThrows
    public List<Bucket> getAllBuckets() {
        return minioClient.listBuckets();
    }

    /**
     * 根据bucketName获取信息
     *
     * @param bucketName bucket名称
     */
    @SneakyThrows
    public Optional<Bucket> getBucket(String bucketName) {
        return minioClient.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
    }

    /**
     * 根据bucketName删除信息
     *
     * @param bucketName bucket名称
     */
    @SneakyThrows
    public void removeBucket(String bucketName) {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 根据文件前置查询文件
     *
     * @param bucketName bucket名称
     * @param prefix     前缀
     * @param recursive  是否递归查询
     * @return MinioItem 列表
     */
    @SneakyThrows
    public List getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
        List<Item> list = new ArrayList<>();
        ListObjectsArgs listObjectsArgs=
                ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build();
        Iterable<Result<Item>> objectsIterator = minioClient.listObjects(listObjectsArgs);
//        Iterable<Result<Item>> objectsIterator = minioClient.listObjects(bucketName, prefix, recursive);
        if (objectsIterator != null) {
            Iterator<Result<Item>> iterator = objectsIterator.iterator();
            if (iterator != null) {
                while (iterator.hasNext()) {
                    Result<Item> result = iterator.next();
                    Item item = result.get();
                    list.add(item);
                }
            }
        }

        return list;
    }

    /**
     * 获取文件外链
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @param expires    过期时间 <=7
     * @return url
     */
    @SneakyThrows
    public String getObjectURL(String bucketName, String objectName, Integer expires) {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        GetPresignedObjectUrlArgs getPresignedObjectUrlArgs=
                GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(objectName).expiry(expires).build();
        return minioClient.getPresignedObjectUrl(getPresignedObjectUrlArgs);
        //return minioClient.presignedGetObject(bucketName, objectName, expires);
//        return minioClient.presignedGetObject(bucketName, objectName);
    }

//    /**
//     * 获取文件路径
//     * @param bucketName
//     * @param fileName
//     * @return
//     */
//    @SneakyThrows
//    public String getObjectLocal(String bucketName, String fileName) {
//        if (bucketName!="") {
//            bucketName = this.bucketName;
//        }
//        return minioClient.getObjectUrl(bucketName, fileName);
//    }



    /**
     * 获取文件
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @return 二进制流
     */
    @SneakyThrows
    public InputStream getObject(String bucketName, String objectName) {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
        //return minioClient.getObject(bucketName, objectName);
    }

    /**
     * 获取文件
     * @param bucketName
     * @param objectName
     * @return
     */
    @SneakyThrows
    public StatObjectResponse statObject(String bucketName, String objectName) {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
//        return minioClient.statObject(bucketName, objectName);
    }

    /**
     * 上传文件
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @param stream     文件流
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
     */
    public void putObject(String bucketName, String objectName,InputStream stream,int length,String suffix) throws Exception {
        String contentType="application/octet-stream";
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        //判断桶是否存在
        boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if(isExist) {
            System.out.println("Bucket already exists.");
        } else {
            // 创建一个名为asiatrip的存储桶,用于存储照片的zip文件。
            createBucket(bucketName);
        }
        if(suffix.equals("jpg") || suffix.equals("JPG")){
            contentType="image/jpg";
        }
        else if(suffix.equals("png") || suffix.equals("PNG")){
            contentType="image/png";
        }
        else if(suffix.equals("jpeg") || suffix.equals("JPEG")){
            contentType="image/jpeg";
        }
        else if(suffix.equals("svg") || suffix.equals("SVG")){
            contentType="image/svg";
        }
        else if(suffix.equals("gif") || suffix.equals("GIF")){
            contentType="image/gif";
        }
//        PutObjectOptions filePutObjectOptions= new PutObjectOptions(stream.available(), length);
//        filePutObjectOptions.setContentType(contentType);
        PutObjectArgs putObjectArgs=PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(stream,stream.available(), length)
                .contentType(contentType).build();
        minioClient.putObject(putObjectArgs);
//        minioClient.putObject(bucketName, objectName, stream, filePutObjectOptions);
    }

    /**
     * 获取文件信息, 如果抛出异常则说明文件不存在
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
     */
    public StatObjectResponse getObjectInfo(String bucketName, String objectName) throws Exception {
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
        //return minioClient.statObject(bucketName, objectName);
    }

    /**
     * 删除文件
     *
     * @param bucketName bucket名称
     * @param objectName 文件名称
     * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#removeObject
     */
    public void removeObject(String bucketName, String objectName){
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        try {
            minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
//            minioClient.removeObject(bucketName, objectName);
        }catch (Exception ex){
            ex.printStackTrace();
            throw new BusinessException("删除失败");
        }
    }

    public void removeObjects(String bucketName, List<String> objectNames){
        if (bucketName.equals("")) {
            bucketName = this.bucketName;
        }
        try {
            //要删除的对象
            List<DeleteObject> deleteObjects=objectNames.stream().map(v->new DeleteObject(v)).collect(Collectors.toList());
            Iterable<Result<DeleteError>> results=minioClient.removeObjects(
                    RemoveObjectsArgs.builder()
                            .bucket(bucketName)
                            .objects(deleteObjects)
                            .build()
            );
            for (Result<DeleteError> result : results) {
                DeleteError error = result.get();
                log.warn("Error in deleting object " + error.objectName() + "; " + error.message());
            }
        }catch (Exception ex){
            ex.printStackTrace();
            throw new BusinessException("删除失败");
        }

    }

}

  • 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
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306

4、controller编写

package com.iot.zmtestboot.controller;

import com.google.common.io.CharStreams;
import com.iot.zmtestboot.minio.MinioTemplate;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@CrossOrigin
@RequestMapping("/iot")
public class MinioController {
    @Autowired
    private MinioTemplate minioTemplate;

    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("上传文件");
        if (file.isEmpty()) {
            return "上传文件不能为空";
        } else {
            // 得到文件流
            InputStream is = file.getInputStream();
            // 文件名
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            // 把文件放到minio的boots桶里面
            minioTemplate.putObject("",fileName,is,-1,suffix);
            // 关闭输入流
            is.close();
            return "上传成功!";
        }
    }

    @ApiOperation(value = "删除文件")
    @DeleteMapping(value = "/delete")
    @SneakyThrows(Exception.class)
    public String delete(@RequestParam("fileName") String fileName) {
        minioTemplate.removeObject("",fileName);
        return "删除成功";
    }

    @ApiOperation(value = "获取文件url预览")
    @GetMapping(value = "/getFileUrl")
    public String getFileUrl(String objectName){
        return minioTemplate.getObjectURL("",objectName,60 * 60 * 24);
    }


    @ApiOperation(value = "获取文件流")
    @GetMapping(value = "/uploadFile")
    public byte[] uploadFile(String objectName) throws IOException {
        InputStream uploadFile=minioTemplate.getObject("",objectName);
        //流转字节码数组
        //byte[] bytes = uploadFile.readAllBytes();\
        //return bytes;
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        while ((nRead = uploadFile.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        return buffer.toByteArray();

    }
}

  • 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
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

5、如果有文件需要在线预览,需要设置contentType

{
  ".*": "application/octet-stream",
  ".001": "application/x-001",
  ".301": "application/x-301",
  ".323": "text/h323",
  ".906": "application/x-906",
  ".907": "drawing/907",
  ".a11": "application/x-a11",
  ".acp": "audio/x-mei-aac",
  ".ai": "application/postscript",
  ".aif": "audio/aiff",
  ".aifc": "audio/aiff",
  ".aiff": "audio/aiff",
  ".anv": "application/x-anv",
  ".asa": "text/asa",
  ".asf": "video/x-ms-asf",
  ".asp": "text/asp",
  ".asx": "video/x-ms-asf",
  ".au": "audio/basic",
  ".avi": "video/avi",
  ".awf": "application/vnd.adobe.workflow",
  ".biz": "text/xml",
  ".bmp": "application/x-bmp",
  ".bot": "application/x-bot",
  ".c4t": "application/x-c4t",
  ".c90": "application/x-c90",
  ".cal": "application/x-cals",
  ".cat": "application/s-pki.seccat",
  ".cdf": "application/x-netcdf",
  ".cdr": "application/x-cdr",
  ".cel": "application/x-cel",
  ".cer": "application/x-x509-ca-cert",
  ".cg4": "application/x-g4",
  ".cgm": "application/x-cgm",
  ".cit": "application/x-cit",
  ".class": "java/*",
  ".cml": "text/xml",
  ".cmp": "application/x-cmp",
  ".cmx": "application/x-cmx",
  ".cot": "application/x-cot",
  ".crl": "application/pkix-crl",
  ".crt": "application/x-x509-ca-cert",
  ".csi": "application/x-csi",
  ".css": "text/css",
  ".cut": "application/x-cut",
  ".dbf": "application/x-dbf",
  ".dbm": "application/x-dbm",
  ".dbx": "application/x-dbx",
  ".dcd": "text/xml",
  ".dcx": "application/x-dcx",
  ".der": "application/x-x509-ca-cert",
  ".dgn": "application/x-dgn",
  ".dib": "application/x-dib",
  ".dll": "application/x-msdownload",
  ".doc": "application/msword",
  ".dot": "application/msword",
  ".drw": "application/x-drw",
  ".dtd": "text/xml",
  ".dwf": "Model/vnd.dwf",
  ".dwg": "application/x-dwg",
  ".dxb": "application/x-dxb",
  ".dxf": "application/x-dxf",
  ".edn": "application/vnd.adobe.edn",
  ".emf": "application/x-emf",
  ".eml": "message/rfc822",
  ".ent": "text/xml",
  ".epi": "application/x-epi",
  ".eps": "application/x-ps",
  ".etd": "application/x-ebx",
  ".exe": "application/x-msdownload",
  ".fax": "image/fax",
  ".fdf": "application/vnd.fdf",
  ".fif": "application/fractals",
  ".fo": "text/xml",
  ".frm": "application/x-frm",
  ".g4": "application/x-g4",
  ".gbr": "application/x-gbr",
  ".gcd": "application/x-gcd",
  ".gif": "image/gif",
  ".gl2": "application/x-gl2",
  ".gp4": "application/x-gp4",
  ".hgl": "application/x-hgl",
  ".hmr": "application/x-hmr",
  ".hpg": "application/x-hpgl",
  ".hpl": "application/x-hpl",
  ".hqx": "application/mac-binhex40",
  ".hrf": "application/x-hrf",
  ".hta": "application/hta",
  ".htc": "text/x-component",
  ".htm": "text/html",
  ".html": "text/html",
  ".htt": "text/webviewhtml",
  ".htx": "text/html",
  ".icb": "application/x-icb",
  ".ico": "image/x-icon",
  ".iff": "application/x-iff",
  ".ig4": "application/x-g4",
  ".igs": "application/x-igs",
  ".iii": "application/x-iphone",
  ".img": "application/x-img",
  ".ins": "application/x-internet-signup",
  ".isp": "application/x-internet-signup",
  ".IVF": "video/x-ivf",
  ".java": "java/*",
  ".jfif": "image/jpeg",
  ".jpe": "image/jpeg",
  ".jpeg": "image/jpeg",
  ".jpg": "image/jpeg",
  ".js": "application/x-javascript",
  ".jsp": "text/html",
  ".la1": "audio/x-liquid-file",
  ".lar": "application/x-laplayer-reg",
  ".latex": "application/x-latex",
  ".lavs": "audio/x-liquid-secure",
  ".lbm": "application/x-lbm",
  ".lmsff": "audio/x-la-lms",
  ".ls": "application/x-javascript",
  ".ltr": "application/x-ltr",
  ".m1v": "video/x-mpeg",
  ".m2v": "video/x-mpeg",
  ".m3u": "audio/mpegurl",
  ".m4e": "video/mpeg4",
  ".mac": "application/x-mac",
  ".man": "application/x-troff-man",
  ".math": "text/xml",
  ".mdb": "application/x-mdb",
  ".mfp": "application/x-shockwave-flash",
  ".mht": "message/rfc822",
  ".mhtml": "message/rfc822",
  ".mi": "application/x-mi",
  ".mid": "audio/mid",
  ".midi": "audio/mid",
  ".mil": "application/x-mil",
  ".mml": "text/xml",
  ".mnd": "audio/x-musicnet-download",
  ".mns": "audio/x-musicnet-stream",
  ".mocha": "application/x-javascript",
  ".movie": "video/x-sgi-movie",
  ".mp1": "audio/mp1",
  ".mp2": "audio/mp2",
  ".mp2v": "video/mpeg",
  ".mp3": "audio/mp3",
  ".mp4": "video/mp4",
  ".mpa": "video/x-mpg",
  ".mpd": "application/-project",
  ".mpe": "video/x-mpeg",
  ".mpeg": "video/mpg",
  ".mpg": "video/mpg",
  ".mpga": "audio/rn-mpeg",
  ".mpp": "application/-project",
  ".mps": "video/x-mpeg",
  ".mpt": "application/-project",
  ".mpv": "video/mpg",
  ".mpv2": "video/mpeg",
  ".mpw": "application/s-project",
  ".mpx": "application/-project",
  ".mtx": "text/xml",
  ".mxp": "application/x-mmxp",
  ".net": "image/pnetvue",
  ".nrf": "application/x-nrf",
  ".nws": "message/rfc822",
  ".odc": "text/x-ms-odc",
  ".out": "application/x-out",
  ".p10": "application/pkcs10",
  ".p12": "application/x-pkcs12",
  ".p7b": "application/x-pkcs7-certificates",
  ".p7c": "application/pkcs7-mime",
  ".p7m": "application/pkcs7-mime",
  ".p7r": "application/x-pkcs7-certreqresp",
  ".p7s": "application/pkcs7-signature",
  ".pc5": "application/x-pc5",
  ".pci": "application/x-pci",
  ".pcl": "application/x-pcl",
  ".pcx": "application/x-pcx",
  ".pdf": "application/pdf",
  ".pdx": "application/vnd.adobe.pdx",
  ".pfx": "application/x-pkcs12",
  ".pgl": "application/x-pgl",
  ".pic": "application/x-pic",
  ".pko": "application-pki.pko",
  ".pl": "application/x-perl",
  ".plg": "text/html",
  ".pls": "audio/scpls",
  ".plt": "application/x-plt",
  ".png": "image/png",
  ".pot": "applications-powerpoint",
  ".ppa": "application/vs-powerpoint",
  ".ppm": "application/x-ppm",
  ".pps": "application-powerpoint",
  ".ppt": "applications-powerpoint",
  ".pr": "application/x-pr",
  ".prf": "application/pics-rules",
  ".prn": "application/x-prn",
  ".prt": "application/x-prt",
  ".ps": "application/postscript",
  ".ptn": "application/x-ptn",
  ".pwz": "application/powerpoint",
  ".r3t": "text/vnd.rn-realtext3d",
  ".ra": "audio/vnd.rn-realaudio",
  ".ram": "audio/x-pn-realaudio",
  ".ras": "application/x-ras",
  ".rat": "application/rat-file",
  ".rdf": "text/xml",
  ".rec": "application/vnd.rn-recording",
  ".red": "application/x-red",
  ".rgb": "application/x-rgb",
  ".rjs": "application/vnd.rn-realsystem-rjs",
  ".rjt": "application/vnd.rn-realsystem-rjt",
  ".rlc": "application/x-rlc",
  ".rle": "application/x-rle",
  ".rm": "application/vnd.rn-realmedia",
  ".rmf": "application/vnd.adobe.rmf",
  ".rmi": "audio/mid",
  ".rmj": "application/vnd.rn-realsystem-rmj",
  ".rmm": "audio/x-pn-realaudio",
  ".rmp": "application/vnd.rn-rn_music_package",
  ".rms": "application/vnd.rn-realmedia-secure",
  ".rmvb": "application/vnd.rn-realmedia-vbr",
  ".rmx": "application/vnd.rn-realsystem-rmx",
  ".rnx": "application/vnd.rn-realplayer",
  ".rp": "image/vnd.rn-realpix",
  ".rpm": "audio/x-pn-realaudio-plugin",
  ".rsml": "application/vnd.rn-rsml",
  ".rt": "text/vnd.rn-realtext",
  ".rtf": "application/x-rtf",
  ".rv": "video/vnd.rn-realvideo",
  ".sam": "application/x-sam",
  ".sat": "application/x-sat",
  ".sdp": "application/sdp",
  ".sdw": "application/x-sdw",
  ".sit": "application/x-stuffit",
  ".slb": "application/x-slb",
  ".sld": "application/x-sld",
  ".slk": "drawing/x-slk",
  ".smi": "application/smil",
  ".smil": "application/smil",
  ".smk": "application/x-smk",
  ".snd": "audio/basic",
  ".sol": "text/plain",
  ".sor": "text/plain",
  ".spc": "application/x-pkcs7-certificates",
  ".spl": "application/futuresplash",
  ".spp": "text/xml",
  ".ssm": "application/streamingmedia",
  ".sst": "application-pki.certstore",
  ".stl": "application/-pki.stl",
  ".stm": "text/html",
  ".sty": "application/x-sty",
  ".svg": "text/xml",
  ".swf": "application/x-shockwave-flash",
  ".tdf": "application/x-tdf",
  ".tg4": "application/x-tg4",
  ".tga": "application/x-tga",
  ".tif": "image/tiff",
  ".tiff": "image/tiff",
  ".tld": "text/xml",
  ".top": "drawing/x-top",
  ".torrent": "application/x-bittorrent",
  ".tsd": "text/xml",
  ".txt": "text/plain",
  ".uin": "application/x-icq",
  ".uls": "text/iuls",
  ".vcf": "text/x-vcard",
  ".vda": "application/x-vda",
  ".vdx": "application/vnd.visio",
  ".vml": "text/xml",
  ".vpg": "application/x-vpeg005",
  ".vsd": "application/x-vsd",
  ".vss": "application/vnd.visio",
  ".vst": "application/x-vst",
  ".vsw": "application/vnd.visio",
  ".vsx": "application/vnd.visio",
  ".vtx": "application/vnd.visio",
  ".vxml": "text/xml",
  ".wav": "audio/wav",
  ".wax": "audio/x-ms-wax",
  ".wb1": "application/x-wb1",
  ".wb2": "application/x-wb2",
  ".wb3": "application/x-wb3",
  ".wbmp": "image/vnd.wap.wbmp",
  ".wiz": "application/msword",
  ".wk3": "application/x-wk3",
  ".wk4": "application/x-wk4",
  ".wkq": "application/x-wkq",
  ".wks": "application/x-wks",
  ".wm": "video/x-ms-wm",
  ".wma": "audio/x-ms-wma",
  ".wmd": "application/x-ms-wmd",
  ".wmf": "application/x-wmf",
  ".wml": "text/vnd.wap.wml",
  ".wmv": "video/x-ms-wmv",
  ".wmx": "video/x-ms-wmx",
  ".wmz": "application/x-ms-wmz",
  ".wp6": "application/x-wp6",
  ".wpd": "application/x-wpd",
  ".wpg": "application/x-wpg",
  ".wpl": "application/-wpl",
  ".wq1": "application/x-wq1",
  ".wr1": "application/x-wr1",
  ".wri": "application/x-wri",
  ".wrk": "application/x-wrk",
  ".ws": "application/x-ws",
  ".ws2": "application/x-ws",
  ".wsc": "text/scriptlet",
  ".wsdl": "text/xml",
  ".wvx": "video/x-ms-wvx",
  ".xdp": "application/vnd.adobe.xdp",
  ".xdr": "text/xml",
  ".xfd": "application/vnd.adobe.xfd",
  ".xfdf": "application/vnd.adobe.xfdf",
  ".xhtml": "text/html",
  ".xls": "application/x-xls",
  ".xlw": "application/x-xlw",
  ".xml": "text/xml",
  ".xpl": "audio/scpls",
  ".xq": "text/xml",
  ".xql": "text/xml",
  ".xquery": "text/xml",
  ".xsd": "text/xml",
  ".xsl": "text/xml",
  ".xslt": "text/xml",
  ".xwd": "application/x-xwd",
  ".x_b": "application/x-x_b",
  ".x_t": "application/x-x_t"
}
  • 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
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/577362
推荐阅读
相关标签
  

闽ICP备14008679号