• springboot整合minio(分部署存储)


    先看下效果

    本篇是在上篇已经部署好minio的基础上 进行的代码整合,开始吧

     新建项目minio-83-client-springboot  

    大概的机构是这样的

    pom.xml

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.10</version>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    </dependency>
    <dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.3.0</version>
    </dependency>
    <dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.1</version>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>



    application.properties
    server.port=9000

    #minio配置
    # 配置文件上传大小限制
    spring.servlet.multipart.max-file-size=100MB
    spring.servlet.multipart.max-request-size=100MB
    minio.endpoint.http=http://49.235.127.115:19000
    minio.endpoint.accessKey: minioadmin
    minio.endpoint.secretKey: minioadmin
    minio.isUse=false
    -------------------------------------------------------
    package com.gisinfo.minio.web.conf;

    import io.minio.MinioClient;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

    @Configuration
    public class MinioConfig {

    @Value("${minio.endpoint.http}")
    private String endpoint;
    @Value("${minio.endpoint.accessKey}")
    private String accessKey;
    @Value("${minio.endpoint.secretKey}")
    private String secretKey;
    @Value("${minio.isUse}")
    private boolean isUse;

    /**
    * 注入minio 客户端
    * @return
    */
    @Bean
    public MinioClient minioClient(){

    return MinioClient.builder()
    .endpoint(endpoint)
    .credentials(accessKey, secretKey)
    .build();
    }
    }


    -------------------------------------------------------

    package com.gisinfo.minio.web.util;

    import cn.hutool.core.io.FileUtil;
    import cn.hutool.core.util.IdUtil;
    import cn.hutool.core.util.ObjectUtil;
    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 org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    import org.springframework.util.StringUtils;
    import org.springframework.web.multipart.MultipartFile;

    import java.io.File;
    import java.io.InputStream;
    import java.time.LocalDate;
    import java.util.*;

    @Service
    public class MinioUtilService {

    /**
    * url分隔符
    */
    public static final String URI_DELIMITER = "/";

    @Autowired
    private MinioClient minioClient;

    private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;

    /**
    * 检查存储桶是否存在
    *
    * @param bucketName 存储桶名称
    * @return
    */
    public boolean bucketExists(String bucketName) throws Exception {
    boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    if (flag) {
    return true;
    }
    return false;
    }

    /**
    * 创建存储桶
    *
    * @param bucketName 存储桶名称
    */
    public boolean makeBucket(String bucketName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (!flag) {
    minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
    return true;
    } else {
    return false;
    }
    }

    /**
    * 列出所有存储桶名称
    *
    * @return
    */
    public List<String> listBucketNames() throws Exception {
    List<Bucket> bucketList = listBuckets();
    List<String> bucketListName = new ArrayList<>();
    for (Bucket bucket : bucketList) {
    bucketListName.add(bucket.name());
    }
    return bucketListName;
    }

    /**
    * 列出所有存储桶
    *
    * @return
    */
    public List<Bucket> listBuckets() throws Exception {
    return minioClient.listBuckets();
    }

    /**
    * 删除存储桶
    *
    * @param bucketName 存储桶名称
    * @return
    */
    public boolean removeBucket(String bucketName) throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    Iterable<Result<Item>> myObjects = listObjects(bucketName);
    for (Result<Item> result : myObjects) {
    Item item = result.get();
    // 有对象文件,则删除失败
    if (item.size() > 0) {
    return false;
    }
    }
    // 删除存储桶,注意,只有存储桶为空时才能删除成功。
    minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    flag = bucketExists(bucketName);
    if (!flag) {
    return true;
    }

    }
    return false;
    }

    /**
    * 列出存储桶中的所有对象名称
    *
    * @param bucketName 存储桶名称
    * @return
    */
    public List<String> listObjectNames(String bucketName) throws Exception {
    List<String> listObjectNames = new ArrayList<>();
    boolean flag = bucketExists(bucketName);
    if (flag) {
    Iterable<Result<Item>> myObjects = listObjects(bucketName);
    for (Result<Item> result : myObjects) {
    Item item = result.get();
    listObjectNames.add(item.objectName());
    }
    }
    return listObjectNames;
    }

    /**
    * 列出存储桶中的所有对象
    *
    * @param bucketName 存储桶名称
    * @return
    */
    public Iterable<Result<Item>> listObjects(String bucketName) throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
    }
    return null;
    }


    /**
    * 获取年月日[2020, 09, 01]
    * @return
    */
    protected static String[] getDateFolder() {
    String[] retVal = new String[3];

    LocalDate localDate = LocalDate.now();
    retVal[0] = localDate.getYear() + "";

    int month = localDate.getMonthValue();
    retVal[1] = month < 10 ? "0" + month : month + "";

    int day = localDate.getDayOfMonth();
    retVal[2] = day < 10 ? "0" + day : day + "";

    return retVal;
    }

    /**
    * 上传文件
    *
    * @param bucketName bucket名称
    * @param objectName 文件名称
    * @param in 文件流
    * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
    */
    public String putObject(String bucketName, String objectName, InputStream in) throws Exception {
    ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .stream(in, in.available(), -1)
    .build()
    );
    return this.getObjectUrl(bucketName,objectName);
    }

    /**
    * 以流的形式获取一个文件对象
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @return
    */
    public InputStream getObject(String bucketName, String objectName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    StatObjectResponse statObject = statObject(bucketName, objectName);
    if (statObject != null && statObject.size() > 0) {
    InputStream stream = minioClient.getObject(GetObjectArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .build());
    return stream;
    }
    }
    return null;
    }

    /**
    * 以流的形式获取一个文件对象(断点下载)
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @param offset 起始字节的位置
    * @param length 要读取的长度 (可选,如果无值则代表读到文件结尾)
    * @return
    */
    public InputStream getObject(String bucketName, String objectName, long offset, Long length)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    StatObjectResponse statObject = statObject(bucketName, objectName);
    if (statObject != null && statObject.size() > 0) {
    InputStream stream = minioClient.getObject(GetObjectArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .offset(offset)
    .length(length)
    .build());
    return stream;
    }
    }
    return null;
    }

    /**
    * 下载并将文件保存到本地
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    // * @param fileName File name
    * @return
    */
    public boolean getObjectFile(String bucketName, String objectName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    StatObjectResponse statObject = statObject(bucketName, objectName);
    if (statObject != null && statObject.size() > 0) {
    GetObjectResponse getObjectResponse = minioClient.getObject(GetObjectArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .build());
    return true;
    }
    }
    return false;
    }

    /**
    * 删除一个对象
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    */
    public boolean removeObject(String bucketName, String objectName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    return true;
    }
    return false;

    }

    /**
    * 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
    *
    * @param bucketName 存储桶名称
    * @param objectNames 含有要删除的多个object名称的迭代器对象
    * @return
    */
    public List<String> removeObject(String bucketName, List<String> objectNames)
    throws Exception {
    List<String> deleteErrorNames = new ArrayList<>();
    boolean flag = bucketExists(bucketName);
    if (flag) {
    List<DeleteObject> deleteObjectList = new ArrayList<>();
    if (ObjectUtil.isNotEmpty(objectNames)){
    for(String objectName : objectNames){
    deleteObjectList.add(new DeleteObject(objectName));
    }
    }
    Iterable<Result<DeleteError>> results = minioClient.removeObjects(
    RemoveObjectsArgs.builder()
    .bucket(bucketName)
    .objects(deleteObjectList)
    .build()
    );
    for (Result<DeleteError> result : results) {
    DeleteError error = result.get();
    deleteErrorNames.add(error.objectName());
    }
    }
    return deleteErrorNames;
    }


    /**
    * 生成一个给HTTP GET请求用的presigned URL
    * 浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
    * @return
    */
    public String presignedGetObject(String bucketName, String objectName, Integer expires)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    String url = "";
    if (flag) {
    if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
    throw new RuntimeException(expires+
    ":expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
    }
    url = minioClient.getPresignedObjectUrl(
    GetPresignedObjectUrlArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .expiry(expires)
    .build());
    }
    return url;
    }

    // /**
    // * 生成一个给HTTP PUT请求用的presigned URL
    // * 浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
    // *
    // * @param bucketName 存储桶名称
    // * @param objectName 存储桶里的对象名称
    // * @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
    // * @return
    // */
    // public String presignedPutObject(String bucketName, String objectName, Integer expires)
    // throws Exception {
    // boolean flag = bucketExists(bucketName);
    // String url = "";
    // if (flag) {
    // if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
    // throw new RuntimeException(expires+
    // ":expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
    // }
    // url = minioClient.presignedPutObject(bucketName, objectName, expires);
    // }
    // return url;
    // }

    /**
    * 获取对象的元数据
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @return
    */
    public StatObjectResponse statObject(String bucketName, String objectName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    StatObjectResponse statObject = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
    return statObject;
    }
    return null;
    }

    /**
    * 文件访问路径
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @return
    */
    public String getObjectUrl(String bucketName, String objectName) throws Exception {
    boolean flag = bucketExists(bucketName);
    String url = "";
    if (flag) {
    url = minioClient.getPresignedObjectUrl(
    GetPresignedObjectUrlArgs.builder()
    .method(Method.GET)
    .bucket(bucketName)
    .object(objectName)
    .build()
    );
    }
    return url;
    }

    }


    ----------------------------------------------------------
    package com.gisinfo.minio.web.util;


    import org.springframework.web.multipart.MultipartFile;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;

    /**
    * @Author: Yin BenLi
    * @Description: MultipartFile转成File工具类
    * @Date: Create in 2020/4/8 17:19
    */
    public class MultipartFileToFile {

    /**
    * MultipartFile File
    * @param file
    * @throws Exception
    */
    public static File multipartFileToFile(MultipartFile file) throws Exception {

    File toFile = null;
    if (file.equals("") || file.getSize() <= 0) {
    file = null;
    } else {
    InputStream ins = null;
    ins = file.getInputStream();
    toFile = new File(file.getOriginalFilename());
    inputStreamToFile(ins, toFile);
    ins.close();
    }
    return toFile;
    }

    /**
    * 获取流文件
    * @param ins
    * @param file
    */
    private static void inputStreamToFile(InputStream ins, File file) {
    try {
    OutputStream os = new FileOutputStream(file);
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
    os.write(buffer, 0, bytesRead);
    }
    os.close();
    ins.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * 删除本地临时文件
    * @param file
    */
    public static void delteTempFile(File file) {
    if (file != null) {
    File del = new File(file.toURI());
    del.delete();
    }
    }
    }


    -------------------------------------------------------
    @RestController
    public class MinioController {

    @Value("${server.port}")
    String port;

    @Resource
    private MinioUtilService minioUtilService;


    @PostMapping(value = "/upload")
    @SneakyThrows(Exception.class)
    public Results upload(@RequestParam("multipartFile") MultipartFile file){
    File pFile = MultipartFileToFile.multipartFileToFile(file);
    System.out.println(pFile.getName());
    minioUtilService.putObject("test01","001/"+pFile.getName(),FileUtil.getInputStream(pFile));
    return Results.success(file.getName());
    }

    @PostMapping(value = "/uploadFiles")
    @SneakyThrows(Exception.class)
    public Results uploadFiles(@RequestParam("multipartFiles") MultipartFile[] files){
    Arrays.asList(files).stream().forEach(file -> {
    try {
    File pFile = MultipartFileToFile.multipartFileToFile(file);
    minioUtilService.putObject("test01","001/"+pFile.getName(),FileUtil.getInputStream(pFile));
    } catch (Exception e) {
    e.printStackTrace();
    }
    } );
    return Results.success("999");
    }

    }

    ------------------------------------------------------
    @SpringBootApplication
    public class Minio830Application {

    public static void main(String[] args) {
    SpringApplication.run(Minio830Application.class, args);
    }

    }

    ----------------------------------------------------

    启动项目

    postman
    http://127.0.0.1:9000/upload

    
    

     上传多张

    http://127.0.0.1:9000/uploadFiles

     欢迎打赏 谢谢看官







    package com.gisinfo.minio.web.util;

    import cn.hutool.core.io.FileUtil;
    import cn.hutool.core.util.IdUtil;
    import cn.hutool.core.util.ObjectUtil;
    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 org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    import org.springframework.util.StringUtils;
    import org.springframework.web.multipart.MultipartFile;

    import java.io.File;
    import java.io.InputStream;
    import java.time.LocalDate;
    import java.util.*;

    @Service
    public class MinioUtilService {

    /**
    * url分隔符
    */
    public static final String URI_DELIMITER = "/";

    @Autowired
    private MinioClient minioClient;

    private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;

    /**
    * 检查存储桶是否存在
    *
    * @param bucketName 存储桶名称
    * @return
    */
    public boolean bucketExists(String bucketName) throws Exception {
    boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    if (flag) {
    return true;
    }
    return false;
    }

    /**
    * 创建存储桶
    *
    * @param bucketName 存储桶名称
    */
    public boolean makeBucket(String bucketName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (!flag) {
    minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
    return true;
    } else {
    return false;
    }
    }

    /**
    * 列出所有存储桶名称
    *
    * @return
    */
    public List<String> listBucketNames() throws Exception {
    List<Bucket> bucketList = listBuckets();
    List<String> bucketListName = new ArrayList<>();
    for (Bucket bucket : bucketList) {
    bucketListName.add(bucket.name());
    }
    return bucketListName;
    }

    /**
    * 列出所有存储桶
    *
    * @return
    */
    public List<Bucket> listBuckets() throws Exception {
    return minioClient.listBuckets();
    }

    /**
    * 删除存储桶
    *
    * @param bucketName 存储桶名称
    * @return
    */
    public boolean removeBucket(String bucketName) throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    Iterable<Result<Item>> myObjects = listObjects(bucketName);
    for (Result<Item> result : myObjects) {
    Item item = result.get();
    // 有对象文件,则删除失败
    if (item.size() > 0) {
    return false;
    }
    }
    // 删除存储桶,注意,只有存储桶为空时才能删除成功。
    minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    flag = bucketExists(bucketName);
    if (!flag) {
    return true;
    }

    }
    return false;
    }

    /**
    * 列出存储桶中的所有对象名称
    *
    * @param bucketName 存储桶名称
    * @return
    */
    public List<String> listObjectNames(String bucketName) throws Exception {
    List<String> listObjectNames = new ArrayList<>();
    boolean flag = bucketExists(bucketName);
    if (flag) {
    Iterable<Result<Item>> myObjects = listObjects(bucketName);
    for (Result<Item> result : myObjects) {
    Item item = result.get();
    listObjectNames.add(item.objectName());
    }
    }
    return listObjectNames;
    }

    /**
    * 列出存储桶中的所有对象
    *
    * @param bucketName 存储桶名称
    * @return
    */
    public Iterable<Result<Item>> listObjects(String bucketName) throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
    }
    return null;
    }


    /**
    * 获取年月日[2020, 09, 01]
    * @return
    */
    protected static String[] getDateFolder() {
    String[] retVal = new String[3];

    LocalDate localDate = LocalDate.now();
    retVal[0] = localDate.getYear() + "";

    int month = localDate.getMonthValue();
    retVal[1] = month < 10 ? "0" + month : month + "";

    int day = localDate.getDayOfMonth();
    retVal[2] = day < 10 ? "0" + day : day + "";

    return retVal;
    }

    /**
    * 上传文件
    *
    * @param bucketName bucket名称
    * @param objectName 文件名称
    * @param in 文件流
    * @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
    */
    public String putObject(String bucketName, String objectName, InputStream in) throws Exception {
    ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .stream(in, in.available(), -1)
    .build()
    );
    return this.getObjectUrl(bucketName,objectName);
    }

    /**
    * 以流的形式获取一个文件对象
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @return
    */
    public InputStream getObject(String bucketName, String objectName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    StatObjectResponse statObject = statObject(bucketName, objectName);
    if (statObject != null && statObject.size() > 0) {
    InputStream stream = minioClient.getObject(GetObjectArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .build());
    return stream;
    }
    }
    return null;
    }

    /**
    * 以流的形式获取一个文件对象(断点下载)
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @param offset 起始字节的位置
    * @param length 要读取的长度 (可选,如果无值则代表读到文件结尾)
    * @return
    */
    public InputStream getObject(String bucketName, String objectName, long offset, Long length)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    StatObjectResponse statObject = statObject(bucketName, objectName);
    if (statObject != null && statObject.size() > 0) {
    InputStream stream = minioClient.getObject(GetObjectArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .offset(offset)
    .length(length)
    .build());
    return stream;
    }
    }
    return null;
    }

    /**
    * 下载并将文件保存到本地
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    // * @param fileName File name
    * @return
    */
    public boolean getObjectFile(String bucketName, String objectName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    StatObjectResponse statObject = statObject(bucketName, objectName);
    if (statObject != null && statObject.size() > 0) {
    GetObjectResponse getObjectResponse = minioClient.getObject(GetObjectArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .build());
    return true;
    }
    }
    return false;
    }

    /**
    * 删除一个对象
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    */
    public boolean removeObject(String bucketName, String objectName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    return true;
    }
    return false;

    }

    /**
    * 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
    *
    * @param bucketName 存储桶名称
    * @param objectNames 含有要删除的多个object名称的迭代器对象
    * @return
    */
    public List<String> removeObject(String bucketName, List<String> objectNames)
    throws Exception {
    List<String> deleteErrorNames = new ArrayList<>();
    boolean flag = bucketExists(bucketName);
    if (flag) {
    List<DeleteObject> deleteObjectList = new ArrayList<>();
    if (ObjectUtil.isNotEmpty(objectNames)){
    for(String objectName : objectNames){
    deleteObjectList.add(new DeleteObject(objectName));
    }
    }
    Iterable<Result<DeleteError>> results = minioClient.removeObjects(
    RemoveObjectsArgs.builder()
    .bucket(bucketName)
    .objects(deleteObjectList)
    .build()
    );
    for (Result<DeleteError> result : results) {
    DeleteError error = result.get();
    deleteErrorNames.add(error.objectName());
    }
    }
    return deleteErrorNames;
    }


    /**
    * 生成一个给HTTP GET请求用的presigned URL
    * 浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
    * @return
    */
    public String presignedGetObject(String bucketName, String objectName, Integer expires)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    String url = "";
    if (flag) {
    if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
    throw new RuntimeException(expires+
    ":expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
    }
    url = minioClient.getPresignedObjectUrl(
    GetPresignedObjectUrlArgs.builder()
    .bucket(bucketName)
    .object(objectName)
    .expiry(expires)
    .build());
    }
    return url;
    }

    // /**
    // * 生成一个给HTTP PUT请求用的presigned URL
    // * 浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
    // *
    // * @param bucketName 存储桶名称
    // * @param objectName 存储桶里的对象名称
    // * @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
    // * @return
    // */
    // public String presignedPutObject(String bucketName, String objectName, Integer expires)
    // throws Exception {
    // boolean flag = bucketExists(bucketName);
    // String url = "";
    // if (flag) {
    // if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
    // throw new RuntimeException(expires+
    // ":expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
    // }
    // url = minioClient.presignedPutObject(bucketName, objectName, expires);
    // }
    // return url;
    // }

    /**
    * 获取对象的元数据
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @return
    */
    public StatObjectResponse statObject(String bucketName, String objectName)
    throws Exception {
    boolean flag = bucketExists(bucketName);
    if (flag) {
    StatObjectResponse statObject = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
    return statObject;
    }
    return null;
    }

    /**
    * 文件访问路径
    *
    * @param bucketName 存储桶名称
    * @param objectName 存储桶里的对象名称
    * @return
    */
    public String getObjectUrl(String bucketName, String objectName) throws Exception {
    boolean flag = bucketExists(bucketName);
    String url = "";
    if (flag) {
    url = minioClient.getPresignedObjectUrl(
    GetPresignedObjectUrlArgs.builder()
    .method(Method.GET)
    .bucket(bucketName)
    .object(objectName)
    .build()
    );
    }
    return url;
    }

    }


  • 相关阅读:
    delete与double free
    OpenCV(1)——基础数据结构CvMat
    防止表单自动提交_随笔2012年5月16日
    Flex 学习笔记学习资料
    当析构函数遇到多线程 ── C++ 中线程安全的对象回调
    .NET Core2.0+MVC 用session,cookie实现的sso单点登录
    TreeView中右击直接获取节点的方法
    webservice 远程调试配置
    数组,集合 转成DataTable 方法
    String类中几个简单的常用方法
  • 原文地址:https://www.cnblogs.com/qq3245792286/p/16135610.html
Copyright © 2020-2023  润新知