Java Code Examples for io.minio.MinioClient#makeBucket()

The following examples show how to use io.minio.MinioClient#makeBucket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThumbnailsFunction.java    From docs with Apache License 2.0 6 votes vote down vote up
/**
 * Uploads the provided data to the storage server, as an object named as specified.
 *
 * @param imageBuffer the image data to upload
 * @param objectName the name of the remote object to create
 */
private void objectUpload(byte[] imageBuffer, String objectName) {
    try {
        MinioClient minioClient = new MinioClient(storageUrl, storageAccessKey, storageSecretKey);

        // Ensure the bucket exists.
        if(!minioClient.bucketExists("alpha")) {
            minioClient.makeBucket("alpha");
        }

        // Upload the image to the bucket with putObject
        minioClient.putObject("alpha", objectName, new ByteArrayInputStream(imageBuffer), imageBuffer.length, "application/octet-stream");
    } catch(Exception e) {
        System.err.println("Error occurred: " + e);
        e.printStackTrace();
    }
}
 
Example 2
Source File: MinIOFileManage.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
/**
 * 如果存储桶不存在 创建存储通
 *
 * @param os
 * @param minioClient
 * @throws Exception
 */
private void checkBucket(OssSetting os, MinioClient minioClient) throws Exception {

    // 如果存储桶不存在 创建存储通
    if (!minioClient.bucketExists(os.getBucket())) {
        minioClient.makeBucket(os.getBucket());
        // 设置隐私权限 公开读
        String builder = "{\n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Action\": [\n" +
                "                \"s3:GetBucketLocation\",\n" +
                "                \"s3:ListBucket\"\n" +
                "            ],\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": \"*\",\n" +
                "            \"Resource\": \"arn:aws:s3:::" + os.getBucket() + "\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"Action\": \"s3:GetObject\",\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": \"*\",\n" +
                "            \"Resource\": \"arn:aws:s3:::" + os.getBucket() + "/*\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"Version\": \"2012-10-17\"\n" +
                "}\n";
        minioClient.setBucketPolicy(os.getBucket(), builder);
    }
}
 
Example 3
Source File: MinioTemplate.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
/**
 * Bucket Operations
 */
public void createBucket(String bucketName) throws XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, IOException, RegionConflictException, NoResponseException, InternalException, ErrorResponseException, InsufficientDataException, InvalidBucketNameException {
    MinioClient client = getMinioClient();
    if(!client.bucketExists(bucketName)){
        client.makeBucket(bucketName);
    }
}
 
Example 4
Source File: MinioController.java    From mall-learning with Apache License 2.0 5 votes vote down vote up
@ApiOperation("文件上传")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public CommonResult upload(@RequestParam("file") MultipartFile file) {
    try {
        //创建一个MinIO的Java客户端
        MinioClient minioClient = new MinioClient(ENDPOINT, ACCESS_KEY, SECRET_KEY);
        boolean isExist = minioClient.bucketExists(BUCKET_NAME);
        if (isExist) {
            LOGGER.info("存储桶已经存在!");
        } else {
            //创建存储桶并设置只读权限
            minioClient.makeBucket(BUCKET_NAME);
            minioClient.setBucketPolicy(BUCKET_NAME, "*.*", PolicyType.READ_ONLY);
        }
        String filename = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 设置存储对象名称
        String objectName = sdf.format(new Date()) + "/" + filename;
        // 使用putObject上传一个文件到存储桶中
        minioClient.putObject(BUCKET_NAME, objectName, file.getInputStream(), file.getContentType());
        LOGGER.info("文件上传成功!");
        MinioUploadDto minioUploadDto = new MinioUploadDto();
        minioUploadDto.setName(filename);
        minioUploadDto.setUrl(ENDPOINT + "/" + BUCKET_NAME + "/" + objectName);
        return CommonResult.success(minioUploadDto);
    } catch (Exception e) {
        LOGGER.info("上传发生错误: {}!", e.getMessage());
    }
    return CommonResult.failed();
}
 
Example 5
Source File: MinioController.java    From mall-swarm with Apache License 2.0 5 votes vote down vote up
@ApiOperation("文件上传")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public CommonResult upload(@RequestParam("file") MultipartFile file) {
    try {
        //创建一个MinIO的Java客户端
        MinioClient minioClient = new MinioClient(ENDPOINT, ACCESS_KEY, SECRET_KEY);
        boolean isExist = minioClient.bucketExists(BUCKET_NAME);
        if (isExist) {
            LOGGER.info("存储桶已经存在!");
        } else {
            //创建存储桶并设置只读权限
            minioClient.makeBucket(BUCKET_NAME);
            minioClient.setBucketPolicy(BUCKET_NAME, "*.*", PolicyType.READ_ONLY);
        }
        String filename = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 设置存储对象名称
        String objectName = sdf.format(new Date()) + "/" + filename;
        // 使用putObject上传一个文件到存储桶中
        minioClient.putObject(BUCKET_NAME, objectName, file.getInputStream(), file.getContentType());
        LOGGER.info("文件上传成功!");
        MinioUploadDto minioUploadDto = new MinioUploadDto();
        minioUploadDto.setName(filename);
        minioUploadDto.setUrl(ENDPOINT + "/" + BUCKET_NAME + "/" + objectName);
        return CommonResult.success(minioUploadDto);
    } catch (Exception e) {
        LOGGER.info("上传发生错误: {}!", e.getMessage());
    }
    return CommonResult.failed();
}
 
Example 6
Source File: KnoteJavaApplication.java    From knote-java with Apache License 2.0 5 votes vote down vote up
private void initMinio() throws InterruptedException {
    boolean success = false;
    while (!success) {
        try {
            minioClient = new MinioClient("http://" + properties.getMinioHost() + ":9000" ,
                                            properties.getMinioAccessKey(),
                                            properties.getMinioSecretKey(),
                                    false);
            // Check if the bucket already exists.
            boolean isExist = minioClient.bucketExists(properties.getMinioBucket());
            if (isExist) {
                System.out.println("> Bucket already exists.");
            } else {
                minioClient.makeBucket(properties.getMinioBucket());
            }
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("> Minio Reconnect: " + properties.isMinioReconnectEnabled());
            if (properties.isMinioReconnectEnabled()) {
                Thread.sleep(5000);
            } else {
                success = true;

            }
        }
    }
    System.out.println("> Minio initialized!");
}
 
Example 7
Source File: MinioController.java    From mall with Apache License 2.0 5 votes vote down vote up
@ApiOperation("文件上传")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public CommonResult upload(@RequestParam("file") MultipartFile file) {
    try {
        //创建一个MinIO的Java客户端
        MinioClient minioClient = new MinioClient(ENDPOINT, ACCESS_KEY, SECRET_KEY);
        boolean isExist = minioClient.bucketExists(BUCKET_NAME);
        if (isExist) {
            LOGGER.info("存储桶已经存在!");
        } else {
            //创建存储桶并设置只读权限
            minioClient.makeBucket(BUCKET_NAME);
            minioClient.setBucketPolicy(BUCKET_NAME, "*.*", PolicyType.READ_ONLY);
        }
        String filename = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 设置存储对象名称
        String objectName = sdf.format(new Date()) + "/" + filename;
        // 使用putObject上传一个文件到存储桶中
        minioClient.putObject(BUCKET_NAME, objectName, file.getInputStream(), file.getContentType());
        LOGGER.info("文件上传成功!");
        MinioUploadDto minioUploadDto = new MinioUploadDto();
        minioUploadDto.setName(filename);
        minioUploadDto.setUrl(ENDPOINT + "/" + BUCKET_NAME + "/" + objectName);
        return CommonResult.success(minioUploadDto);
    } catch (Exception e) {
        LOGGER.info("上传发生错误: {}!", e.getMessage());
    }
    return CommonResult.failed();
}