SpringBoot 对Minio对象存储服务操作,关于多级目录如何操作

实现功能

按我个人理解,桶(文件夹),比如现在在minio的目录中创建一个文件夹(log),然后在log文件夹中创建子文件夹(loginLog),然后在loginLog文件夹中添加一个日志文件 2019-8-26-xxxxx.txt

步骤:
  • 1、先创建文件夹 log/loginLog
  • 2、创建文件 2019-8-26-xxxxx.txt
  • 3、调用API public InputStream getObject(String bucketName, String objectName)
官方API
主要参数
参数类型描述
bucketNameString存储桶名称
objectNameString存储桶里的对象名称

可是就在这时问题来了…按之前的理解 bucketName = “log/loginLog”, objectName = “2019-8-26-xxxxx.txt”
调用getObject方法的时候报异常了 bucket name does not follow Amazon S3 standards. For more information refer http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
之前只要一个目录是是没问题的,按这个理解,这样传的参数没有错,然后各种百度,google,github 翻遍了。。没有找到解决方法

后来

后来看了AWS的文档,bucket是一个全局唯一的,可理解为根目录,或项目名,或总节点
最后测试

String bucketName = "log";
String objectName = "loginLog/2019-8-26-xxxxx.txt";
InputStream stream = minioClient.getObject(bucketName,objectName);

发现成功了…
最后贴几个常用的文件上传类型
比如图片要预览功能,就要设置 contextType = "image/jpeg"

/**
	 * 
	 * @Title: uploadImage
	 * @Description:上传图片
	 * @param inputStream
	 * @param suffix
	 * @return
	 * @throws Exception
	 */
	public static JSONObject uploadImage(InputStream inputStream, String suffix) throws Exception {
		return upload(inputStream, suffix, "image/jpeg");
	}
 
 
	/**
	 * @Title: uploadVideo
	 * @Description:上传视频
	 * @param inputStream
	 * @param suffix
	 * @return
	 * @throws Exception
	 */
	public static JSONObject uploadVideo(InputStream inputStream, String suffix) throws Exception {
		return upload(inputStream, suffix, "video/mp4");
	}
 
 
	/**
	 * @Title: uploadVideo
	 * @Description:上传文件
	 * @param inputStream
	 * @param suffix
	 * @return
	 * @throws Exception
	 */
	public static JSONObject uploadFile(InputStream inputStream, String suffix) throws Exception {
		return upload(inputStream, suffix, "application/octet-stream");
	}
 
 
	/**
	 * 上传字符串大文本内容
	 * 
	 * @Title: uploadString
	 * @Description:描述方法
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static JSONObject uploadString(String str) throws Exception {
		if (!StringUtils.notNullAndEmpty(str)) {
			return new JSONObject();
		}
		InputStream inputStream = new ByteArrayInputStream(str.getBytes());
		return upload(inputStream, null, "text/html");
	}
 
  • 13
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
要实现Spring Boot整合MinIO实现多级目录下文件的下载,可以按照以下步骤进行: 1. 引入MinIO的依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.1.0</version> </dependency> ``` 2. 配置MinIO连接信息 在application.yml文件中添加以下配置信息: ```yaml minio: endpoint: http://localhost:9000 # MinIO服务地址 accessKey: minioadmin # 访问Key secretKey: minioadmin # 访问Secret bucketName: test-bucket # 存储桶名称 ``` 3. 创建MinIO客户端 创建MinIO客户端的代码如下: ```java @Configuration public class MinioConfig { @Value("${minio.endpoint}") private String endpoint; @Value("${minio.accessKey}") private String accessKey; @Value("${minio.secretKey}") private String secretKey; @Value("${minio.bucketName}") private String bucketName; @Bean public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException { return MinioClient.builder() .endpoint(endpoint) .credentials(accessKey, secretKey) .build(); } @Bean public String bucketName() { return bucketName; } } ``` 4. 实现文件下载接口 实现文件下载接口的代码如下: ```java @RestController @RequestMapping("/file") public class FileController { @Autowired private MinioClient minioClient; @Autowired private String bucketName; @GetMapping("/download") public ResponseEntity<Resource> downloadFile(@RequestParam("path") String path) throws Exception { String[] pathArr = path.split("/"); String objectName = pathArr[pathArr.length - 1]; String objectPath = path.substring(0, path.lastIndexOf("/") + 1); InputStream inputStream = minioClient.getObject(GetObjectArgs.builder() .bucket(bucketName) .object(objectPath + objectName) .build()); ByteArrayResource resource = new ByteArrayResource(inputStream.readAllBytes()); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + objectName + "\"") .body(resource); } } ``` 其中,`path`参数是要下载的文件路径,例如:`folder1/folder2/test.txt`。 5. 测试文件下载接口 启动应用程序后,访问`http://localhost:8080/file/download?path=folder1/folder2/test.txt`即可下载名为`test.txt`的文件,该文件位于MinIO存储桶的`folder1/folder2/`路径下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值