Skip to content

Commit

Permalink
support s3 r/w with and without credentials
Browse files Browse the repository at this point in the history
fix small bug
  • Loading branch information
StephanPreibisch committed Sep 13, 2024
1 parent c3295bb commit dfc8962
Showing 1 changed file with 83 additions and 6 deletions.
89 changes: 83 additions & 6 deletions src/main/java/util/URITools.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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 )
Expand All @@ -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 )
Expand All @@ -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
Expand Down

0 comments on commit dfc8962

Please sign in to comment.