1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Springboot 整合常用对象存储工具(asw s3 亚马逊云存储 minio 阿里oss)

Springboot 整合常用对象存储工具(asw s3 亚马逊云存储 minio 阿里oss)

时间:2023-07-25 12:10:15

相关推荐

Springboot 整合常用对象存储工具(asw s3 亚马逊云存储 minio 阿里oss)

1、引入依赖(gradle)

// asw s3 亚马逊云存储implementation 'com.amazonaws:aws-java-sdk-s3:1.11.830'// minio clientapi "io.minio:minio:8.2.1"// oss clientimplementation 'com.aliyun.oss:aliyun-sdk-oss:3.10.2'

2、MINIO

import io.minio.BucketExistsArgs;import io.minio.MakeBucketArgs;import io.minio.MinioClient;import io.minio.RemoveBucketArgs;import io.minio.messages.Bucket;import org.assertj.core.util.Lists;import java.util.List;/*** @author yue.wu* @description TODO* @date /10/27 10:00*/public class ObjectStorageMinIOUtil {public ObjectStorageMinIOUtil() {}/*** 获取Minio客户端** @param endpoint String* @param accessKeyIdString* @param accessKeySecret String* @return MinioClient Minio客户端* @author yue.wu* @date /10/27 10:02*/public static MinioClient getMinioClient(String endpoint, String accessKeyId, String accessKeySecret) {return MinioClient.builder().endpoint(endpoint).credentials(accessKeyId, accessKeySecret).build();}/*** 查询Bucket是否存在** @param minioClient minio客户端* @param bucketName bucket名称* @return Boolean Bucket是否存在* @author yue.wu* @date /10/27 10:04*/public Boolean existBucket(MinioClient minioClient, String bucketName) {try {return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());} catch (Exception e) {e.printStackTrace();}return false;}/*** 创建Bucket** @param minioClient minio客户端* @param bucketName bucket名称* @return Boolean 创建成功/失败* @author yue.wu* @date /10/27 10:07*/public Boolean makeBucket(MinioClient minioClient, String bucketName) {try {minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());} catch (Exception e) {e.printStackTrace();return false;}return true;}/*** 删除Bucket** @param minioClient minio客户端* @param bucketName bucket名称* @return Boolean 删除成功/失败* @author yue.wu* @date /10/27 10:07*/public Boolean removeBucket(MinioClient minioClient, String bucketName) {try {minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());} catch (Exception e) {e.printStackTrace();return false;}return true;}/*** 获取Bucket列表** @param minioClient minio客户端* @return List Bucket列表* @author yue.wu* @date /10/27 10:11*/public static List<Bucket> getBucketList(MinioClient minioClient) {try {return minioClient.listBuckets();} catch (Exception e) {e.printStackTrace();}return Lists.newArrayList();}}

3、亚马逊

import com.amazonaws.ClientConfiguration;import com.amazonaws.Protocol;import com.amazonaws.auth.AWSCredentials;import com.amazonaws.auth.AWSCredentialsProvider;import com.amazonaws.auth.AWSStaticCredentialsProvider;import com.amazonaws.auth.BasicAWSCredentials;import com.amazonaws.client.builder.AwsClientBuilder;import com.amazonaws.services.s3.AmazonS3;import com.amazonaws.services.s3.AmazonS3ClientBuilder;import com.amazonaws.services.s3.model.Bucket;import java.util.List;/*** @author yue.wu* @description TODO* @date /10/27 10:13*/public class ObjectStorageAWSUtil {private static final String accessKey = "";private static final String secretKey = "";private static final String endpoint = "";private static final Protocol http = Protocol.HTTP;/*** TODO** @param endpoint String* @param accessKeyIdString* @param accessKeySecret String* @param protocol 协议类型* @return AmazonS3 S3客户端* @author yue.wu* @date /10/27 11:15*/public static AmazonS3 getConnect(String endpoint, String accessKeyId, String accessKeySecret, Protocol protocol) {ValidationUtils.assertAllNotNull("param cannot be null", endpoint, accessKeyId, accessKeySecret, protocol);AmazonS3ClientBuilder client = AmazonS3ClientBuilder.standard();ClientConfiguration config = new ClientConfiguration();// 默认HTTPconfig.setProtocol(protocol);config.setConnectionTimeout(15 * 1000);// v2config.setSignerOverride("S3SignerType");// v4// config.setSignerOverride("AWSS3V4SignerType");AWSCredentials acre = new BasicAWSCredentials(accessKeyId, accessKeySecret);AWSCredentialsProvider provider = new AWSStaticCredentialsProvider(acre);AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(endpoint, null);client.setClientConfiguration(config);// client.setChunkedEncodingDisabled(true);client.setCredentials(provider);client.setEndpointConfiguration(endpointConfig);return client.build();}/*** 获取bucket列表** @param client s3客户端* @return List<Bucket> bucket列表* @author yue.wu* @date /10/27 11:21*/public static List<Bucket> getBucketList(AmazonS3 client) {return ValidationUtils.assertNotNull(client, "client").listBuckets();}/*** 检查bucket是否存在** @param clients3客户端* @param bucketName bucket名称* @return Boolean 是否存在* @author yue.wu* @date /10/27 11:23*/public static Boolean existBucket(AmazonS3 client, String bucketName) {ValidationUtils.assertAllNotNull("param cannot be null", client, bucketName);return client.doesBucketExistV2(bucketName);}/*** 创建bucket** @param clients3客户端* @param bucketName bucket名称* @return Bucket Bucket* @author yue.wu* @date /10/27 11:28*/public static Bucket createBucket(AmazonS3 client, String bucketName) {ValidationUtils.assertAllNotNull("param cannot be null", client, bucketName);return client.createBucket(bucketName);}}

4、阿里OSS

import com.aliyun.oss.ClientBuilderConfiguration;import com.aliyun.oss.OSS;import com.aliyun.oss.OSSClientBuilder;import com.aliyun.oss.model.Bucket;import java.util.List;/*** @author yue.wu* @description TODO* @date /10/27 11:14*/public class ObjectStorageOSSUtil {public ObjectStorageOSSUtil() {}/*** 获取OSS连接** @param endpoint String* @param accessKeyIdString* @param accessKeySecret String* @param configuration ClientBuilderConfiguration* @return OSS* @author yue.wu* @date /8/24 9:25*/public static OSS getOssClient(String endpoint, String accessKeyId, String accessKeySecret,ClientBuilderConfiguration configuration) {return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, configuration);}/*** 获取OSS连接** @param endpoint String* @param accessKeyIdString* @param accessKeySecret String* @return OSS* @author yue.wu* @date /8/24 9:25*/public static OSS getOssClient(String endpoint, String accessKeyId, String accessKeySecret) {return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);}/*** 获取BUCKET列表** @param client OSS* @return List<Bucket>* @author yue.wu* @date /8/24 14:11*/public static List<Bucket> getBucketList(OSS client) {return client.listBuckets();}/*** 创建BUCKET** @param clientOSS* @param bucketName bucket名称* @return Bucket Bucket* @author yue.wu* @date /10/27 11:27*/public static Bucket createBucket(OSS client, String bucketName) {return client.createBucket(bucketName);}}

5、校验工具(偷亚马逊的)

import java.util.Collection;/*** @author yue.wu* @description TODO* @date /10/25 15:59*/public class ValidationUtils {public ValidationUtils() {}/*** Asserts that the given object is non-null and returns it.** @param object Object to assert on* @param fieldName Field name to display in exception message if null* @return Object if non null* @throws IllegalArgumentException If object was null*/public static <T> T assertNotNull(T object, String fieldName) throws IllegalArgumentException {if (object == null) {throw new IllegalArgumentException(String.format("%s cannot be null", fieldName));}return object;}/*** Asserts that all of the objects are null.** @throws IllegalArgumentException if any object provided was NOT null.*/public static void assertAllAreNull(String messageIfNull, Object... objects) throws IllegalArgumentException {for (Object object : objects) {if (object != null) {throw new IllegalArgumentException(messageIfNull);}}}/*** Asserts that all of the objects are null.** @throws IllegalArgumentException if any object provided was NOT null.*/public static void assertAllNotNull(String messageIfNull, Object... objects) throws IllegalArgumentException {for (Object object : objects) {if (object == null) {throw new IllegalArgumentException(messageIfNull);}}}/*** Asserts that the given number is positive (non-negative and non-zero).** @param num Number to validate* @param fieldName Field name to display in exception message if not positive.* @return Number if positive.*/public static int assertIsPositive(int num, String fieldName) {if (num <= 0) {throw new IllegalArgumentException(String.format("%s must be positive", fieldName));}return num;}public static <T extends Collection<?>> T assertNotEmpty(T collection, String fieldName) throws IllegalArgumentException {assertNotNull(collection, fieldName);if (collection.isEmpty()) {throw new IllegalArgumentException(String.format("%s cannot be empty", fieldName));}return collection;}public static <T> T[] assertNotEmpty(T[] array, String fieldName) throws IllegalArgumentException {assertNotNull(array, fieldName);if (array.length == 0) {throw new IllegalArgumentException(String.format("%s cannot be empty", fieldName));}return array;}public static String assertStringNotEmpty(String string, String fieldName) throws IllegalArgumentException {assertNotNull(string, fieldName);if (string.isEmpty()) {throw new IllegalArgumentException(String.format("%s cannot be empty", fieldName));}return string;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。