2323import java .io .InputStream ;
2424import java .io .PrintStream ;
2525import java .nio .charset .StandardCharsets ;
26+ import java .nio .file .FileSystems ;
27+ import java .nio .file .PathMatcher ;
2628import java .util .ArrayList ;
29+ import java .util .HashMap ;
30+ import java .util .HashSet ;
2731import java .util .List ;
2832import java .util .Map ;
2933import java .util .Properties ;
34+ import java .util .Set ;
3035import java .util .concurrent .ExecutorService ;
3136import java .util .concurrent .Executors ;
3237import java .util .concurrent .ThreadFactory ;
@@ -103,6 +108,11 @@ public static void main(String[] args) throws Exception {
103108 1 , 20 , 60 * 1000 , factory );
104109 ImmutableMap .Builder <String , Map .Entry <String , BlobStore >> locators =
105110 ImmutableMap .builder ();
111+ ImmutableMap .Builder <String , List <Map .Entry <PathMatcher ,
112+ BlobStore >>> globLocators = ImmutableMap .builder ();
113+ Map <String , ImmutableList .Builder <Map .Entry <PathMatcher , BlobStore >>>
114+ globBuilders = new HashMap <>();
115+ Set <String > parsedIdentities = new HashSet <>();
106116 for (File propertiesFile : options .propertiesFiles ) {
107117 Properties properties = new Properties ();
108118 try (InputStream is = new FileInputStream (propertiesFile )) {
@@ -117,14 +127,35 @@ public static void main(String[] args) throws Exception {
117127
118128 String s3ProxyAuthorizationString = properties .getProperty (
119129 S3ProxyConstants .PROPERTY_AUTHORIZATION );
130+ ImmutableList .Builder <String > locatorBuckets =
131+ new ImmutableList .Builder <>();
132+
133+ String localIdentity = "" ;
120134 if (AuthenticationType .fromString (s3ProxyAuthorizationString ) !=
121135 AuthenticationType .NONE ) {
122- String localIdentity = properties .getProperty (
136+ localIdentity = properties .getProperty (
123137 S3ProxyConstants .PROPERTY_IDENTITY );
124138 String localCredential = properties .getProperty (
125139 S3ProxyConstants .PROPERTY_CREDENTIAL );
126- locators .put (localIdentity , Maps .immutableEntry (
127- localCredential , blobStore ));
140+ if (parsedIdentities .add (localIdentity )) {
141+ locators .put (localIdentity ,
142+ Maps .immutableEntry (localCredential , blobStore ));
143+ }
144+ }
145+ ImmutableList .Builder <Map .Entry <PathMatcher , BlobStore >>
146+ globBuilder = globBuilders .get (localIdentity );
147+ if (globBuilder == null ) {
148+ globBuilder = new ImmutableList .Builder <>();
149+ globBuilders .put (localIdentity , globBuilder );
150+ }
151+ for (String key : properties .stringPropertyNames ()) {
152+ if (key .startsWith (S3ProxyConstants .PROPERTY_BUCKET_LOCATOR )) {
153+ locatorBuckets .add (properties .getProperty (key ));
154+ globBuilder .add (Maps .immutableEntry (
155+ FileSystems .getDefault ().getPathMatcher (
156+ "glob:" + properties .getProperty (key )),
157+ blobStore ));
158+ }
128159 }
129160
130161 S3Proxy .Builder s3ProxyBuilder2 = S3Proxy .Builder
@@ -149,21 +180,51 @@ public static void main(String[] args) throws Exception {
149180 throw e ;
150181 }
151182
183+ for (Map .Entry <String , ImmutableList .Builder <Map .Entry <PathMatcher ,
184+ BlobStore >>> entry : globBuilders .entrySet ()) {
185+ globLocators .put (entry .getKey (), entry .getValue ().build ());
186+ }
187+
152188 final Map <String , Map .Entry <String , BlobStore >> locator =
153189 locators .build ();
190+ final Map <String , List <Map .Entry <PathMatcher , BlobStore >>>
191+ globLocator = globLocators .build ();
154192 if (!locator .isEmpty ()) {
155193 s3Proxy .setBlobStoreLocator (new BlobStoreLocator () {
156194 @ Override
157195 public Map .Entry <String , BlobStore > locateBlobStore (
158196 String identity , String container , String blob ) {
159- if (identity == null ) {
160- if (locator .size () == 1 ) {
161- return locator .entrySet ().iterator ().next ()
162- .getValue ();
197+ Map .Entry <String , BlobStore > locatorEntry =
198+ locator .get (identity );
199+ List <Map .Entry <PathMatcher , BlobStore >> globEntries =
200+ globLocator .get (identity );
201+ if (globEntries != null ) {
202+ for (Map .Entry <PathMatcher , BlobStore > entry :
203+ globLocator .get (identity )) {
204+ if (entry .getKey ().matches (FileSystems .getDefault ()
205+ .getPath (container ))) {
206+ return Maps .immutableEntry (
207+ locatorEntry .getKey (),
208+ entry .getValue ());
209+ }
210+ }
211+ }
212+ // Check if the anonymous access globs were configured
213+ globEntries = globLocator .get ("" );
214+ if (globEntries != null ) {
215+ for (Map .Entry <PathMatcher ,
216+ BlobStore > entry :
217+ globEntries ) {
218+ if (entry .getKey ().matches (FileSystems .getDefault ()
219+ .getPath (container ))) {
220+ return Maps .immutableEntry (
221+ locatorEntry .getKey (),
222+ entry .getValue ());
223+ }
163224 }
164- throw new IllegalArgumentException (
165- "cannot use anonymous access with multiple" +
166- " backends" );
225+ }
226+ if ( identity == null ) {
227+ return locator . entrySet (). iterator (). next (). getValue ( );
167228 }
168229 return locator .get (identity );
169230 }
0 commit comments