package com.sf.file.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class AmazonConfig {
	@Value("${file.ceph.endpoint}")
    private String FILE_CEPH_POINT;

    @Value("${file.ceph.access.key}")
    private String FILE_CEPH_ACCESS_KEY;

    @Value("${file.ceph.secret.key}")
    private String FILE_CEPH_SECRET_KEY;

    @Value("${file.ceph.bucket.name}")
    private String FILE_CEPH_BUCKET_NAME;

    
    @Bean(name = "amazonS3")
    public AmazonS3 s3() {
		ClientConfiguration config = new ClientConfiguration();
        config.setMaxConnections(200);
        AwsClientBuilder.EndpointConfiguration endpointConfig =
                new AwsClientBuilder.EndpointConfiguration(FILE_CEPH_POINT, Regions.CN_NORTH_1.getName());

        AWSCredentials awsCredentials = new BasicAWSCredentials(FILE_CEPH_ACCESS_KEY, FILE_CEPH_SECRET_KEY);
        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);

        AmazonS3 s3 = AmazonS3Client.builder()
                .withEndpointConfiguration(endpointConfig)
                .withClientConfiguration(config)
                .withCredentials(awsCredentialsProvider)
                .disableChunkedEncoding()
                .withPathStyleAccessEnabled(true)
                .build();
        log.debug("完成初始化AWS访问组件");
        return s3;
	}
}
