Minio分布式文件存储
package wangbiao.config.minio;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import lombok.SneakyThrows;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MinIoTest {
@Autowired
private MinioClient minioClient;
private final String bucketName = "AAAAA";
@Test
//上传已知大小的输入流
void getObject0() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
File file = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
String name = file.getName();
String substring = name.substring(name.lastIndexOf("."));
FileInputStream inputStream = new FileInputStream(file);
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object("aaaa").stream(
inputStream, 2795, -1)
.contentType(substring)
.build());
}
@SneakyThrows
@Test
//上传W未知知大小的输入流
void getObject1() throws FileNotFoundException {
File file = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
String name = file.getName();
String substring = name.substring(name.lastIndexOf("."));
FileInputStream inputStream = new FileInputStream(file);
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object("bbbb").stream(
inputStream, -1, 10485760)
.contentType(substring)
.build());
}
@Test
//创建带有"/"结尾的文件夹
void getObject2() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object("cccc/").stream(
new ByteArrayInputStream(new byte[]{}), 0, -1)
.build());
}
@Test
//上床具有header信息和用户元数据的文件流
void getObject3() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
File file = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
FileInputStream inputStream = new FileInputStream(file);
Map<String, String> headers = new HashMap<>();
headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("My-Project", "Project One");
ObjectWriteResponse objectWriteResponse = minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object("dddd").stream(
inputStream, 2795, -1)
.headers(headers)
.userMetadata(userMetadata)
.build());
String etag = objectWriteResponse.etag();
String versionId = objectWriteResponse.versionId();
String bucket = objectWriteResponse.bucket();
String object = objectWriteResponse.object();
String region = objectWriteResponse.region();
System.out.println("etag:" + etag);
System.out.println("versionId:" + versionId);
System.out.println("object:" + object);
System.out.println("bucket:" + bucket);
System.out.println("region:" + region);
}
@Test
// 上传带有服务器信息的流
void getObject4() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
HashMap<String, String> stringStringHashMap = new HashMap<>();
ServerSideEncryptionKms serverSideEncryptionKms = new ServerSideEncryptionKms("132", stringStringHashMap);
File file = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
FileInputStream inputStream = new FileInputStream(file);
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object("eeee").stream(
inputStream, 2795, -1)
.sse(serverSideEncryptionKms)
.build());
}
@Test
//把文件作为对象存在桶里
void getObject5() throws IOException, ServerException, InsufficientDataException, InternalException,
InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, XmlParserException,
ErrorResponseException {
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(bucketName)
.object("ffffff")
.filename("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java")
.build());
//// 设置上传内容类型
// minioClient.uploadObject(
// UploadObjectArgs.builder()
// .bucket("my-bucketname")
// .object("my-objectname")
// .filename("my-video.avi")
// .contentType("video/mp4")
// .build());
}
//多文件上传 ?T
@Test
void getObject6() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
File file0 = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
FileInputStream inputStream0 = new FileInputStream(file0);
byte[] bytes0 = new byte[1024];
ByteArrayOutputStream output0 = null;
int read0;
while ((read0 = inputStream0.read(bytes0)) != -1) {
output0.write(bytes0, 0, read0);
}
byte[] buffer0 = output0.toByteArray();
File file1 = new File("C:\\Users\\lzadmin\\Desktop\\MinIoUtil.java");
FileInputStream inputStream1 = new FileInputStream(file1);
byte[] bytes1 = new byte[1024];
ByteArrayOutputStream output1 = null;
int read1;
while ((read1 = inputStream1.read(bytes1)) != -1) {
output0.write(bytes1, 0, read1);
}
byte[] buffer1 = output1.toByteArray();
List<SnowballObject> objects = new ArrayList<SnowballObject>();
objects.add(
new SnowballObject(
"gggg",
new ByteArrayInputStream(buffer0),
5,
null));
objects.add(
new SnowballObject(
"hhhhhh",
new ByteArrayInputStream(buffer1),
4,
null));
minioClient.uploadSnowballObjects(
UploadSnowballObjectsArgs.builder().bucket(bucketName).objects(objects).build());
}
@Test
void getObject7() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
//删除文件
minioClient.removeObject(
RemoveObjectArgs.builder().bucket(bucketName).object("aaaa").build());
// 删除指定版本的案件
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket(bucketName)
.object("my-versioned-objectname")
.versionId("my-versionid")
.build());
// Remove versioned object bypassing Governance mode.
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket("my-bucketname")
.object("my-versioned-objectname")
.versionId("my-versionid")
.bypassGovernanceMode(true)//旁路管理模式
.build());
}
@Test
//删除多个文件
void getObject8() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException,
NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
List<DeleteObject> objects = new LinkedList<>();
objects.add(new DeleteObject("aaaa"));
objects.add(new DeleteObject("bbbb"));
objects.add(new DeleteObject("cccc"));
Iterable<Result<DeleteError>> results =
minioClient.removeObjects(
RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build());
for (Result<DeleteError> result : results) {
DeleteError error = result.get();
System.out.println(
"Error in deleting object " + error.objectName() + "; " + error.message());
}
}
@Test
void getObject9() {
}
@Test
void getObject10() {
}
}
@Configuration
public class MinioConfig {
private final MinioProperty minioProperty;
public MinioConfig(MinioProperty minioProperty) {
this.minioProperty = minioProperty;
}
@Bean
public MinioClient getMinioClient() {
return MinioClient.builder()
.endpoint(minioProperty.getEndpoint())
.credentials(minioProperty.getAccessKey(), minioProperty.getSecretKey())
.build();
}
}
@Data
@Component
@ConfigurationProperties(prefix ="minio")
public class MinioProperty {
private String endpoint;
private String accessKey;
private String secretKey;
}
minio.endpoint=localhost:9200/ minio.accessKey=minioadmin minio.secretKey=minioadmin
本文来自博客园,作者:余生请多指教ANT,转载请注明原文链接:https://www.cnblogs.com/wangbiaohistory/p/16867390.html

浙公网安备 33010602011771号