minio教程 给出代码案例 其中包括文件的上传下载 已经上传前的压缩 基于springboot
时间: 2024-06-10 12:04:47 浏览: 136
基于springboot实现的文件下载上传
很好的问题!对于Minio的基本操作,您可以使用MinIO Java Client SDK来实现。这里是一个包括文件上传、下载和压缩的代码示例(基于Spring Boot):
```
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Component
public class MinioService {
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.url}")
private String url;
@Value("${minio.bucket-name}")
private String bucketName;
private MinioClient generateMinioClient() throws InvalidPortException, InvalidEndpointException {
return new MinioClient(url, accessKey, secretKey);
}
public void uploadFile(String fileName, InputStream inputStream, long size, String contentType) throws IOException, InterruptedException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, ErrorResponseException, RegionConflictException, InsufficientDataException, InternalException {
MinioClient minioClient = generateMinioClient();
if(!minioClient.bucketExists(bucketName)) {
minioClient.makeBucket(bucketName);
}
minioClient.putObject(bucketName, fileName, inputStream, size, contentType);
}
public InputStream downloadFile(String fileName) throws IOException, InterruptedException, InvalidKeyException, InvalidResponseException, ErrorResponseException, NoSuchAlgorithmException, InvalidArgumentException, InternalException {
MinioClient minioClient = generateMinioClient();
return minioClient.getObject(bucketName, fileName);
}
public void compressAndUploadFile(String fileName, InputStream inputStream, long size, String contentType) throws IOException, InterruptedException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, ErrorResponseException, RegionConflictException, InsufficientDataException, InternalException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
byte[] fileBytes = inputStream.readAllBytes();
zipOutputStream.write(fileBytes);
zipOutputStream.closeEntry();
zipOutputStream.close();
ByteArrayResource byteArrayResource = new ByteArrayResource(byteArrayOutputStream.toByteArray());
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
uploadFile(fileName + ".zip", byteArrayInputStream, byteArrayResource.contentLength(), contentType);
}
public void deleteFile(String fileName) throws IOException, InterruptedException, InvalidKeyException, InvalidResponseException, ErrorResponseException, NoSuchAlgorithmException, InvalidArgumentException, InternalException {
MinioClient minioClient = generateMinioClient();
minioClient.removeObject(bucketName, fileName);
}
}
```
通过调用 `MinioService` 类中的方法,您可以轻松完成上传、下载和压缩文件的操作。希望这个示例能有所帮助!
阅读全文