Skip to content

Commit

Permalink
don't list if where not necessarry
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhulbert committed Dec 10, 2024
1 parent 6587cc9 commit f84541d
Showing 1 changed file with 41 additions and 1 deletion.
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,38 @@ public AmazonS3KeyValueAccess(final AmazonS3 s3, final URI containerURI, final b
}
}

private boolean bucketExists() {
if (bucketCheckedAndExists != null)
return bucketCheckedAndExists;

bucketCheckedAndExists = s3.doesBucketExistV2(bucketName);
return bucketCheckedAndExists;
}

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);
}

}

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

Expand Down Expand Up @@ -431,6 +467,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 @@ -607,7 +647,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

0 comments on commit f84541d

Please sign in to comment.