Skip to content

Commit

Permalink
perf: skip s3 checks when creditials explicitly passed (#33)
Browse files Browse the repository at this point in the history
* perf: skip s3 checks when creditials explicitly passed
* style: AmazonS3UtilsTest
* test: AmazonS3UtilsTest uses explicit anonymous credentials
  • Loading branch information
bogovicj authored Jun 5, 2024
1 parent 52ad9c5 commit f2901bd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
28 changes: 15 additions & 13 deletions src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package org.janelia.saalfeldlab.n5.s3;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

import org.janelia.saalfeldlab.n5.N5Exception;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
Expand All @@ -12,15 +21,6 @@
import com.amazonaws.services.s3.AmazonS3URI;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.ListObjectsV2Request;
import org.janelia.saalfeldlab.n5.N5Exception;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

public class AmazonS3Utils {
public static final Pattern AWS_ENDPOINT_PATTERN = Pattern.compile("^(.+\\.)?(s3\\..*amazonaws\\.com)", Pattern.CASE_INSENSITIVE);
Expand Down Expand Up @@ -108,7 +108,7 @@ public static AWSCredentialsProvider getS3Credentials(final AWSCredentials s3Cre
if (!s3Anonymous)
return new DefaultAWSCredentialsProviderChain();
else
return new AWSStaticCredentialsProvider(new AnonymousAWSCredentials());
return null;
}
}

Expand Down Expand Up @@ -170,7 +170,9 @@ public static AmazonS3 createS3(
if (!isAmazon)
builder.withPathStyleAccessEnabled(true);

if (credentialsProvider != null)
if (credentialsProvider == null)
builder.withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()));
else
builder.withCredentials(credentialsProvider);

if (endpointConfiguration != null)
Expand All @@ -180,9 +182,9 @@ else if (region != null)
else
builder.withRegion("us-east-1");

AmazonS3 s3 = builder.build();
final AmazonS3 s3 = builder.build();
// try to listBucket if we are anonymous, if we cannot, don't use anonymous.
if (credentialsProvider != null && AmazonS3Utils.areAnonymous(credentialsProvider)) {
if (credentialsProvider == null) {

// I initially tried checking whether the bucket exists, but
// that, apparently, returns even when the client does not have access
Expand Down
29 changes: 17 additions & 12 deletions src/test/java/org/janelia/saalfeldlab/n5/s3/AmazonS3UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,43 @@
import java.util.HashMap;
import java.util.Map;

import com.google.common.collect.Maps;
import org.junit.Test;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.AnonymousAWSCredentials;

public class AmazonS3UtilsTest {

@Test
public void testUriParsing() throws URISyntaxException {

// dummy client
String[] prefixes = new String[]{
final String[] prefixes = new String[]{
"s3://",
"https://s3-eu-west-1.amazonaws.com/",
"http://localhost:8001/",
};

String[] buckets = new String[]{
final String[] buckets = new String[]{
"zarr-n5-demo",
"static.wk.org"};

String[] paths = new String[]{
final String[] paths = new String[]{
"",
"foo.zarr",
"data/sample"};

for (String prefix : prefixes)
for (String bucket : buckets)
for (String path : paths) {
String uriString = prefix + bucket + "/" + path;
URI uri = new URI(uriString);
for (final String prefix : prefixes)
for (final String bucket : buckets)
for (final String path : paths) {
final String uriString = prefix + bucket + "/" + path;
final URI uri = new URI(uriString);
assertEquals("bucket from uri", bucket, AmazonS3Utils.getS3Bucket(uri));
assertEquals("key from uri", path, AmazonS3Utils.getS3Key(uri));
AmazonS3Utils.createS3(uriString);
AmazonS3Utils.createS3(uriString,
null,
new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()),
null);
}

}
Expand Down Expand Up @@ -87,9 +92,9 @@ public void getS3InfoTest() {
keyToTests.put("a", onePartKeyNoSlashTests);
keyToTests.put("a/b/c/d", multiPartKeyNoSlashTests);

for (Map.Entry<String, String[]> tests : keyToTests.entrySet()) {
for (final Map.Entry<String, String[]> tests : keyToTests.entrySet()) {
final String expectedKey = tests.getKey();
for (String uri : tests.getValue()) {
for (final String uri : tests.getValue()) {
assertEquals(bucketName, AmazonS3Utils.getS3Bucket(uri));
assertEquals("Unexpected key for " + uri, expectedKey, AmazonS3Utils.getS3Key(uri));
}
Expand Down

0 comments on commit f2901bd

Please sign in to comment.