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

1、引入依赖(gradle)
    // asw s3 亚马逊云存储
    implementation 'com.amazonaws:aws-java-sdk-s3:1.11.830'
    // minio client
    api "io.minio:minio:8.2.1"
    // oss client
    implementation '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 2022/10/27 10:00
 */
public class ObjectStorageMinIOUtil {

    public ObjectStorageMinIOUtil() {
    }

    /**
     * 获取Minio客户端
     *
     * @param endpoint        String
     * @param accessKeyId     String
     * @param accessKeySecret String
     * @return MinioClient Minio客户端
     * @author yue.wu
     * @date 2022/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 2022/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 2022/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 2022/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 2022/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 2022/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 accessKeyId     String
     * @param accessKeySecret String
     * @param protocol        协议类型
     * @return AmazonS3 S3客户端
     * @author yue.wu
     * @date 2022/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();
        // 默认HTTP
        config.setProtocol(protocol);
        config.setConnectionTimeout(15 * 1000);
        // v2
        config.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 2022/10/27 11:21
     */
    public static List<Bucket> getBucketList(AmazonS3 client) {
        return ValidationUtils.assertNotNull(client, "client").listBuckets();
    }

    /**
     * 检查bucket是否存在
     *
     * @param client     s3客户端
     * @param bucketName bucket名称
     * @return Boolean 是否存在
     * @author yue.wu
     * @date 2022/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 client     s3客户端
     * @param bucketName bucket名称
     * @return Bucket  Bucket
     * @author yue.wu
     * @date 2022/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 2022/10/27 11:14
 */
public class ObjectStorageOSSUtil {

    public ObjectStorageOSSUtil() {
    }

    /**
     * 获取OSS连接
     *
     * @param endpoint        String
     * @param accessKeyId     String
     * @param accessKeySecret String
     * @param configuration   ClientBuilderConfiguration
     * @return OSS
     * @author yue.wu
     * @date 2022/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 accessKeyId     String
     * @param accessKeySecret String
     * @return OSS
     * @author yue.wu
     * @date 2022/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 2022/8/24 14:11
     */
    public static List<Bucket> getBucketList(OSS client) {
        return client.listBuckets();
    }

    /**
     * 创建BUCKET
     *
     * @param client     OSS
     * @param bucketName bucket名称
     * @return Bucket Bucket
     * @author yue.wu
     * @date 2022/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 2022/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;
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我不确定你指的是什么“asw”,但是在安装pytorch时可能会出现一个名为Amazon SageMaker Python SDK (ASW)的软件包,它是亚马逊云服务提供的用于构建、训练和部署机器学习模型的框架。如果你没有使用亚马逊云服务,则不需要安装ASW。 ### 回答2: 安装完PyTorch之后,出现的AWS指的是亚马逊网络服务(Amazon Web Services),它是一种提供云计算服务的平台。AWS提供了广泛的计算与存储资源,用于构建、部署和管理各种应用程序和服务。在PyTorch中,AWS可以用于训练和部署深度学习模型。 通过AWS,用户可以在云端创建虚拟服务器(EC2实例)来训练深度学习模型。用户可以选择适合自己需求的计算实例类型和规模,并根据需要灵活地扩展或缩减计算资源。 另外,AWS还提供了自动缩放的功能,可以根据实例的工作负载自动调整计算资源。这对于处理大规模训练数据集或运行复杂的模型非常有帮助。 同时,AWS还提供了存储服务,如Amazon S3Amazon EBS,用于安全地存储数据集和模型参数。此外,AWS还提供了用于模型部署的服务,如Amazon SageMaker,可以轻松地将训练好的模型部署到生产环境中使用。 总而言之,安装完PyTorch后,AWS作为一种强大的云计算平台,可以为深度学习任务提供高性能的计算和存储资源,帮助用户更方便地训练和部署模型。 ### 回答3: 安装完pytorch之后出现的asw是指Amazon SageMaker的官方软件包。Amazon SageMaker是亚马逊推出的一项机器学习服务,旨在使开发者和数据科学家能够轻松地构建、训练和部署机器学习模型。 aswAmazon SageMaker 官方软件包的简称,全名为Amazon SageMaker Python SDK。 asw提供了一系列的Python库和工具,用于简化机器学习模型的开发和训练过程。通过asw,您可以使用PyTorch等流行的深度学习框架在Amazon SageMaker平台上构建和训练模型。asw提供了许多便捷的功能,例如数据集处理、模型训练、模型评估等。 安装PyTorch后,asw通常会自动安装在Python环境中。但是,如果您的Python环境中没有安装asw,您可以使用pip命令手动安装asw,例如:pip install sagemaker。 安装完asw后,您可以在Python中使用asw库来使用Amazon SageMaker提供的各种功能和服务。通过asw,您可以通过简单的Python代码定义模型、训练模型、调整超参数等。此外,asw还提供了与Amazon SageMaker其他功能的集成,例如Amazon S3存储桶、Amazon Elastic Inference等。这些功能可以帮助您更好地管理和部署您的机器学习模型。 总之,asw是安装PyTorch之后出现的Amazon SageMaker官方软件包,提供了一系列用于简化机器学习模型开发和训练的Python库和工具

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值