Minio预览和下载

1、依赖

io.miniominio8.5.2

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 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 生成不同的链接(下载或预览)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部