Search in sources :

Example 1 with MinioClient

use of io.minio.MinioClient in project fess by codelibs.

the class AdminStorageAction method getFileItems.

public static List<Map<String, Object>> getFileItems(final String prefix) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final ArrayList<Map<String, Object>> list = new ArrayList<>();
    try {
        final MinioClient minioClient = createClient(fessConfig);
        final ListObjectsArgs args = ListObjectsArgs.builder().bucket(fessConfig.getStorageBucket()).prefix(prefix != null && prefix.length() > 0 ? prefix + "/" : prefix).recursive(false).includeUserMetadata(false).useApiVersion1(false).build();
        for (final Result<Item> result : minioClient.listObjects(args)) {
            final Map<String, Object> map = new HashMap<>();
            final Item item = result.get();
            final String objectName = item.objectName();
            map.put("id", encodeId(objectName));
            map.put("name", getName(objectName));
            map.put("hashCode", item.hashCode());
            map.put("size", item.size());
            map.put("directory", item.isDir());
            if (!item.isDir()) {
                map.put("lastModified", item.lastModified());
            }
            list.add(map);
            if (list.size() > fessConfig.getStorageMaxItemsInPageAsInteger()) {
                break;
            }
        }
    } catch (final ErrorResponseException e) {
        final String code = e.errorResponse().code();
        if ("NoSuchBucket".equals(code)) {
            final MinioClient minioClient = createClient(fessConfig);
            try {
                final MakeBucketArgs args = MakeBucketArgs.builder().bucket(fessConfig.getStorageBucket()).build();
                minioClient.makeBucket(args);
                logger.info("Created bucket: {}", fessConfig.getStorageBucket());
            } catch (final Exception e1) {
                logger.warn("Failed to create bucket: {}", fessConfig.getStorageBucket(), e1);
            }
        } else if (logger.isDebugEnabled()) {
            logger.debug("Failed to access {}", fessConfig.getStorageEndpoint(), e);
        }
    } catch (final Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to access {}", fessConfig.getStorageEndpoint(), e);
        }
    }
    return list;
}
Also used : MakeBucketArgs(io.minio.MakeBucketArgs) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StorageException(org.codelibs.fess.exception.StorageException) ErrorResponseException(io.minio.errors.ErrorResponseException) ListObjectsArgs(io.minio.ListObjectsArgs) Item(io.minio.messages.Item) MinioClient(io.minio.MinioClient) ErrorResponseException(io.minio.errors.ErrorResponseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MinioClient

use of io.minio.MinioClient in project yyl_example by Relucent.

the class MinioExample method main.

public static void main(String[] args) throws Exception {
    try {
        String endpoint = "http://localhost:9000";
        String accessKey = "minioadmin";
        String secretKey = "minioadmin";
        String bucketName = "test" + System.currentTimeMillis();
        MinioClient client = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
        boolean exists = client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists) {
            client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
        String objectName = "hello.txt";
        byte[] content = "hello".getBytes();
        String contentType = "text/plain";
        try (InputStream input = new ByteArrayInputStream(content)) {
            int length = content.length;
            Map<String, String> headers = new HashMap<>();
            client.putObject(// 
            PutObjectArgs.builder().bucket(// 
            bucketName).object(// 
            objectName).stream(input, length, // 
            -1).contentType(// 
            contentType).headers(// 
            headers).build());
        }
        StatObjectResponse stat = client.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
        System.out.println(stat);
        try (InputStream input = client.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build())) {
            byte[] data = IOUtils.toByteArray(input);
            System.out.println(new String(data));
        }
        client.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
        if (!exists) {
            client.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
        }
    } catch (MinioException e) {
        e.printStackTrace();
    }
}
Also used : MinioClient(io.minio.MinioClient) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StatObjectResponse(io.minio.StatObjectResponse) MinioException(io.minio.errors.MinioException)

Example 3 with MinioClient

use of io.minio.MinioClient in project fess by codelibs.

the class AdminStorageAction method deleteObject.

public static void deleteObject(final String objectName) {
    try {
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        final MinioClient minioClient = createClient(fessConfig);
        final RemoveObjectArgs args = RemoveObjectArgs.builder().bucket(fessConfig.getStorageBucket()).object(objectName).build();
        minioClient.removeObject(args);
    } catch (final Exception e) {
        throw new StorageException("Failed to delete " + objectName, e);
    }
}
Also used : MinioClient(io.minio.MinioClient) RemoveObjectArgs(io.minio.RemoveObjectArgs) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StorageException(org.codelibs.fess.exception.StorageException) StorageException(org.codelibs.fess.exception.StorageException) ErrorResponseException(io.minio.errors.ErrorResponseException)

Example 4 with MinioClient

use of io.minio.MinioClient in project fess by codelibs.

the class AdminStorageAction method uploadObject.

public static void uploadObject(final String objectName, final MultipartFormFile uploadFile) {
    try (final InputStream in = uploadFile.getInputStream()) {
        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        final MinioClient minioClient = createClient(fessConfig);
        final PutObjectArgs args = PutObjectArgs.builder().bucket(fessConfig.getStorageBucket()).object(objectName).stream(in, uploadFile.getFileSize(), -1).contentType("application/octet-stream").build();
        minioClient.putObject(args);
    } catch (final Exception e) {
        throw new StorageException("Failed to upload " + objectName, e);
    }
}
Also used : MinioClient(io.minio.MinioClient) InputStream(java.io.InputStream) PutObjectArgs(io.minio.PutObjectArgs) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StorageException(org.codelibs.fess.exception.StorageException) StorageException(org.codelibs.fess.exception.StorageException) ErrorResponseException(io.minio.errors.ErrorResponseException)

Aggregations

MinioClient (io.minio.MinioClient)4 ErrorResponseException (io.minio.errors.ErrorResponseException)3 StorageException (org.codelibs.fess.exception.StorageException)3 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 ListObjectsArgs (io.minio.ListObjectsArgs)1 MakeBucketArgs (io.minio.MakeBucketArgs)1 PutObjectArgs (io.minio.PutObjectArgs)1 RemoveObjectArgs (io.minio.RemoveObjectArgs)1 StatObjectResponse (io.minio.StatObjectResponse)1 MinioException (io.minio.errors.MinioException)1 Item (io.minio.messages.Item)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1