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

  • getObject 获取对象

主要参数

参数 类型 描述
bucketName String 存储桶名称
objectName String 存储桶里的对象名称

可是就在这时问题来了…按之前的理解 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");
	}
 

你可能感兴趣的:(SpringBoot 对Minio对象存储服务操作,关于多级目录如何操作)