Minio预览和下载

该文章提供了一个使用MinioJavaSDK进行文件上传、预览链接生成和文件删除的示例。代码包括配置Minio客户端,上传文件到指定bucket,获取下载和预览链接,以及检查文件存在性和删除文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、依赖

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

2、代码

package utils;

import io.minio.*;
import io.minio.errors.MinioException;
import io.minio.http.Method;
import org.xmlpull.v1.XmlPullParserException;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * minio文件工具类
 * @author leo
 * @date 2023/6/16 18:21
 */
public class MinioFileUtil8 {


    private final static String ENDPOINT = "http://10.16.2.97:19000";
    private final static String ACCESS_KEY = "user";
    private final static String SECRET_KEY = "password";

    /**
     * 删除文件
     * @param args
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws InvalidKeyException
     * @throws XmlPullParserException
     */
    public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException, XmlPullParserException {
        try {

            MinioClient minioClient = MinioClient.builder().endpoint(ENDPOINT).credentials(ACCESS_KEY, SECRET_KEY).build();
            InputStream inputStream = new FileInputStream("C:\\Users\\Dell\\Downloads\\abc.png");
            int available = inputStream.available();
            // 上传参数中指定
            String bucket = "normal";
            String object = "abc.png";
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucket)
                    .object(object)
                    // 上传时指定对应对ContentType
                    .contentType(ViewContentType.getContentType("abc.png"))
                    .stream(inputStream, inputStream.available(), -1)
                    .build();
            ObjectWriteResponse objectWriteResponse = minioClient.putObject(putObjectArgs);
            String etag = objectWriteResponse.etag();
            String s = objectWriteResponse.versionId();
            System.out.println(etag);
            System.out.println(s);
            GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucket).object(object).build();
            String presignedObjectUrl = minioClient.getPresignedObjectUrl(build);
            System.out.println(presignedObjectUrl);
//
//            boolean b = checkFileExist(ENDPOINT, ACCESS_KEY, SECRET_KEY, bucket, "aaa"+object);
            String ba = generatorOuterLink(ENDPOINT, ACCESS_KEY, SECRET_KEY, bucket, object);
            String baa = generatorPreviewOuterLink(ENDPOINT, ACCESS_KEY, SECRET_KEY, bucket, object);
//            boolean bab = removeFile(ENDPOINT, ACCESS_KEY, SECRET_KEY, bucket, object);
//            System.out.println(b);
            System.out.println(ba);
            System.out.println(baa);
//            System.out.println(available);

//            minioClient.downloadObject(
//                    DownloadObjectArgs.builder()
//                            .bucket("normal")
//                            .object("bg.jpg")
//                            .filename("C:\\Users\\Dell\\Downloads\\down-bg.jpg")
//                            .build());

//            extraQueryParams(reqParams)
        } catch(MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }


    public static boolean doUpload(String endpoint, String accessKey, String secretKey, String bucketName, String objectName, InputStream inputStream){
        try {
            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .stream(inputStream, inputStream.available(), -1)
                    .build();
            minioClient.putObject(putObjectArgs);

            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean doUploadForPreview(String endpoint, String accessKey, String secretKey, String bucketName, String objectName, InputStream inputStream){
        try {
            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    // 上传时指定对应对ContentType
                    .contentType(ViewContentType.getContentType(objectName))
                    .stream(inputStream, inputStream.available(), -1)
                    .build();
            minioClient.putObject(putObjectArgs);

            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 验证文件是否存在
     * @param endpoint
     * @param accessKey
     * @param secretKey
     * @param bucketName
     * @param objectName
     * @return true-存在
     */
    public static boolean checkFileExist(String endpoint, String accessKey, String secretKey, String bucketName, String objectName){
        try {
            MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
            StatObjectArgs statObjectArgs = StatObjectArgs.builder().bucket(bucketName).object(objectName).build();
            minioClient.statObject(statObjectArgs);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 生成预览外链
     * @param endpoint
     * @param accessKey
     * @param secretKey
     * @param bucketName
     * @param objectName
     * @return
     */
    public static String generatorPreviewOuterLink(String endpoint, String accessKey, String secretKey, String bucketName, String objectName){
        try {
            MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
            GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                    .method(Method.GET)
                    .bucket(bucketName)
                    .object(objectName)
                    .build();
            String url = minioClient.getPresignedObjectUrl(args);
            return url;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成外链
     * @param endpoint
     * @param accessKey
     * @param secretKey
     * @param bucketName
     * @param objectName
     * @return
     */
    public static String generatorOuterLink(String endpoint, String accessKey, String secretKey, String bucketName, String objectName){
        try {
            Map<String, String> reqParams = new HashMap<>(3);
            reqParams.put("response-content-type", "application/octet-stream");
            MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
            GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                    .method(Method.GET)
                    .bucket(bucketName)
                    .object(objectName)
                    .extraQueryParams(reqParams)
                    .build();
            String url = minioClient.getPresignedObjectUrl(args);
            return url;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除文件
     * @param endpoint
     * @param accessKey
     * @param secretKey
     * @param bucketName
     * @param objectName
     * @return
     */
    public static boolean removeFile(String endpoint, String accessKey, String secretKey, String bucketName, String objectName){
        try {

            MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
            RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build();
            minioClient.removeObject(removeObjectArgs);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

3、使用 doUpload 和 doUploadForPreview 上传

4、使用 generatorOuterLink 和 generatorPreviewOuterLink 生成不同的链接(下载或预览)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值