revert to old S3 api
This commit is contained in:
parent
a66a4645a5
commit
e896bae2d6
3 changed files with 42 additions and 51 deletions
10
pom.xml
10
pom.xml
|
|
@ -18,9 +18,9 @@
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>software.amazon.awssdk</groupId>
|
<groupId>com.amazonaws</groupId>
|
||||||
<artifactId>bom</artifactId>
|
<artifactId>aws-java-sdk-bom</artifactId>
|
||||||
<version>2.28.7</version>
|
<version>1.12.759</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
@ -59,8 +59,8 @@
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>software.amazon.awssdk</groupId>
|
<groupId>com.amazonaws</groupId>
|
||||||
<artifactId>s3</artifactId>
|
<artifactId>aws-java-sdk-s3</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,16 @@
|
||||||
package ru.micord.ervu.av.s3;
|
package ru.micord.ervu.av.s3;
|
||||||
|
|
||||||
import java.net.URI;
|
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.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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
|
* @author r.latypov
|
||||||
|
|
@ -17,8 +19,6 @@ import software.amazon.awssdk.services.s3.S3Client;
|
||||||
public class S3Config {
|
public class S3Config {
|
||||||
@Value("${s3.out.endpoint}")
|
@Value("${s3.out.endpoint}")
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
@Value("${s3.region:auto}")
|
|
||||||
private String regionStr;
|
|
||||||
@Value("${s3.out.access_key}")
|
@Value("${s3.out.access_key}")
|
||||||
private String accessKey;
|
private String accessKey;
|
||||||
@Value("${s3.out.secret_key}")
|
@Value("${s3.out.secret_key}")
|
||||||
|
|
@ -34,18 +34,19 @@ public class S3Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean("outClient")
|
@Bean("outClient")
|
||||||
public S3Client getS3OutClient() {
|
public AmazonS3 getS3OutClient() {
|
||||||
Region region = Region.of(regionStr);
|
return getS3Client(endpoint, accessKey, secretKey, pathStyleAccessEnabled);
|
||||||
AwsBasicCredentials credentials = AwsBasicCredentials.builder()
|
}
|
||||||
.accessKeyId(accessKey)
|
|
||||||
.secretAccessKey(secretKey)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return S3Client.builder()
|
private static AmazonS3 getS3Client(String endpoint, String accessKey,
|
||||||
.region(region)
|
String secretKey, boolean pathStyleAccessEnabled) {
|
||||||
.credentialsProvider(StaticCredentialsProvider.create(credentials))
|
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
|
||||||
.endpointOverride(URI.create(endpoint))
|
String region = Region.getRegion(Regions.DEFAULT_REGION).toString();
|
||||||
.forcePathStyle(pathStyleAccessEnabled)
|
|
||||||
|
return AmazonS3ClientBuilder.standard()
|
||||||
|
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
|
||||||
|
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
||||||
|
.withPathStyleAccessEnabled(pathStyleAccessEnabled)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,53 +2,43 @@ package ru.micord.ervu.av.s3;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
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 bucketName;
|
private final String outBucketName;
|
||||||
private final S3Client s3Client;
|
private final AmazonS3 outClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public S3Service(String bucketName, S3Client s3Client) {
|
public S3Service(String outBucketName, AmazonS3 outClient) {
|
||||||
this.bucketName = bucketName;
|
this.outBucketName = outBucketName;
|
||||||
this.s3Client = s3Client;
|
this.outClient = outClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
private void init() {
|
private void init() {
|
||||||
HeadBucketRequest headBucketRequest = HeadBucketRequest.builder()
|
if (!outClient.doesBucketExistV2(outBucketName)) {
|
||||||
.bucket(bucketName)
|
outClient.createBucket(outBucketName);
|
||||||
.build();
|
|
||||||
|
|
||||||
try {
|
|
||||||
s3Client.headBucket(headBucketRequest);
|
|
||||||
}
|
|
||||||
catch (NoSuchBucketException e) {
|
|
||||||
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
|
|
||||||
.bucket(bucketName)
|
|
||||||
.build();
|
|
||||||
s3Client.createBucket(bucketRequest);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void putFile(Path filePath, String key) throws FileUploadException {
|
public void putFile(Path filePath, String key) throws FileUploadException {
|
||||||
PutObjectRequest objectRequest = PutObjectRequest.builder()
|
try {
|
||||||
.bucket(bucketName)
|
outClient.putObject(outBucketName, generateResourceName(outBucketName, key),
|
||||||
.key(generateResourceName(bucketName, key))
|
filePath.toFile());
|
||||||
.build();
|
}
|
||||||
s3Client.putObject(objectRequest, filePath);
|
catch (AmazonServiceException e) {
|
||||||
|
// todo message
|
||||||
|
throw new FileUploadException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String generateResourceName(String bucketName, String key) {
|
private static String generateResourceName(String bucketName, String key) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue