11package com .comcast .dynocon .s3 ;
22
3- import com .amazonaws .services .s3 .AmazonS3 ;
4- import com .amazonaws .services .s3 .AmazonS3ClientBuilder ;
5- import com .amazonaws .services .s3 .model .*;
6- import com .amazonaws .util .IOUtils ;
73import com .comcast .dynocon .ConfigFactory ;
84import com .comcast .dynocon .ConfigUtil ;
95import com .comcast .dynocon .PropertiesSource ;
106import org .slf4j .Logger ;
117import org .slf4j .LoggerFactory ;
12-
8+ import software .amazon .awssdk .services .s3 .S3Client ;
9+ import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
10+ import software .amazon .awssdk .services .s3 .model .ListObjectsV2Request ;
11+ import software .amazon .awssdk .services .s3 .model .ListObjectsV2Response ;
12+ import software .amazon .awssdk .services .s3 .model .S3Object ;
13+
14+ import java .io .BufferedReader ;
15+ import java .io .InputStream ;
16+ import java .io .InputStreamReader ;
17+ import java .nio .charset .StandardCharsets ;
1318import java .util .HashMap ;
1419import java .util .Map ;
1520import java .util .Optional ;
1621import java .util .concurrent .Executors ;
1722import java .util .concurrent .ScheduledExecutorService ;
1823import java .util .concurrent .TimeUnit ;
24+ import java .util .stream .Collectors ;
1925
2026public class S3PropertiesSource implements PropertiesSource {
2127
@@ -24,7 +30,7 @@ public class S3PropertiesSource implements PropertiesSource {
2430
2531 private static final Logger LOGGER = LoggerFactory .getLogger (S3PropertiesSource .class );
2632
27- private static final AmazonS3 CLIENT = AmazonS3ClientBuilder . standard (). build ();
33+ private static final S3Client CLIENT = S3Client . create ();
2834 private static final String DEFAULT_BUCKET_NAME = "config" ;
2935 private static final int DEFAULT_POLLING_DELAY_SEC = 30 ;
3036
@@ -81,22 +87,29 @@ public S3PropertiesSource(String s3BucketName) {
8187 protected Map <String , String > getProperties () {
8288 Map <String , String > result = new HashMap <>();
8389 try {
84- ListObjectsV2Request req = new ListObjectsV2Request ().withBucketName (bucketName ).withMaxKeys (100 );
85- ListObjectsV2Result s3Result ;
90+ String continuationToken = null ;
8691 do {
87- s3Result = CLIENT .listObjectsV2 (req );
88- for (S3ObjectSummary objectSummary : s3Result .getObjectSummaries ()) {
89- S3Object fullObject = CLIENT .getObject (new GetObjectRequest (bucketName , objectSummary .getKey ()));
90- String key = objectSummary .getKey ();
91- if (key .endsWith (".json" )) {
92- key = key .substring (0 , key .length () - 5 );
92+ ListObjectsV2Request .Builder reqBuilder = ListObjectsV2Request .builder ().bucket (bucketName ).maxKeys (100 );
93+ if (continuationToken != null ) {
94+ reqBuilder .continuationToken (continuationToken );
95+ }
96+ ListObjectsV2Request req = reqBuilder .build ();
97+ ListObjectsV2Response s3Result = CLIENT .listObjectsV2 (req );
98+ for (S3Object objectSummary : s3Result .contents ()) {
99+ GetObjectRequest getObjectRequest = GetObjectRequest .builder ().bucket (bucketName ).key (objectSummary .key ()).build ();
100+ try (InputStream is = CLIENT .getObject (getObjectRequest );
101+ BufferedReader reader = new BufferedReader (new InputStreamReader (is , StandardCharsets .UTF_8 ))) {
102+ String key = objectSummary .key ();
103+ if (key .endsWith (".json" )) {
104+ key = key .substring (0 , key .length () - 5 );
105+ }
106+ String value = reader .lines ().collect (Collectors .joining ("\n " ));
107+ LOGGER .trace ("S3 item: key=`{}` value=`{}`" , key , value );
108+ result .put (key , value );
93109 }
94- String value = IOUtils .toString (fullObject .getObjectContent ());
95- LOGGER .trace ("S3 item: key=`{}` value=`{}`" , key , value );
96- result .put (key , value );
97110 }
98- req . setContinuationToken ( s3Result .getNextContinuationToken () );
99- } while (s3Result . isTruncated ());
111+ continuationToken = s3Result .nextContinuationToken ( );
112+ } while (continuationToken != null && ! continuationToken . isEmpty ());
100113 lastKnownGood = result ;
101114 } catch (Throwable e ) {
102115 result = lastKnownGood ;
0 commit comments