From dfc8962bc3709859debeb145acacbe79d073e489 Mon Sep 17 00:00:00 2001 From: Stephan Preibisch Date: Fri, 13 Sep 2024 15:22:48 -0400 Subject: [PATCH] support s3 r/w with and without credentials fix small bug --- src/main/java/util/URITools.java | 89 +++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/src/main/java/util/URITools.java b/src/main/java/util/URITools.java index b330fb60..c3bd6f8f 100644 --- a/src/main/java/util/URITools.java +++ b/src/main/java/util/URITools.java @@ -25,6 +25,7 @@ import org.janelia.saalfeldlab.n5.s3.AmazonS3Utils; import org.janelia.saalfeldlab.n5.universe.N5Factory; import org.janelia.saalfeldlab.n5.universe.N5Factory.StorageFormat; +import org.janelia.saalfeldlab.n5.zarr.N5ZarrReader; import org.janelia.saalfeldlab.n5.zarr.N5ZarrWriter; import org.jdom2.Document; import org.jdom2.Element; @@ -47,6 +48,9 @@ public class URITools public static int cloudThreads = 256; + public static boolean useS3CredentialsWrite = true; + public static boolean useS3CredentialsRead = true; + public static class ParsedBucket { public String protocol; @@ -159,7 +163,24 @@ else if ( format.equals( StorageFormat.ZARR )) } else { - return new N5Factory().openWriter( format, uri ); // cloud support, avoid dependency hell if it is a local file + N5Writer n5w; + + try + { + System.out.println( "Trying anonymous writing ..." ); + n5w = new N5Factory().openWriter( format, uri ); + } + catch ( Exception e ) + { + System.out.println( "Anonymous failed; trying writing with credentials ..." ); + + N5Factory factory = new N5Factory(); + factory.s3UseCredentials(); + n5w = factory.openWriter( format, uri ); + } + + return n5w; + //return new N5Factory().openWriter( format, uri ); // cloud support, avoid dependency hell if it is a local file } } @@ -168,14 +189,32 @@ public static N5Reader instantiateN5Reader( final StorageFormat format, final UR if ( URITools.isFile( uri ) ) { if ( format.equals( StorageFormat.N5 )) - return new N5FSWriter( URITools.removeFilePrefix( uri ) ); + return new N5FSReader( URITools.removeFilePrefix( uri ) ); else if ( format.equals( StorageFormat.ZARR )) - return new N5ZarrWriter( URITools.removeFilePrefix( uri ) ); + return new N5ZarrReader( URITools.removeFilePrefix( uri ) ); else throw new RuntimeException( "Format: " + format + " not supported." ); } else - return new N5Factory().openReader( format, uri ); // cloud support, avoid dependency hell if it is a local file + { + N5Reader n5r; + + try + { + System.out.println( "Trying anonymous reading ..." ); + n5r = new N5Factory().openReader( format, uri ); + } + catch ( Exception e ) + { + System.out.println( "Anonymous failed; trying reading with credentials ..." ); + N5Factory factory = new N5Factory(); + factory.s3UseCredentials(); + n5r = factory.openReader( format, uri ); + } + + return n5r; + //return new N5Factory().openReader( format, uri ); // cloud support, avoid dependency hell if it is a local file + } } public static N5Reader instantiateGuessedN5Reader( final URI uri ) @@ -190,7 +229,25 @@ else if ( uri.toString().toLowerCase().endsWith( ".n5" ) ) throw new RuntimeException( "Format for local storage of: " + uri + " could not be guessed (make it end in .n5 or .zarr)." ); } else - return new N5Factory().openReader( uri.toString() ); // cloud support, avoid dependency hell if it is a local file + { + N5Reader n5r; + + try + { + System.out.println( "Trying anonymous reading ..." ); + n5r = new N5Factory().openReader( uri.toString() ); + } + catch ( Exception e ) + { + System.out.println( "Anonymous failed; trying reading with credentials ..." ); + N5Factory factory = new N5Factory(); + factory.s3UseCredentials(); + n5r = factory.openReader( uri.toString() ); + } + + return n5r; + //return new N5Factory().openReader( uri.toString() ); // cloud support, avoid dependency hell if it is a local file + } } public static N5Writer instantiateGuessedN5Writer( final URI uri ) @@ -205,7 +262,27 @@ else if ( uri.toString().toLowerCase().endsWith( ".n5" ) ) throw new RuntimeException( "Format for local storage of: " + uri + " could not be guessed (make it end in .n5 or .zarr)." ); } else - return new N5Factory().openWriter( uri.toString() ); // cloud support, avoid dependency hell if it is a local file + { + N5Writer n5w; + + try + { + System.out.println( "Trying anonymous writing ..." ); + n5w = new N5Factory().openWriter( uri.toString() ); + } + catch ( Exception e ) + { + System.out.println( "Anonymous failed; trying writing with credentials ..." ); + + N5Factory factory = new N5Factory(); + factory.s3UseCredentials(); + n5w = factory.openWriter( uri.toString() ); + } + + return n5w; + + //return new N5Factory().openWriter( uri.toString() ); // cloud support, avoid dependency hell if it is a local file + } } public static SpimData2 loadSpimData( final URI xmlURI, final XmlIoSpimData2 io ) throws SpimDataException