Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't list if where not necessarry #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public class AmazonS3KeyValueAccess implements KeyValueAccess {
private final URI containerURI;
private final String bucketName;

private final boolean createBucket;
private Boolean bucketCheckedAndExists = null;

private static URI uncheckedContainterLocationStringToURI(String uri) {

try {
Expand Down Expand Up @@ -119,6 +122,7 @@ public AmazonS3KeyValueAccess(final AmazonS3 s3, final URI containerURI, final b
this.containerURI = containerURI;

this.bucketName = AmazonS3Utils.getS3Bucket(containerURI);
this.createBucket = createBucket;

if (!s3.doesBucketExistV2(bucketName)) {
if (createBucket) {
Expand All @@ -136,6 +140,51 @@ public AmazonS3KeyValueAccess(final AmazonS3 s3, final URI containerURI, final b
}
}

private boolean bucketExists() {

return bucketCheckedAndExists = bucketCheckedAndExists != null
? bucketCheckedAndExists
: s3.doesBucketExistV2(bucketName);
}

private void createBucket() {

if (!createBucket)
throw new N5Exception("Create Bucket Not Allowed");

if (bucketExists())
return;

Region region;
try {
region = s3.getRegion();
} catch (final IllegalStateException e) {
region = Region.US_Standard;
}
try {
s3.createBucket(new CreateBucketRequest(bucketName, region));
bucketCheckedAndExists = true;
} catch (Exception e) {
throw new N5Exception("Could not create bucket " + bucketName, e);
}

}

private void deleteBucket() {
if (!createBucket)
throw new N5Exception("Delete Bucket Not Allowed");

if (!bucketExists())
return;

try {
s3.deleteBucket(bucketName);
bucketCheckedAndExists = false;
} catch (Exception e) {
throw new N5Exception("Could not delete bucket " + bucketName, e);
}
}

@Override
public String[] components(final String path) {

Expand Down Expand Up @@ -431,6 +480,10 @@ public String[] list(final String normalPath) throws IOException {
@Override
public void createDirectories(final String normalPath) {

if (!bucketExists() && createBucket){
createBucket();
}

String path = "";
for (final String component : components(removeLeadingSlash(normalPath))) {
path = addTrailingSlash(compose(path, component));
Expand Down Expand Up @@ -476,7 +529,7 @@ public void delete(final String normalPath) {
}
}

s3.deleteBucket(bucketName);
deleteBucket();
return;
}

Expand Down Expand Up @@ -607,7 +660,7 @@ public InputStream newInputStream() {
try {
object = s3.getObject(bucketName, path);
} catch (final AmazonServiceException e) {
if (e.getStatusCode() == 404)
if (e.getStatusCode() == 404 || e.getStatusCode() == 403)
throw new N5Exception.N5NoSuchKeyException("No such key", e);
throw e;
}
Expand Down
43 changes: 39 additions & 4 deletions src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class AmazonS3Utils {
public static final Pattern AWS_ENDPOINT_PATTERN = Pattern.compile("^(.+\\.)?(s3\\..*amazonaws\\.com)", Pattern.CASE_INSENSITIVE);
public final static Pattern S3_SCHEME = Pattern.compile("s3", Pattern.CASE_INSENSITIVE);

private AmazonS3Utils() {
private static final String DISABLE_WARNING_KEY = "aws.java.v1.disableDeprecationAnnouncement";

private AmazonS3Utils() {
}

public static String getS3Bucket(final String uri) {
Expand Down Expand Up @@ -102,14 +103,30 @@ private static Regions parseRegion(String stringRegionFromUri) {

public static AWSCredentialsProvider getS3Credentials(final AWSCredentials s3Credentials, final boolean s3Anonymous) {

/*
* TODO necessary until we update to AWS SDK (2.x)
* see https://github.com/saalfeldlab/n5-aws-s3/issues/28
*/
final String initialDisableWarningPropertyValue = System.getProperty(DISABLE_WARNING_KEY);
if( initialDisableWarningPropertyValue == null)
System.setProperty(DISABLE_WARNING_KEY, "true");

if (s3Credentials != null) {
return new AWSStaticCredentialsProvider(s3Credentials);
final AWSStaticCredentialsProvider provider = new AWSStaticCredentialsProvider(s3Credentials);
resetDisableWarningValue(initialDisableWarningPropertyValue);
return provider;
} else {
// if not anonymous, try finding credentials
if (!s3Anonymous)
return new DefaultAWSCredentialsProviderChain();
if (!s3Anonymous) {
final DefaultAWSCredentialsProviderChain provider = new DefaultAWSCredentialsProviderChain();
resetDisableWarningValue(initialDisableWarningPropertyValue);
return provider;
}
else
{
resetDisableWarningValue(initialDisableWarningPropertyValue);
return null;
}
}
}

Expand Down Expand Up @@ -190,6 +207,15 @@ public static AmazonS3 createS3(
@Nullable final Regions region) {

final boolean isAmazon = endpointConfiguration == null || AmazonS3Utils.AWS_ENDPOINT_PATTERN.matcher(endpointConfiguration.getServiceEndpoint()).find();

/*
* TODO necessary until we update to AWS SDK (2.x)
* see https://github.com/saalfeldlab/n5-aws-s3/issues/28
*/
final String initialDisableWarningPropertyValue = System.getProperty(DISABLE_WARNING_KEY);
if( initialDisableWarningPropertyValue == null)
System.setProperty(DISABLE_WARNING_KEY, "true");

final AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();

if (!isAmazon)
Expand Down Expand Up @@ -220,12 +246,21 @@ else if (region != null)
// bucket not detected with anonymous credentials, try detecting credentials
// and return it even if it can't detect the bucket, since there's nothing else to do
builder.withCredentials(new DefaultAWSCredentialsProviderChain());
resetDisableWarningValue(initialDisableWarningPropertyValue);
return builder.build();
}
}

resetDisableWarningValue(initialDisableWarningPropertyValue);
return s3;
}

private static void resetDisableWarningValue(final String initialDisableWarningPropertyValue) {

if (initialDisableWarningPropertyValue == null)
System.clearProperty(DISABLE_WARNING_KEY);
}

private static boolean canListBucket(final AmazonS3 s3, final String bucket) {

final ListObjectsV2Request request = new ListObjectsV2Request();
Expand Down
Loading