简介
MinIO 是在 GNU Affero 通用公共许可证 v3.0 下发布的高性能对象存储。它是与 Amazon S3 云存储服务兼容的 API。使用 MinIO 为机器学习、分析和应用程序数据工作负载构建高性能基础架构。
搭建mini
MinIO 需要一个持久卷来存储配置和应用程序数据。但是,出于测试目的,您可以通过简单地传递一个目录(/data在下面的示例中)来启动 MinIO 。该目录是在容器启动时在容器文件系统中创建的。但是容器退出后所有的数据都丢失了。如果打算持久化,进行挂载即可。
docker run \
-p 9000:9000 \
-p 9001:9001 \
-e "MINIO_ROOT_USER=LIUYUNSHENG" \
-e "MINIO_ROOT_PASSWORD=LIUYUNSHENGKEY" \
minio/minio server /data --console-address ":9001"
搭建持久化的
docker run --restart always --privileged=true -d -p 9000:9000 -p 9001:9001\
-v /home/lys/minio/miniodata:/data:rw -v /home/lys/minio/minioconfig:/root/.minio:rw \
-v /etc/localtime:/etc/localtime:ro \
minio/minio server /data \
--console-address ":9001"
web访问
http://10.6.8.227:9001/dashboard
java 常用操作
package com.sugon.cloud.utils;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import com.sugon.cloud.exception.GlobalException;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* @Auther: liuysh
* @Date: 2021/8/30 13:50
* @Description:
*/
public class S3Test {
public static void main(String[] args) throws Exception{
AmazonS3 s3Client= S3Utils.getS3Client("LIUYUNSHENG","LIUYUNSHENGKEY","http://10.6.8.227:9000");
List<Bucket> bucketList=s3Client.listBuckets();
List<Bucket> myBuckets=bucketList.stream().filter(bucket -> bucket.getName().equals("bucket01")).collect(Collectors.toList());
if(myBuckets.size()<1){
s3Client.createBucket("bucket01");
}
putObject(s3Client);
S3Object s3Object = s3Client.getObject(
"bucket01",
"1.jpg"
);
downLoadObject(s3Object);
System.out.println(bucketList);
ListObjectsV2Result result = s3Client.listObjectsV2("bucket01");
List<S3ObjectSummary> objects = result.getObjectSummaries();
for (S3ObjectSummary os : objects) {
System.out.println("含有的文件:" + os.getKey());
}
// 共享文件
generatePresignedUrl(s3Client,"bucket01", "1.jpg");
}
public static void putObject(AmazonS3 s3Client) throws Exception{
File localFile=new File("E:\\Download\\a.txt");
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
//metadata.setContentLength(5);
String s3FileFullPath = UUID.randomUUID()+"a.txt";
PutObjectResult putResult = s3Client.putObject("bucket01", s3FileFullPath, new FileInputStream(localFile), metadata);
System.out.println(putResult);
}
public static void downLoadObject(S3Object s3Object) throws Exception{
File targetFile=new File("E:\\Download\\111.jpg");
OutputStream out=new FileOutputStream(targetFile);
try(BufferedInputStream bufferedInputStream = new BufferedInputStream(
s3Object.getObjectContent()
)){
int len ;
byte[] buffer = new byte[1024];
while((len=bufferedInputStream.read(buffer))!=-1){
out.write(buffer, 0, len);
}
out.flush();
}catch (IOException e){
GlobalException.throwException(e.getMessage());
}
out.close();
}
public static String generatePresignedUrl(AmazonS3 s3Client,String bucketName, String key){
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
bucketName, key);
Date expirationDate = null;
try {
expirationDate = new SimpleDateFormat("yyyy-MM-dd").parse("2021-09-19");
} catch (Exception e) {
e.printStackTrace();
}
//设置过期时间
urlRequest.setExpiration(expirationDate);
URL url =s3Client.generatePresignedUrl(urlRequest);
System.out.println(url);
return url.getPath();
}
}