Create an AWS Account: If you don't already have one, create an AWS account on AWS Sign-Up.
Create an admin user using IAM. Generate Accesskey in IAM, the use case is Local code. And note down your Access key id and Secret access key.
Go to the S3 service in the AWS Console and create a new bucket to store your files.
Upload a picture to your bucket just for test.
Note down the Bucket Name and Region(e.g., ap-southeast-2).
Open your terminal
.aws :mkdir -p ~/.awsvim ~/.aws/credentialsAccess key id and Secret access key :[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEYchmod 600 ~/.aws/credentialsAdd the AWS SDK dependencies to your Spring Boot project. If you're using Maven, add the following dependencies to your pom.xml file.
<dependencies>
<!-- AWS SDK for S3 -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.31.47</version>
</dependency>
<!-- Spring Boot Starter Web for RESTful APIs -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>set region and bucket name in your application.yml
aws:
region: us-east-1
s3:
bucket: your-bucket-name package com.example.s3demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "aws")
public class AwsProperties {
private String region;
private S3Properties s3;
// Getters and setters
public String getRegion() { return region; }
public void setRegion(String region) { this.region = region; }
public S3Properties getS3() { return s3; }
public void setS3(S3Properties s3) { this.s3 = s3; }
public static class S3Properties {
private String bucket;
public String getBucket() { return bucket; }
public void setBucket(String bucket) { this.bucket = bucket; }
}
}Configuration
public class S3Config {
private final AwsProperties awsProperties;
public S3Config(AwsProperties awsProperties) {
this.awsProperties = awsProperties;
}
@Bean
public S3Client s3Client() {
try {
return S3Client.builder()
.region(Region.of(awsProperties.getRegion()))
.credentialsProvider(DefaultCredentialsProvider.create())
.build();
} catch (Exception e) {
throw e;
}
}
@Bean
public S3Presigner s3Presigner() {
try {
return S3Presigner.builder()
.region(Region.of(awsProperties.getRegion()))
.credentialsProvider(DefaultCredentialsProvider.create())
.build();
} catch (Exception e) {
throw e;
}
}
}@Service
public class S3Service {
private final S3Client s3Client;
private final String bucketName;
public S3Service(S3Client s3Client, S3Presigner s3Presigner, AwsProperties awsProperties) {
this.s3Client = s3Client;
this.s3Presigner = s3Presigner;
this.bucketName = awsProperties.getS3().getBucket();
}
public List<String> listFiles() {
ListObjectsV2Request listObjectsRequest = ListObjectsV2Request.builder()
.bucket(bucketName)
.build();
return s3Client.listObjectsV2(listObjectsRequest)
.contents()
.stream()
.map(S3Object::key)
.collect(Collectors.toList());
}
}package com.example.s3demo.controller;
import com.example.s3demo.service.S3Service;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/files")
public class S3Controller {
private final S3Service s3Service;
public S3Controller(S3Service s3Service) {
this.s3Service = s3Service;
}
@GetMapping
public ResponseEntity<List<String>> listFiles() {
return ResponseEntity.ok(s3Service.listFiles());
}
}In your main application class:
package com.example.s3demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties
public class S3DemoApplication {
public static void main(String[] args) {
SpringApplication.run(S3DemoApplication.class, args);
}
}Start your springboot application, open the idea integrated terminal and run:
curl http://localhost:8080/api/files
Comments (0)
Loading comments...