io.github.ncc0706.oss.MinioService Maven / Gradle / Ivy
The newest version!
package io.github.ncc0706.oss;
import io.minio.*;
import io.minio.errors.ErrorResponseException;
import io.minio.http.Method;
import io.minio.messages.DeleteObject;
import io.minio.messages.ErrorResponse;
import io.minio.messages.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.utils.IoUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* Created by IntelliJ IDEA.
*
* @author NiuYuxian
* @since 2024-01-13 15:02:00
*/
public class MinioService extends AbstractOssService {
private final Logger logger = LoggerFactory.getLogger(getClass());
private MinioClient minioClient;
public MinioService(MinioClient minioClient) {
this.minioClient = minioClient;
}
@Override
public boolean exitsBucket(String bucket) {
try {
return this.minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
} catch (Exception e) {
logger.error("", e);
return false;
}
}
@Override
public void createBucket(String bucket) {
try {
MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder()
.bucket(bucket)
.build();
minioClient.makeBucket(makeBucketArgs);
} catch (Exception e) {
logger.error("create bucket {} error, ", e);
throw new RuntimeException("create bucket error");
}
}
@Override
public void deleteBucket(String bucket) {
try {
RemoveBucketArgs removeBucketArgs = RemoveBucketArgs.builder().bucket(bucket).build();
minioClient.removeBucket(removeBucketArgs);
} catch (Exception e) {
if (e instanceof ErrorResponseException) {
ErrorResponseException exception = (ErrorResponseException) e;
ErrorResponse errorResponse = exception.errorResponse();
String code = errorResponse.code();
String message = exception.getMessage();
System.out.println(code + " " + message);
logger.error("delete bucket [{}] error, code: {}, message: {}", bucket, code, message);
}
throw new RuntimeException("delete bucket error");
}
}
@Override
public void putObjectFromBytes(String bucket, String key, byte[] bytes) {
try {
InputStream inputStream = new ByteArrayInputStream(bytes);
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucket)
.object(key)
.stream(inputStream, bytes.length, -1)
.build();
minioClient.putObject(putObjectArgs);
} catch (Exception e) {
throw new RuntimeException("put object error");
}
}
@Override
public void putObjectFile(String bucket, String key, File file) {
try {
InputStream inputStream = new FileInputStream(file);
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucket)
.object(key)
.stream(inputStream, inputStream.available(), -1)
.build();
minioClient.putObject(putObjectArgs);
} catch (Exception e) {
throw new RuntimeException("put object error");
}
}
@Override
public void putObjectFromInputStream(String bucket, String key, InputStream inputStream) {
try {
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucket)
.object(key)
.stream(inputStream, inputStream.available(), -1)
.build();
minioClient.putObject(putObjectArgs);
} catch (Exception e) {
throw new RuntimeException("put object error");
}
}
@Override
public byte[] getObjectAsBytes(String bucket, String key) {
InputStream inputStream = this.getObjectAsInputStream(bucket, key);
try {
return IoUtils.toByteArray(inputStream);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public InputStream getObjectAsInputStream(String bucket, String key) {
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucket).object(key).build();
try {
return minioClient.getObject(getObjectArgs);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteObject(String bucket, String key) {
RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(key).build();
try {
minioClient.removeObject(removeObjectArgs);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteObject(String bucket, Set keySet) {
try {
List objects = keySet.stream().map(item -> new DeleteObject(item))
.collect(Collectors.toList());
RemoveObjectsArgs removeObjectsArgs = RemoveObjectsArgs.builder()
.bucket(bucket)
.objects(objects)
.build();
minioClient.removeObjects(removeObjectsArgs);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void copyBucketObject(String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) {
CopyObjectArgs copyObjectArgs = CopyObjectArgs.builder().source(CopySource.builder()
.bucket(sourceBucket).object(destinationBucket).build())
.bucket(destinationBucket).object(destinationKey).build();
try {
minioClient.copyObject(copyObjectArgs);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void moveBucketObject(String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) {
}
@Override
public void rename(String bucket, String sourceKey, String destinationKey) {
}
@Override
public List listBucketObjects(String bucket) {
ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder()
.bucket(bucket)
.recursive(true)
.build();
Iterable> results = minioClient.listObjects(listObjectsArgs);
List objects = new ArrayList<>();
for (Result- result : results) {
try {
Item item = result.get();
String objectName = item.objectName();
objects.add(objectName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return objects;
}
@Override
public String previewUrl(String bucket, String key) {
return this.previewUrl(bucket, key, 10);
}
@Override
public String previewUrl(String bucket, String key, long minutes) {
GetPresignedObjectUrlArgs presignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder()
.bucket(bucket)
.object(key)
.expiry((int) minutes, TimeUnit.MINUTES)
.method(Method.GET)
.build();
try {
String presignedObjectUrl = minioClient.getPresignedObjectUrl(presignedObjectUrlArgs);
return presignedObjectUrl;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}