SUPPORT-8566: add new api for S3
This commit is contained in:
parent
0d8b8b2b3d
commit
10abd45fbb
4 changed files with 84 additions and 80 deletions
10
pom.xml
10
pom.xml
|
|
@ -18,9 +18,9 @@
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.amazonaws</groupId>
|
<groupId>software.amazon.awssdk</groupId>
|
||||||
<artifactId>aws-java-sdk-bom</artifactId>
|
<artifactId>bom</artifactId>
|
||||||
<version>1.12.759</version>
|
<version>2.28.7</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
@ -59,8 +59,8 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.amazonaws</groupId>
|
<groupId>software.amazon.awssdk</groupId>
|
||||||
<artifactId>aws-java-sdk-s3</artifactId>
|
<artifactId>s3</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
||||||
51
src/main/java/ru/micord/ervu/av/s3/S3Config.java
Normal file
51
src/main/java/ru/micord/ervu/av/s3/S3Config.java
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
package ru.micord.ervu.av.s3;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||||
|
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||||
|
import software.amazon.awssdk.regions.Region;
|
||||||
|
import software.amazon.awssdk.services.s3.S3Client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author r.latypov
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class S3Config {
|
||||||
|
@Value("${s3.out.endpoint}")
|
||||||
|
private String endpoint;
|
||||||
|
@Value("${s3.region:auto}")
|
||||||
|
private String regionStr;
|
||||||
|
@Value("${s3.out.access_key}")
|
||||||
|
private String accessKey;
|
||||||
|
@Value("${s3.out.secret_key}")
|
||||||
|
private String secretKey;
|
||||||
|
@Value("${s3.out.bucket_name}")
|
||||||
|
private String bucketName;
|
||||||
|
@Value("${s3.out.path.style.enabled:true}")
|
||||||
|
private boolean pathStyleAccessEnabled;
|
||||||
|
|
||||||
|
@Bean("outBucketName")
|
||||||
|
public String getBucket() {
|
||||||
|
return bucketName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("outClient")
|
||||||
|
public S3Client getS3OutClient() {
|
||||||
|
Region region = Region.of(regionStr);
|
||||||
|
AwsBasicCredentials credentials = AwsBasicCredentials.builder()
|
||||||
|
.accessKeyId(accessKey)
|
||||||
|
.secretAccessKey(secretKey)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return S3Client.builder()
|
||||||
|
.region(region)
|
||||||
|
.credentialsProvider(StaticCredentialsProvider.create(credentials))
|
||||||
|
.endpointOverride(URI.create(endpoint))
|
||||||
|
.forcePathStyle(pathStyleAccessEnabled)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,52 +0,0 @@
|
||||||
package ru.micord.ervu.av.s3;
|
|
||||||
|
|
||||||
import com.amazonaws.auth.AWSCredentials;
|
|
||||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
|
||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
|
||||||
import com.amazonaws.client.builder.AwsClientBuilder;
|
|
||||||
import com.amazonaws.regions.Region;
|
|
||||||
import com.amazonaws.regions.Regions;
|
|
||||||
import com.amazonaws.services.s3.AmazonS3;
|
|
||||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author r.latypov
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class S3Connection {
|
|
||||||
@Value("${s3.out.endpoint}")
|
|
||||||
private String endpointOut;
|
|
||||||
@Value("${s3.out.access_key}")
|
|
||||||
private String accessKeyOut;
|
|
||||||
@Value("${s3.out.secret_key}")
|
|
||||||
private String secretKeyOut;
|
|
||||||
@Value("${s3.out.bucket_name}")
|
|
||||||
private String bucketNameOut;
|
|
||||||
@Value("${s3.out.path.style.access.enabled:true}")
|
|
||||||
private boolean pathStyleAccessEnabled;
|
|
||||||
|
|
||||||
@Bean("outBucketName")
|
|
||||||
public String getBucketNameOut() {
|
|
||||||
return bucketNameOut;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean("outClient")
|
|
||||||
public AmazonS3 getS3OutClient() {
|
|
||||||
return getS3Client(endpointOut, accessKeyOut, secretKeyOut, pathStyleAccessEnabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static AmazonS3 getS3Client(String endpoint, String accessKey,
|
|
||||||
String secretKey, boolean pathStyleAccessEnabled) {
|
|
||||||
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
|
|
||||||
String region = Region.getRegion(Regions.DEFAULT_REGION).toString();
|
|
||||||
|
|
||||||
return AmazonS3ClientBuilder.standard()
|
|
||||||
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
|
|
||||||
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
|
||||||
.withPathStyleAccessEnabled(pathStyleAccessEnabled)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,48 +1,53 @@
|
||||||
package ru.micord.ervu.av.s3;
|
package ru.micord.ervu.av.s3;
|
||||||
|
|
||||||
import java.io.File;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
import com.amazonaws.AmazonServiceException;
|
|
||||||
import com.amazonaws.services.s3.AmazonS3;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.micord.ervu.av.exception.FileUploadException;
|
import ru.micord.ervu.av.exception.FileUploadException;
|
||||||
|
import software.amazon.awssdk.services.s3.S3Client;
|
||||||
|
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
|
||||||
|
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
|
||||||
|
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
|
||||||
|
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author r.latypov
|
* @author r.latypov
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class S3Service {
|
public class S3Service {
|
||||||
private final String outBucketName;
|
private final String bucketName;
|
||||||
private final AmazonS3 outClient;
|
private final S3Client s3Client;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public S3Service(String outBucketName, AmazonS3 outClient) {
|
public S3Service(String bucketName, S3Client s3Client) {
|
||||||
this.outBucketName = outBucketName;
|
this.bucketName = bucketName;
|
||||||
this.outClient = outClient;
|
this.s3Client = s3Client;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
private void init() {
|
private void init() {
|
||||||
if (!outClient.doesBucketExistV2(outBucketName)) {
|
HeadBucketRequest headBucketRequest = HeadBucketRequest.builder()
|
||||||
outClient.createBucket(outBucketName);
|
.bucket(bucketName)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
s3Client.headBucket(headBucketRequest);
|
||||||
|
}
|
||||||
|
catch (NoSuchBucketException e) {
|
||||||
|
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
|
||||||
|
.bucket(bucketName)
|
||||||
|
.build();
|
||||||
|
s3Client.createBucket(bucketRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void putFile(String filePath, String key) throws FileUploadException {
|
public void putFile(String filePath, String key) throws FileUploadException {
|
||||||
try {
|
PutObjectRequest objectRequest = PutObjectRequest.builder()
|
||||||
outClient.putObject(outBucketName, generateResourceName(outBucketName, key),
|
.bucket(bucketName)
|
||||||
new File(filePath)
|
.key(key)
|
||||||
);
|
.build();
|
||||||
}
|
s3Client.putObject(objectRequest, Paths.get(filePath));
|
||||||
catch (AmazonServiceException e) {
|
|
||||||
// todo message
|
|
||||||
throw new FileUploadException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String generateResourceName(String bucketName, String key) {
|
|
||||||
return String.join("/", bucketName, key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue