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

The following examples show how to use io.minio.MinioClient#putObject() . 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
@Override
public String pathUpload(String filePath, String key) {

    OssSetting os = getOssSetting();
    try {
        MinioClient minioClient = new MinioClient(os.getHttp() + os.getEndpoint(), os.getAccessKey(), os.getSecretKey());
        checkBucket(os, minioClient);
        minioClient.putObject(os.getBucket(), key, filePath, null, null, null, null);
    } catch (Exception e) {
        throw new SkException("上传出错,请检查MinIO配置");
    }
    return os.getHttp() + os.getEndpoint() + "/" + os.getBucket() + "/" + key;
}
 
Example 3
Source File: MinIOFileManage.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
@Override
public String inputStreamUpload(InputStream inputStream, String key, MultipartFile file) {

    OssSetting os = getOssSetting();
    try {
        MinioClient minioClient = new MinioClient(os.getHttp() + os.getEndpoint(), os.getAccessKey(), os.getSecretKey());
        checkBucket(os, minioClient);
        minioClient.putObject(os.getBucket(), key, inputStream, file.getSize(), null, null, file.getContentType());
    } catch (Exception e) {
        throw new SkException("上传出错,请检查MinIO配置");
    }
    return os.getHttp() + os.getEndpoint() + "/" + os.getBucket() + "/" + key;
}
 
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 jeecg-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件到minio服务
 *
 * @param file
 * @return
 */
@PostMapping("upload")
public String upload(@RequestParam("file") MultipartFile file) {
    try {
        MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
        InputStream is = file.getInputStream(); //得到文件流
        String fileName = "/upload/img/" + file.getOriginalFilename(); //文件名
        String contentType = file.getContentType();  //类型
        minioClient.putObject(bucketName, fileName, is, contentType); //把文件放置Minio桶(文件夹)
        return "上传成功";
    } catch (Exception e) {
        return "上传失败";
    }
}
 
Example 6
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 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();
}
 
Example 8
Source File: MinioController.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件到minio服务
 *
 * @param file
 * @return
 */
@PostMapping("upload")
public String upload(@RequestParam("file") MultipartFile file) {
    try {
        MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
        InputStream is = file.getInputStream(); //得到文件流
        String fileName = "/upload/img/" + file.getOriginalFilename(); //文件名
        String contentType = file.getContentType();  //类型
        minioClient.putObject(bucketName, fileName, is, contentType); //把文件放置Minio桶(文件夹)
        return "上传成功";
    } catch (Exception e) {
        return "上传失败";
    }
}
 
Example 9
Source File: S3CacheManager.java    From simpleci with MIT License 5 votes vote down vote up
@Override
public void uploadCache(JobOutputProcessor outputProcessor, String cachePath) {
    try {
            outputProcessor.output(String.format("Uploading cache file %s to s3 server %s\n", cachePath, settings.endPoint));

            MinioClient minioClient = new MinioClient(settings.endPoint, settings.accessKey, settings.secretKey);
            minioClient.putObject(settings.bucketName, cacheFileName, cachePath);
            outputProcessor.output("Cache uploaded\n");
    } catch (Exception e) {
        outputProcessor.output("Error upload cache: " + e.getMessage() + "\n");
    }
}
 
Example 10
Source File: AdminStorageAction.java    From fess with Apache License 2.0 5 votes vote down vote up
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);
        minioClient.putObject(fessConfig.getStorageBucket(), objectName, in, new PutObjectOptions(uploadFile.getFileSize(), -1));
    } catch (final Exception e) {
        throw new StorageException("Failed to upload " + objectName, e);
    }
}