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>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>bom</artifactId>
|
||||
<version>2.28.7</version>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-bom</artifactId>
|
||||
<version>1.12.759</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
|
@ -59,8 +59,8 @@
|
|||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>s3</artifactId>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-s3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
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.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
|
||||
|
|
@ -17,8 +19,6 @@ import software.amazon.awssdk.services.s3.S3Client;
|
|||
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}")
|
||||
|
|
@ -34,18 +34,19 @@ public class S3Config {
|
|||
}
|
||||
|
||||
@Bean("outClient")
|
||||
public S3Client getS3OutClient() {
|
||||
Region region = Region.of(regionStr);
|
||||
AwsBasicCredentials credentials = AwsBasicCredentials.builder()
|
||||
.accessKeyId(accessKey)
|
||||
.secretAccessKey(secretKey)
|
||||
.build();
|
||||
public AmazonS3 getS3OutClient() {
|
||||
return getS3Client(endpoint, accessKey, secretKey, pathStyleAccessEnabled);
|
||||
}
|
||||
|
||||
return S3Client.builder()
|
||||
.region(region)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(credentials))
|
||||
.endpointOverride(URI.create(endpoint))
|
||||
.forcePathStyle(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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,53 +2,43 @@ package ru.micord.ervu.av.s3;
|
|||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.amazonaws.AmazonServiceException;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
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
|
||||
*/
|
||||
@Service
|
||||
public class S3Service {
|
||||
private final String bucketName;
|
||||
private final S3Client s3Client;
|
||||
private final String outBucketName;
|
||||
private final AmazonS3 outClient;
|
||||
|
||||
@Autowired
|
||||
public S3Service(String bucketName, S3Client s3Client) {
|
||||
this.bucketName = bucketName;
|
||||
this.s3Client = s3Client;
|
||||
public S3Service(String outBucketName, AmazonS3 outClient) {
|
||||
this.outBucketName = outBucketName;
|
||||
this.outClient = outClient;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
HeadBucketRequest headBucketRequest = HeadBucketRequest.builder()
|
||||
.bucket(bucketName)
|
||||
.build();
|
||||
|
||||
try {
|
||||
s3Client.headBucket(headBucketRequest);
|
||||
}
|
||||
catch (NoSuchBucketException e) {
|
||||
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
|
||||
.bucket(bucketName)
|
||||
.build();
|
||||
s3Client.createBucket(bucketRequest);
|
||||
if (!outClient.doesBucketExistV2(outBucketName)) {
|
||||
outClient.createBucket(outBucketName);
|
||||
}
|
||||
}
|
||||
|
||||
public void putFile(Path filePath, String key) throws FileUploadException {
|
||||
PutObjectRequest objectRequest = PutObjectRequest.builder()
|
||||
.bucket(bucketName)
|
||||
.key(generateResourceName(bucketName, key))
|
||||
.build();
|
||||
s3Client.putObject(objectRequest, filePath);
|
||||
try {
|
||||
outClient.putObject(outBucketName, generateResourceName(outBucketName, key),
|
||||
filePath.toFile());
|
||||
}
|
||||
catch (AmazonServiceException e) {
|
||||
// todo message
|
||||
throw new FileUploadException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String generateResourceName(String bucketName, String key) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue