content: 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();
      }
  }
  