1 Ocak 2023 Pazar

AWS S3 API S3AsyncClient Sınıfı - Kullanmayın

Giriş
Şu satırı dahil ederiz
import software.amazon.awssdk.services.s3.S3AsyncClient;
Maven
Şu satırı dahil ederiz
<!-- AWS SDK Java V2  -->
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.18.41</version>
</dependency>
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>netty-nio-client</artifactId>
    <version>2.18.41</version>
</dependency>
application.properties şöyle olsun
# AWS properties
aws:
  access-key: test
  secret-key: test
  region: eu-west-1
  s3-bucket-name: my-test-bucket
  multipart-min-part-size: 5242880 # 5MB
  endpoint: http://localhost:4566/
constructor
Şu satırı dahil ederiz
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3Configuration;
Şöyle yaparız
@RequiredArgsConstructor
@Configuration
public class AwsS3Config {
  private final AwsProperties s3ConfigProperties;

  @Bean
  public S3AsyncClient s3AsyncClient(AwsCredentialsProvider awsCredentialsProvider) {
    return S3AsyncClient.builder()
      .httpClient(sdkAsyncHttpClient())
      .region(Region.of(s3ConfigProperties.getRegion()))
      .credentialsProvider(awsCredentialsProvider)
      .endpointOverride(URI.create(s3ConfigProperties.getEndpoint()))
      .forcePathStyle(true)
      .serviceConfiguration(s3Configuration()).build();
  }

  private SdkAsyncHttpClient sdkAsyncHttpClient() {
    return NettyNioAsyncHttpClient.builder()
      .writeTimeout(Duration.ZERO)
      .maxConcurrency(64)
      .build();
  }

  private S3Configuration s3Configuration() {
    return S3Configuration.builder()
       .checksumValidationEnabled(false)
       .chunkedEncodingEnabled(true)
       .build();
  }
  @Bean
  AwsCredentialsProvider awsCredentialsProvider() {
    return () -> AwsBasicCredentials.create(s3ConfigProperties.getAccessKey(), 
      s3ConfigProperties.getSecretKey());
  }
}
deleteObject metodu
Şöyle yaparız
public Mono<Void> deleteObject(@NotNull String objectKey) {
  return Mono.just(DeleteObjectRequest.builder()
      .bucket(s3ConfigProperties.getS3BucketName())
      .key(objectKey)
      .build())
    .map(s3AsyncClient::deleteObject)
    .flatMap(Mono::fromFuture)
    .then();
  }
getObject metodu
Şöyle yaparız
public Mono<Void> deleteObject(@NotNull String objectKey) {
  return Mono.just(DeleteObjectRequest.builder()
      .bucket(s3ConfigProperties.getS3BucketName())
      .key(objectKey)
      .build())
    .map(s3AsyncClient::deleteObject)
    .flatMap(Mono::fromFuture)
    .then();
}




Hiç yorum yok:

Yorum Gönder