请提供minio + spring boot 多级文件夹下的文件批量上传代码
时间: 2023-12-02 12:05:14 浏览: 48
好的,以下是使用Minio和Spring Boot实现多级文件夹下的文件批量上传的代码:
```
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Service
public class MinioService {
@Autowired
private MinioClient minioClient;
@Value("${minio.bucket-name}")
private String bucketName;
public void uploadFiles(String parentFolder, MultipartFile[] files) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, InsufficientDataException, XmlParserException, InternalException, ErrorResponseException {
for(MultipartFile file: files) {
String fileName = file.getOriginalFilename();
String[] folders = parentFolder.split("/");
String folderPath = "";
for(String folder: folders) {
if(!folder.isEmpty()) {
folderPath += folder + "/";
if(!minioClient.bucketExists(bucketName + "/" + folderPath)) {
minioClient.makeBucket(bucketName + "/" + folderPath);
}
}
}
minioClient.putObject(bucketName, parentFolder + "/" + fileName, file.getInputStream(), file.getSize(), file.getContentType());
}
}
}
```
这里使用了Minio Java客户端来与Minio进行交互,通过`minioClient.bucketExists`方法判断是否存在该文件夹,如果不存在则创建,然后通过`minioClient.putObject`方法上传文件。
在application.properties文件中需要配置如下属性:
```
minio.endpoint=http://localhost:9000
minio.access-key=minio
minio.secret-key=minio123
minio.bucket-name=my-bucket
```
其中`minio.endpoint`为Minio服务的地址,`minio.access-key`和`minio.secret-key`为访问Minio服务的秘钥,`minio.bucket-name`为要上传到的桶的名称。
希望这个代码对你有所帮助!