Skip to content

Commit

Permalink
Use listObjects to check if bucket exists
Browse files Browse the repository at this point in the history
  • Loading branch information
manojlds committed Sep 7, 2017
1 parent 95f2f2f commit 095d606
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,8 @@ public void getPrefix(String prefix, String to) {

public boolean bucketExists() {
try {
List<Bucket> buckets = client.listBuckets();
return Lists.exists(buckets, new Functions.Predicate<Bucket>() {
@Override
public Boolean execute(Bucket input) {
return input.getName().equals(bucket);
}
});
client.listObjects(new ListObjectsRequest(bucket, null, null, null, 0));
return true;
} catch (Exception ex) {
return false;
}
Expand Down Expand Up @@ -215,7 +210,6 @@ public static AmazonS3 getS3client(GoEnvironment env) {
amazonS3ClientBuilder.withCredentials(new AWSStaticCredentialsProvider(basicCreds));
}


return amazonS3ClientBuilder.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.indix.gocd.utils.store;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.PutObjectRequest;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -54,4 +56,18 @@ public void shouldUseGlacierStorageClass() {
assertThat(putRequest.getStorageClass(), is("GLACIER"));
}

@Test
public void shouldSuccessfullyCheckIfBucketExists() {
doReturn(new ObjectListing()).when(mockClient).listObjects(any(ListObjectsRequest.class));
S3ArtifactStore store = new S3ArtifactStore(mockClient, "foo-bar");
assertThat(store.bucketExists(), is(true));
}

@Test
public void shouldHandleBucketDoesNotExists() {
doThrow(new RuntimeException("Bucket does not exist")).when(mockClient).listObjects(any(ListObjectsRequest.class));
S3ArtifactStore store = new S3ArtifactStore(mockClient, "foo-bar");
assertThat(store.bucketExists(), is(false));
}

}

0 comments on commit 095d606

Please sign in to comment.