当前位置:网站首页>学习笔记-minio
学习笔记-minio
2022-07-27 10:26:00 【weixin0605】
minio
- 分布式文件存储系统
- 概念
- Bucket
- 桶
- Object
- 存储文件
- Drive
- 磁盘
- Set
- Drive集合
- Bucket
- 纠删码(EC)
- Erasure Code
- 保证高可靠性
- 安装
- docker
- docker compose
- 部署
- 纠删码模式部署
- 分布式集群部署
- 客户端mc操作
- mc config host ls
- 查看minio服务器名称与地址
- mc config host add xxx http://xxx.xx.xx.xx:port username password
- 添加minio服务器到列表
- mc config host remove xxx
- 删除列表中对应的minio服务器
- mc ls 服务器名称
- 查看minio服务文件列表
- mc cp minio服务器名/文件路径/文件名 D:\xx\xx
- 下载文件
- mc cp D:\xx\xx minio服务器名/桶
- 上传文件
- mc rm minio服务器名/文件路径/文件名
- 删除文件
- mc mb minio服务器名/桶名
- 创建bucket
- mc rm --force minio服务器名/桶名
- 强制删除bucket(bucket不为空需要强制删除)
- mc du minio服务器(/路径)
- 磁盘使用情况
- mc admin user list minio服务器
- 查看用户信息
- mc admin user add minio服务器 newUser
- 添加新用户
- mc policy list 服务器
- 查看策略列表
- mc admin policy info 服务器 策略名
- 查看策略详情
- mc admin policy add 服务器 策略名 策略文件地址
- 添加策略
- mc admin policy set 用户 策略名
- 为用户设置策略
- mc config host ls
java整合
- 依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.0</version>
</dependency>
<dependency>
<groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId>
<version>0.5.3</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.1</version>
</dependency>
// 连接minio
MinioClient minioClient =
MinioClient.builder()
.endpoint("https://play.min.io")
.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
.build();
// 创建bucket
String bucketName = "xxx";
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if(!found){
// 不存在
minioClient.makeBucket(MakeBuketArgs.builder().bucket(bucketName).build());
}
// 上传文件
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(bucketName)
.object("xxx") // 上传文件路径
.filename("xxx") // 本地磁盘路径
.build());
// 下载
minioClient.downLoadObject(
DownLoadObjectArgs.builder()
.bucket(bucketName)
.object("xxx") // 上传文件路径
.filename("xxx") // 本地磁盘路径
.build());
springboot整合
minio:
endpoint:xxx
accesskey:xxx
secretKey:xxx
bucketName:xxx
@Data
@Component
@ConfigurationProperties(prefix="minio")
public class MinioProperties{
private String endpoint;
private String accesskey;
private String secretkey;
}
@Configuration
public class MinioConfig{
@Autowired
private MinioProperties minioProperties;
@Bean
public MinioClient minioClient(){
MinioClient minioClient =
MinioClient.builder()
.endpoint(minioProperties.getEndPoint())
.credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
.build();
return minioClient;
}
}
@Component
public class MinioUtil {
@Autowired
private MinioClient minioClient;
/** * 文件上传 */
public Boolean upload(MultipartFile file, String filePath, String tempFileName, String bucketName) {
try {
if (!bucketExists(bucketName)) {
makeBucket(bucketName);
}
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName)
.object(filePath + tempFileName)
.stream(file.getInputStream(), file.getSize(), -1)
.contentType(file.getContentType()).build();
//文件名称相同会覆盖
ObjectWriteResponse response = minioClient.putObject(objectArgs);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * 预览文件 */
public void preview(String bucketName, String fileUrl, String openStyle, String fileName, HttpServletResponse res) {
GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
.object(fileUrl + fileName).build();
try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
byte[] buf = new byte[1024];
int len;
try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
while ((len = response.read(buf)) != -1) {
os.write(buf, 0, len);
}
os.flush();
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
byte[] bytes = os.toByteArray();
res.setCharacterEncoding("utf-8");
String src = "application/" + suffix;
res.setContentType(src);
res.addHeader("Content-Disposition", openStyle + ";fileName=" + fileName);
try (ServletOutputStream stream = res.getOutputStream()) {
stream.write(bytes);
stream.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 查看存储bucket是否存在 */
public Boolean bucketExists(String bucketName) {
Boolean found;
try {
found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return found;
}
/** * 创建存储bucket */
public Boolean makeBucket(String bucketName) {
try {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/** * 删除存储bucket */
public Boolean removeBucket(String bucketName) {
try {
minioClient.removeBucket(RemoveBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/** * 文件下载 */
public void download(String bucketName, String fileUrl, String fileName, HttpServletResponse res) {
GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
.object(fileUrl + fileName).build();
try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
byte[] buf = new byte[1024];
int len;
try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
while ((len = response.read(buf)) != -1) {
os.write(buf, 0, len);
}
os.flush();
byte[] bytes = os.toByteArray();
res.setCharacterEncoding("utf-8");
//设置强制下载不打开
res.setContentType("application/force-download");
res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
try (ServletOutputStream stream = res.getOutputStream()) {
stream.write(bytes);
stream.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 批量删除文件对象 */
public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
List<DeleteObject> dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
return results;
}
//文件删除
@DeleteMapping
public boolean delete(String objectName, String bucketName) {
try {
RemoveObjectArgs objectArgs = RemoveObjectArgs.builder().object(objectName).bucket(bucketName).build();
minioClient.removeObject(objectArgs);
return true;
} catch (Exception e) {
return false;
}
}
/** * 上传文件 */
public boolean putObject(String bucketName, String objectName, InputStream stream) throws Exception {
try {
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName)
.object(objectName).contentType("application/octet-stream").stream(stream, stream.available(), -1).build();
minioClient.putObject(objectArgs);
return true;
} catch (Exception e) {
return false;
}
}
}
边栏推荐
- antd table中排序th阻止悬停变色+table悬停行变色+table表头变色
- Analysis of heterogeneous computing technology
- 一次跨域问题的记录
- Integrated design of communication perception based on CSI: problems, challenges and Prospects
- MySQL log management, backup and recovery
- Apache22 and opencms9.5 integrated configuration
- MySQL index, transaction and storage engine
- Shardingproxy sub database and table actual combat and comparison of similar products
- Your appearance is amazing! Two JSON visualization tools are recommended for use with swagger. It's really fragrant
- 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
猜你喜欢

antd table中排序th阻止悬停变色+table悬停行变色+table表头变色

Recruit top talents! The "megeagle creator program" of Kuangshi technology was officially launched

Metaspolit

Use kaggle to run Li Hongyi's machine learning homework

Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions

How to build a data index system is the most effective. It will take you a quick start from 0 to 1

Server access speed

JSP自定义标签之自定义分页01

PHP generates text and image watermarks

ASP. Net core dependency injection journey: 1. Theoretical concepts
随机推荐
Analysis of new communication security risks brought by quantum computer and Countermeasures
PHP generates text and image watermarks
Substr and substring function usage in SQL
WEB服务如何平滑的上下线
antd table+checkbox 默认值显示
Edata base, a secondary development project based on spark packaging, is introduced
Overview of data security in fog computing
File upload vulnerability related
FTP server
JSP自定义标签之自定义分页01
一次跨域问题的记录
Project team summer vacation summary 01
Metasploit Eternal Blue attack
【Liunx】MariaDB/MySQL定时全量备份脚本及数据恢复
Eslint's error message module error (from./node_modules/ [email protected] @Eslint loader / index. JS)
Advanced operation of MySQL data table
ctf (hardrce)
游戏玩家问题
File upload vulnerability bypass method
Awesome! VMware esxi installation record, with download