All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.admin4j.oss.AmazonS3Factory Maven / Gradle / Ivy

Go to download

封装基于Amazon S3的OSS对象存储服务。在SpringBoot 中通过封装,简单地方式将文件存储到 MinIO、阿里云OSS、华为云OBS、七牛云Kodo、腾讯云COS等支持 S3 协议的平台

There is a newer version: 0.10.0
Show newest version
package com.admin4j.oss;

import com.amazonaws.ClientConfiguration;
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.AmazonS3Client;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * AmazonS3 工厂类
 *
 * @author andanyang
 * @since 2023/4/16 20:19
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AmazonS3Factory {

    /**
     * 根据配置文件初始化 AmazonS3
     *
     * @param ossProperties
     * @return AmazonS3
     */
    public static AmazonS3 create(OssProperties ossProperties) {

        // 客户端配置,主要是配置信息
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setMaxConnections(ossProperties.getMaxConnections());
        // url以及region配置
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
                ossProperties.getEndpoint(), ossProperties.getRegion());
        // 凭证配置
        AWSCredentials awsCredentials = new BasicAWSCredentials(ossProperties.getAccessKey(),
                ossProperties.getSecretKey());
        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
        // build amazonS3Client客户端
        return AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)
                .withClientConfiguration(clientConfiguration).withCredentials(awsCredentialsProvider)
                .disableChunkedEncoding().withPathStyleAccessEnabled(ossProperties.getPathStyleAccess()).build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy