首页 > 其他分享 >Minio分布式文件存储

Minio分布式文件存储

时间:2022-11-07 20:58:53浏览次数:42  
标签:存储 Minio object bucket import Test new minioClient 分布式

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() {

    }
}

 

标签:存储,Minio,object,bucket,import,Test,new,minioClient,分布式
From: https://www.cnblogs.com/wangbiaohistory/p/16867390.html

相关文章

  • 18-存储篇之单机存储(1)_ev
                                         ......
  • 分布式系统中的 CAP
    本文转自:纯洁的微笑公众号在计算机领域,如果是初入行就算了,如果是多年的老码农还不懂CAP定理,那就真的说不过去了。CAP可是每一名技术架构师都必须掌握的基础原则啊。现......
  • <4> 分布式实现
    1"""2配置redis3安装pip3installscrapy-redis4修改scrapy项目(先正常实现scrapy爬虫):5"""67#----1导入分布式爬虫类8fromsc......
  • Linux 挂载Windows共享文件夹和NAS存储
    summary:[Linux挂载共享存储]概述将Windows共享文件夹和NAS存储挂载至Linux。Linux系统环境:CentOS挂载共享存储查看外部主机共享了哪些目录smbclient-L//10.1......
  • C# 中使用 Redis 简单存储
    原文网址:https://www.cnblogs.com/timeddd/p/11117787.htmlRedis是一个开源的使用ANSIC语言编写的支持网络、可基于内存也可持久化的日志型、Key-Value数据库。常......
  • MySQL_存储过程_和函数
    存储过程和函数:类似于Java中的方法 好处:1提高代码的重用性2简化操作3减少了编译次数并且减少了和数据库服务器的连接次数,提高了效率 存储过程含义一组预先......
  • 【分布式技术专题】「架构实践于案例分析」盘点分布式服务的(无状态\有状态)认证实现方
    ⽆状态vs有状态有状态、⽆状态是什么有状态:服务器端需要保存请求的相关信息,每个请求可以默认地使⽤以前的请求信息⽆状态:服务器端不记录请求的相关信息,服务器处理的内容完......
  • 初识微服务(技术栈、单体、分布式架构)、SpringCloud
    (目录)微服务技术栈从单体架构过度到微服务架构,需要一系列中间技术支撑,其中重要的部分包括:注册中心:Eureka、Zookeeper、Nacos服务网关:Zuul、Gateway微服务远程调......
  • 我把分布式音乐播放器适配了Stage模型
     OpenAtomOpenHarmony(以下简称“OpenHarmony”)应用开发自API8及其更早版本一直使用的是FA模型进行开发。FA模型是FeatureAbility的缩写,它和PA(ParticleAbility)两种类型......
  • 基于 MinIO 部署单实例 Databend | 新手篇(1)
    MinIO是个高性能,云原生的对象存储。它提供了与AmazonS3云存储服务兼容的API,使用MinIO为机器学习、分析和应用程序数据工作负载构建高性能基础架构。轻量,操作简单。D......