Minio introductory series [6] Detailed explanation of the use of JAVA integrated Minio object operation API

1 Upload object

1.1 PutObject

Call the PutObject interface to upload files (Object)

public ObjectWriteResponse putObject(PutObjectArgs args)

Precautions:

  • The size of the added Object cannot exceed 5 GB.

  • By default, if an Object with the same name already exists and has access rights to the Object, the newly added Object will overwrite the original Object and return 200 OK.

  • OSS does not have the concept of folders, and all resources are stored in files. However, you can create a simulated folder by creating an Object ending with a forward slash (/) and with a size of 0.

Example 1, InputStream upload:

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        putObject(new File("C:\\Users\\1\\Desktop\\minio.jpg"), "mall4cloud");
    }

    /**
     * 上传文件
     *
     * @param file 需要上传的文件
     * @param bucket 桶名称
     */
    public static void putObject(File file ,String bucket) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        InputStream inputStream = Files.newInputStream(file.toPath());
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucket).object(file.getName()).stream(
                                inputStream, inputStream.available(), -1)
                        .build());
        inputStream.close();
    }

Example 2, InputStream uses SSE-C encryption to upload (will be introduced later):

            minioClient.putObject(                  PutObjectArgs.builder().bucket("mall4cloud").object(file.getName()).stream(
                            bais, bais.available(), -1)
                            .sse(ssec)
                            .build());
            bais.close();

Example 3, InputStream uploads files and adds custom metadata and message headers:

    /**
     * 上传文件(InputStream上传文件,添加自定义元数据及消息头)
     *
     * @param file 需要上传的文件
     * @param bucket 桶名称
     */
    public static void putObject(File file ,String bucket, Map<String, String> headers) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        InputStream inputStream = Files.newInputStream(file.toPath());
        Map<String, String> userMetadata = new HashMap<>();
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucket).object(file.getName()).stream(
                                inputStream, inputStream.available(), -1)
                        .headers(headers)
                        .userMetadata(userMetadata)
                        .build());
        inputStream.close();
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/octet-stream");
        headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
        putObject(new File("C:\\Users\\1\\Desktop\\minio.jpg"), "mall4cloud", headers);
    }

1.2 uploadObject

Upload the contents of the file as objects in the bucket.

public void uploadObject(UploadObjectArgs args)

Example:

            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("mall4cloud")
                            .object("minio.jpg")
                            .filename("C:\\Users\\1\\Desktop\\minio.jpg")
                            .build());

2 Get the object

2.1 getObject

The GetObject interface is used to obtain a file (Object). This operation requires read permission for this Object.

Get the object's data. InputStream must be closed after use to release network resources.

public InputStream getObject(GetObjectArgs args)

Example:

    /**
     * 获取文件
     *
     * @param bucket 桶名称
     * @param filename 文件名
     * @param targetPath 存储的路径
     */
    public static void getObject(String bucket, String filename, String targetPath) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        InputStream stream =
                minioClient.getObject(
                        GetObjectArgs.builder().bucket(bucket).object(filename).build());
        // 读流
        File targetFile = new File(targetPath);
        FileUtils.copyInputStreamToFile(stream, targetFile);
        stream.close();
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        getObject("mall4cloud", "minio.jpg", "C:\\Users\\1\\Desktop\\minio2.jpg");
    }

2.2 downloadObject

Downloads the object's data to a file.

public void downloadObject(DownloadObjectArgs args) 

Example:

    /**
     * 下载文件
     *
     * @param bucket 桶名称
     * @param filename 文件名
     * @param targetPath 存储的路径
     */
    public static void downloadObject(String bucket, String filename, String targetPath) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        minioClient.downloadObject(
                DownloadObjectArgs.builder()
                        .bucket(bucket)
                        .object(filename)
                        .filename(targetPath)
                        .build());
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        downloadObject("mall4cloud", "minio.jpg", "C:\\Users\\1\\Desktop\\minio2.jpg");
    }

2.3 getPresignedObjectUrl

Get an object URL address that specifies the HTTP method, expiration time and custom request parameters, that is, return a signed URL. This address can be provided to third parties who are not logged in to share access or upload objects.

public String getPresignedObjectUrl(GetPresignedObjectUrlArgs args) 

Example:

    /**
     * 获取文件url
     *
     * @param bucket 桶名称
     * @param filename 文件名
     */
    public static String getPresignedObjectUrl(String bucket, String filename) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucket)
                        .object(filename)
                        .expiry(60 * 60 * 24)
                        .build());
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        System.out.println(getPresignedObjectUrl("mall4cloud", "minio.jpg"));
    }

2.4 selectObjectContent

Select the contents of an object through a SQL expression.

public SelectResponseStream selectObjectContent(SelectObjectContentArgs args)

Example:
Upload a file with the following content:
Insert image description here
Example:

    /**
     * 通过 SQL 表达式选择对象的内容
     *
     * @param data 上传的数据
     * @param bucket 桶名称
     * @param filename 文件名
     */
    public static String selectObjectContent(byte[] data, String bucket, String filename) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        // 1. 上传一个文件
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucket).object(filename).stream(
                                byteArrayInputStream, data.length, -1)
                        .build());
        // 调用SQL表达式获取对象
        String sqlExpression = "select * from S3Object";
        InputSerialization is =
                new InputSerialization(null, false, null, null, FileHeaderInfo.USE, null, null, null);
        OutputSerialization os =
                new OutputSerialization(null, null, null, QuoteFields.ASNEEDED, null);
        SelectResponseStream stream =
                minioClient.selectObjectContent(
                        SelectObjectContentArgs.builder()
                                .bucket(bucket)
                                .object(filename)
                                .sqlExpression(sqlExpression)
                                .inputSerialization(is)
                                .outputSerialization(os)
                                .requestProgress(true)
                                .build());

        byte[] buf = new byte[512];
        int bytesRead = stream.read(buf, 0, buf.length);
        return new String(buf, 0, bytesRead, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        List<String> stringList = FileUtil.readLines("C:\\Users\\1\\Desktop\\test.txt", StandardCharsets.UTF_8);
        System.out.println(selectObjectContent(String.join("\n", stringList).getBytes(), "mall4cloud", "test.txt"));
    }

2.5 getPresignedPostFormData

Using this method, obtain the object's upload policy (including signature, file information, path, etc.), and then use this information to upload data using the form data of the POST method. That is to say, a temporarily uploaded information object can be generated, and third parties can use this information to upload files.

Generally, it can be used to request an upload strategy from the front-end, and the back-end returns it to the front-end. The front-end uses Post request + access strategy to upload files. This can be used to integrate minio in a mixed way of JS+SDK.

Insert image description here

public Map<String,String> getPresignedPostFormData(PostPolicy policy)

For example, first we create a Post strategy, and then third parties can use these strategies to directly upload objects using POST. The code is as follows:

    /**
     * 设置并获取Post策略
     *
     * @param bucket 桶名
     */
    public static Map<String, String> setPostPolicy(String bucket) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        // 为存储桶创建一个上传策略,过期时间为7天
        PostPolicy policy = new PostPolicy(bucket, ZonedDateTime.now().plusDays(7));
        // 设置一个参数key,值为上传对象的名称
        policy.addEqualsCondition("key", bucket);
        // 添加Content-Type以"image/"开头,表示只能上传照片
        policy.addStartsWithCondition("Content-Type", "image/");
        // 设置上传文件的大小 64kiB to 10MiB.
        policy.addContentLengthRangeCondition(64 * 1024, 10 * 1024 * 1024);
        return minioClient.getPresignedPostFormData(policy);
    }

    /**
     * 使用Post上传
     * 
     * @param formData Post策略
     * @param url minio服务器地址
     * @param bucket 桶名
     * @param path 本地文件地址
     */
    public static boolean uploadByHttp(Map<String, String> formData, String url, String bucket, String path) throws IOException {
    
    
        // 创建MultipartBody对象
        MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
        multipartBuilder.setType(MultipartBody.FORM);
        for (Map.Entry<String, String> entry : formData.entrySet()) {
    
    
            multipartBuilder.addFormDataPart(entry.getKey(), entry.getValue());
        }
        multipartBuilder.addFormDataPart("key", bucket);
        multipartBuilder.addFormDataPart("Content-Type", "image/png");
        multipartBuilder.addFormDataPart(
                "file", "my-objectname", RequestBody.create(new File(path), null));
        // 模拟第三方,使用OkHttp调用Post上传对象
        Request request =
                new Request.Builder()
                        .url(url + "/" + bucket)
                        .post(multipartBuilder.build())
                        .build();
        OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
        Response response = httpClient.newCall(request).execute();
        return response.isSuccessful();
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        String bucket = "mall4cloud";
        //第一步:设置并获取Post策略
        Map<String, String> formData = setPostPolicy(bucket);
        //第二步:使用Post请求上传对象
        uploadByHttp(formData, url, bucket, "C:\\Users\\1\\Desktop\\minio.jpg");
    }

3 Copy objects

3.1 copyObject

Create an object by copying data from another object server-side

public ObjectWriteResponse copyObject(CopyObjectArgs args)

Example:

    /**
     * 对象拷贝
     *
     * @param sourceBucket 源对象桶
     * @param sourceFilename 源文件名
     * @param targetBucket 目标对象桶
     * @param targetFilename 目标文件名
     */
    public static void copyObject(String sourceBucket, String sourceFilename, String targetBucket, String targetFilename) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        minioClient.copyObject(
                CopyObjectArgs.builder()
                        .bucket(targetBucket)
                        .object(targetFilename)
                        .source(CopySource.builder()
                                        .bucket(sourceBucket)
                                        .object(sourceFilename)
                                        .build())
                        .build());
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        copyObject("mall4cloud", "minio.jpg", "mall4cloud", "minioCopy.jpg");
    }

4 Delete objects

4.1 removeObject

Remove an object

public void removeObject(RemoveObjectArgs args) 

Example:

    /**
     * 删除单个对象
     *
     * @param bucket 桶名称
     * @param filename 文件名
     */
    public static void removeObject(String bucket, String filename) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        minioClient.removeObject(
                RemoveObjectArgs.builder().bucket(bucket).object(filename).build());
    }

    /**
     * 删除指定版本号的对象
     *
     * @param bucket 桶名称
     * @param filename 文件名
     * @param versionId 版本号
     */
    public static void removeObject(String bucket, String filename, String versionId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        //
        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket(bucket)
                        .object(filename)
                        .versionId(versionId)
                        .build());
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        removeObject("mall4cloud", "minioCopy.jpg");
    }

4.2 emoveObjects

Lazy deletion of multiple objects. It needs to iterate over the returned Iterable to perform the deletion.

public Iterable<Result<DeleteError>> removeObjects(RemoveObjectsArgs args) 

Example:

    /**
     * 删除多个文件
     * 
     * @param bucket 桶名称
     * @param filenames 文件名列表
     * @return 返回每个文件的执行情况
     */
    public static Iterable<Result<DeleteError>> removeObjects(String bucket, List<String> filenames){
    
    
        if (ObjectUtils.isEmpty(filenames)) {
    
    
            return new LinkedList<>();
        }
        // 7. 删除多个文件
        List<DeleteObject> objects = new LinkedList<>();
        filenames.forEach(filename-> objects.add(new DeleteObject(filename)));
        return minioClient.removeObjects(
                RemoveObjectsArgs.builder().bucket(bucket).objects(objects).build());
    }

    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        List<String> filenames = new ArrayList<>();
        filenames.add("minioCopy.jpg");
        Iterable<Result<DeleteError>> resultList = removeObjects("mall4cloud", filenames);
        for (Result<DeleteError> result : resultList) {
    
    
            DeleteError error = result.get();
            System.out.println(
                    "Error in deleting object " + error.objectName() + "; " + error.message());
        }
    }

5 Object information query and setting

5.1 Bucket object information list

listObjects lists the object information of the bucket

public Iterable<Result<Item>> listObjects(ListObjectsArgs args)

Example 1, query file information under the bucket:

 Iterable<Result<Item>> results =
                    minioClient.listObjects(ListObjectsArgs.builder().bucket("my-bucketname").build());
            for (Result<Item> result : results) {
    
    
                Item item = result.get();
                System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());
            }

Example 2, recursively query file information under a bucket:

Iterable<Result<Item>> results =
                    minioClient.listObjects(
                            ListObjectsArgs.builder().bucket("my-bucketname").recursive(true).build());

            for (Result<Item> result : results) {
    
    
                Item item = result.get();
                System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());
            }

Example 3, conditional query, specifying prefix, suffix, and maximum quantity:

// 条件查询,指定前缀、后缀、最大数量
            Iterable<Result<Item>> results =
                    minioClient.listObjects(
                            ListObjectsArgs.builder()
                                    .bucket("my-bucketname")
                                    .startAfter("ExampleGuide.pdf")
                                    .prefix("E")
                                    .maxKeys(100)
                                    .build());

            for (Result<Item> result : results) {
    
    
                Item item = result.get();
                System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName());

5.2 Preserve configuration

Get the retained configuration of an object

public Retention getObjectRetention(GetObjectRetentionArgs args) 

Example:

  // 获取对象保留配置
            Retention retention =
                    minioClient.getObjectRetention(
                            GetObjectRetentionArgs.builder()
                                    .bucket("my-bucketname-in-eu-with-object-lock")
                                    .object("k3s-arm64")
                                    .build());

            System.out.println("Mode: " + retention.mode());
            System.out.println("Retainuntil Date: " + retention.retainUntilDate());

To add object retention configuration, the bucket needs to be set to object locking mode and version control is not enabled, otherwise an error will be reported and worm protection will be collected.

public void setObjectLockRetention(SetObjectRetentionArgs)
// 对象保留配置,保留至当前日期后3天。
            ZonedDateTime retentionUntil = ZonedDateTime.now(Time.UTC).plusDays(3).withNano(0);
            Retention retention1 = new Retention(RetentionMode.COMPLIANCE, retentionUntil);
            minioClient.setObjectRetention(
                    SetObjectRetentionArgs.builder()
                            .bucket("my-bucketname-in-eu-with-object-lock")
                            .object("k3s-arm64")
                            .config(retention1)
                            .bypassGovernanceMode(true)
                            .build());

5.3 Labels

Set labels for objects

public void setObjectTags(SetObjectTagsArgs args)

Example:

Map<String, String> map = new HashMap<>();
            map.put("Project", "Project One");
            map.put("User", "jsmith");
            minioClient.setObjectTags(
                    SetObjectTagsArgs.builder()
                            .bucket("my-bucketname")
                            .object("my-objectname")
                            .tags(map)
                            .build());

Get the object's label.

public Tags getObjectTags(GetObjectTagsArgs args) 

Example:

Tags tags = minioClient.getObjectTags(
                            GetObjectTagsArgs.builder().bucket("my-bucketname").object("my-objectname").build());
System.out.println("Object tags: " + tags.get());

Removes an object's label.

private void deleteObjectTags(DeleteObjectTagsArgs args)

Example:

 minioClient.deleteObjectTags(
          DeleteObjectTagsArgs.builder().bucket("my-bucketname").object("my-objectname").build());
      System.out.println("Object tags deleted successfully");

5.4 Legally reserved objects

Enable legal holds on objects

public void enableObjectLegalHold(EnableObjectLegalHoldArgs args) 

Case:

 minioClient.enableObjectLegalHold(
          EnableObjectLegalHoldArgs.builder()
              .bucket("my-bucketname")
              .object("my-objectname")
              .versionId("object-versionId")
              .build());

      System.out.println("Legal hold enabled on object successfully ");

Disable legal holds on objects

public void disableObjectLegalHold(DisableObjectLegalHoldArgs args)

Example:

minioClient.disableObjectLegalHold(
          DisableObjectLegalHoldArgs.builder()
              .bucket("my-bucketname")
              .object("my-objectname")
              .build());
      System.out.println("Legal hold disabled on object successfully ");

5.5 Combined objects

Objects are created by combining data from different source objects using server-side replicas, such as uploading files in parts and then merging them into a single file.

public ObjectWriteResponse composeObject(ComposeObjectArgs args)

Example:

List<ComposeSource> sources = new ArrayList<ComposeSource>();
        sources.add(
            ComposeSource.builder()
                .bucket("my-bucketname-one")
                .object("my-objectname-one")
                .build());
        sources.add(
            ComposeSource.builder()
                .bucket("my-bucketname-two")
                .object("my-objectname-two")
                .build());

        minioClient.composeObject(
            ComposeObjectArgs.builder()
                .bucket("my-destination-bucket")
                .object("my-destination-object")
                .sources(sources)
                .build());
        System.out.println("Object Composed successfully");

5.6 Metadata

Get object information and metadata for an object

public ObjectStat statObject(StatObjectArgs args)

Example:

StatObjectResponse stat =
                    minioClient.statObject(
                            StatObjectArgs.builder()
                                    .bucket("my-bucketname")
                                    .object("start.sh")
                                    .build());
            System.out.println(stat.toString());

6 Tool Reference

import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.*;
import okhttp3.*;
import org.apache.commons.io.FileUtils;
import org.springframework.util.ObjectUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * minio对象操作工具类
 *
 * @author wuKeFan
 * @date 2023-09-19 09:45:43
 */
public class MinioObjectUtil {
    
    

    /**
     * minio地址(自己填写)
     */
    private static final String url = "url";

    /**
     * minio用户名(自己填写)
     */
    private static final String accessKey = "accessKey";

    /**
     * minio密码(自己填写)
     */
    private static final String secretKey = "secretKey";

    private static final MinioClient minioClient;

    static {
    
    
        minioClient = MinioClient.builder().endpoint(url)
                .credentials(accessKey, secretKey).build();
    }

    /**
     * 上传文件(InputStream上传)
     *
     * @param file 需要上传的文件
     * @param bucket 桶名称
     */
    public static void putObject(File file ,String bucket) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        InputStream inputStream = Files.newInputStream(file.toPath());
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucket).object(file.getName()).stream(
                                inputStream, inputStream.available(), -1)
                        .build());
        inputStream.close();
    }

    /**
     * 上传文件(InputStream使用SSE-C加密上传)
     *
     * @param file 需要上传的文件
     * @param bucket 桶名称
     */
    public static void putObject(File file ,String bucket, ServerSideEncryption ssec) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        InputStream inputStream = Files.newInputStream(file.toPath());
        minioClient.putObject(
                PutObjectArgs.builder().bucket("mall4cloud").object(file.getName()).stream(
                                inputStream, inputStream.available(), -1)
                        .sse(ssec)
                        .build());
        inputStream.close();
    }

    /**
     * 上传文件(InputStream上传文件,添加自定义元数据及消息头)
     *
     * @param file 需要上传的文件
     * @param bucket 桶名称
     */
    public static void putObject(File file ,String bucket, Map<String, String> headers) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        InputStream inputStream = Files.newInputStream(file.toPath());
        Map<String, String> userMetadata = new HashMap<>();
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucket).object(file.getName()).stream(
                                inputStream, inputStream.available(), -1)
                        .headers(headers)
                        .userMetadata(userMetadata)
                        .build());
        inputStream.close();
    }

    /**
     * 获取文件
     *
     * @param bucket 桶名称
     * @param filename 文件名
     * @param targetPath 存储的路径
     */
    public static void getObject(String bucket, String filename, String targetPath) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        InputStream stream =
                minioClient.getObject(
                        GetObjectArgs.builder().bucket(bucket).object(filename).build());
        // 读流
        File targetFile = new File(targetPath);
        FileUtils.copyInputStreamToFile(stream, targetFile);
        stream.close();
    }

    /**
     * 下载文件
     *
     * @param bucket 桶名称
     * @param filename 文件名
     * @param targetPath 存储的路径
     */
    public static void downloadObject(String bucket, String filename, String targetPath) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        minioClient.downloadObject(
                DownloadObjectArgs.builder()
                        .bucket(bucket)
                        .object(filename)
                        .filename(targetPath)
                        .build());
    }

    /**
     * 获取文件url
     *
     * @param bucket 桶名称
     * @param filename 文件名
     */
    public static String getPresignedObjectUrl(String bucket, String filename) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucket)
                        .object(filename)
                        .expiry(60 * 60 * 24)
                        .build());
    }

    /**
     * 通过 SQL 表达式选择对象的内容
     *
     * @param data 上传的数据
     * @param bucket 桶名称
     * @param filename 文件名
     */
    public static String selectObjectContent(byte[] data, String bucket, String filename) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        // 1. 上传一个文件
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucket).object(filename).stream(
                                byteArrayInputStream, data.length, -1)
                        .build());
        // 调用SQL表达式获取对象
        String sqlExpression = "select * from S3Object";
        InputSerialization is =
                new InputSerialization(null, false, null, null, FileHeaderInfo.USE, null, null, null);
        OutputSerialization os =
                new OutputSerialization(null, null, null, QuoteFields.ASNEEDED, null);
        SelectResponseStream stream =
                minioClient.selectObjectContent(
                        SelectObjectContentArgs.builder()
                                .bucket(bucket)
                                .object(filename)
                                .sqlExpression(sqlExpression)
                                .inputSerialization(is)
                                .outputSerialization(os)
                                .requestProgress(true)
                                .build());

        byte[] buf = new byte[512];
        int bytesRead = stream.read(buf, 0, buf.length);
        return new String(buf, 0, bytesRead, StandardCharsets.UTF_8);
    }

    /**
     * 设置并获取Post策略
     *
     * @param bucket 桶名
     */
    public static Map<String, String> setPostPolicy(String bucket) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        // 为存储桶创建一个上传策略,过期时间为7天
        PostPolicy policy = new PostPolicy(bucket, ZonedDateTime.now().plusDays(7));
        // 设置一个参数key,值为上传对象的名称
        policy.addEqualsCondition("key", bucket);
        // 添加Content-Type以"image/"开头,表示只能上传照片
        policy.addStartsWithCondition("Content-Type", "image/");
        // 设置上传文件的大小 64kiB to 10MiB.
        policy.addContentLengthRangeCondition(64 * 1024, 10 * 1024 * 1024);
        return minioClient.getPresignedPostFormData(policy);
    }

    /**
     * 使用Post上传
     *
     * @param formData Post策略
     * @param url minio服务器地址
     * @param bucket 桶名
     * @param path 本地文件地址
     */
    public static boolean uploadByHttp(Map<String, String> formData, String url, String bucket, String path) throws IOException {
    
    
        // 创建MultipartBody对象
        MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
        multipartBuilder.setType(MultipartBody.FORM);
        for (Map.Entry<String, String> entry : formData.entrySet()) {
    
    
            multipartBuilder.addFormDataPart(entry.getKey(), entry.getValue());
        }
        multipartBuilder.addFormDataPart("key", bucket);
        multipartBuilder.addFormDataPart("Content-Type", "image/png");
        multipartBuilder.addFormDataPart(
                "file", "my-objectname", RequestBody.create(new File(path), null));
        // 模拟第三方,使用OkHttp调用Post上传对象
        Request request =
                new Request.Builder()
                        .url(url + "/" + bucket)
                        .post(multipartBuilder.build())
                        .build();
        OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
        Response response = httpClient.newCall(request).execute();
        return response.isSuccessful();
    }

    /**
     * 对象拷贝
     *
     * @param sourceBucket 源对象桶
     * @param sourceFilename 源文件名
     * @param targetBucket 目标对象桶
     * @param targetFilename 目标文件名
     */
    public static void copyObject(String sourceBucket, String sourceFilename, String targetBucket, String targetFilename) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        minioClient.copyObject(
                CopyObjectArgs.builder()
                        .bucket(targetBucket)
                        .object(targetFilename)
                        .source(CopySource.builder()
                                        .bucket(sourceBucket)
                                        .object(sourceFilename)
                                        .build())
                        .build());
    }

    /**
     * 删除单个对象
     *
     * @param bucket 桶名称
     * @param filename 文件名
     */
    public static void removeObject(String bucket, String filename) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        minioClient.removeObject(
                RemoveObjectArgs.builder().bucket(bucket).object(filename).build());
    }

    /**
     * 删除指定版本号的对象
     *
     * @param bucket 桶名称
     * @param filename 文件名
     * @param versionId 版本号
     */
    public static void removeObject(String bucket, String filename, String versionId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    
    
        //
        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket(bucket)
                        .object(filename)
                        .versionId(versionId)
                        .build());
    }

    /**
     * 删除多个文件
     *
     * @param bucket 桶名称
     * @param filenames 文件名列表
     * @return 返回每个文件的执行情况
     */
    public static Iterable<Result<DeleteError>> removeObjects(String bucket, List<String> filenames){
    
    
        if (ObjectUtils.isEmpty(filenames)) {
    
    
            return new LinkedList<>();
        }
        // 7. 删除多个文件
        List<DeleteObject> objects = new LinkedList<>();
        filenames.forEach(filename-> objects.add(new DeleteObject(filename)));
        return minioClient.removeObjects(
                RemoveObjectsArgs.builder().bucket(bucket).objects(objects).build());
    }

    public static void main(String[] args) {
    
    

    }

}

Guess you like

Origin blog.csdn.net/qq_37284798/article/details/133014533