Skip to content

Instantly share code, notes, and snippets.

@vaibhavdes
Created November 14, 2020 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavdes/702b0a7024719b9670d7bf77057d0426 to your computer and use it in GitHub Desktop.
Save vaibhavdes/702b0a7024719b9670d7bf77057d0426 to your computer and use it in GitHub Desktop.
AWS S3 Client - Java / Spring
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
@Configuration
public class S3Config {
private static final Logger LOGGER = LoggerFactory.getLogger(S3Config.class);
@Value("${cloud.aws.region.static}")
String region;
@Bean
public AmazonS3 s3client() {
LOGGER.info("--- S3 Configuration Completed---");
// AWSCredentials credentials = new BasicAWSCredentials(accessKey,accessSecret);
AmazonS3 s3Client = AmazonS3ClientBuilder
.standard()
//.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withCredentials(new DefaultAWSCredentialsProviderChain())
.withRegion(Regions.fromName(region))
.build();
return s3Client ;
}
@Bean
public AmazonS3Client amazonS3Client() {
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.withRegion(Regions.fromName(region))
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment