Skip to content

Commit 558efb7

Browse files
timuralpgaul
authored andcommitted
Allow differentiating blob stores by buckets
Allows differentiating the backend blob stores by configured buckets. The commit allows using the same s3proxy credentials across the different backends and relies on the bucket names to disambiguate them. If no buckets are configured, the prior blob store selection algorithm is used, which returns the first configured blob store for the specified identity. The patch supports the glob syntax, which allows specifying groups of buckets more easily. However, there is no checking for overlapping globs. Fixes: #371
1 parent c055dfd commit 558efb7

2 files changed

Lines changed: 83 additions & 10 deletions

File tree

src/main/java/org/gaul/s3proxy/Main.java

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
import java.io.InputStream;
2424
import java.io.PrintStream;
2525
import java.nio.charset.StandardCharsets;
26+
import java.nio.file.FileSystems;
27+
import java.nio.file.PathMatcher;
2628
import java.util.ArrayList;
29+
import java.util.HashMap;
30+
import java.util.HashSet;
2731
import java.util.List;
2832
import java.util.Map;
2933
import java.util.Properties;
34+
import java.util.Set;
3035
import java.util.concurrent.ExecutorService;
3136
import java.util.concurrent.Executors;
3237
import 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
}

src/main/java/org/gaul/s3proxy/S3ProxyConstants.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ public final class S3ProxyConstants {
6666
"s3proxy.max-single-part-object-size";
6767
public static final String PROPERTY_V4_MAX_NON_CHUNKED_REQUEST_SIZE =
6868
"s3proxy.v4-max-non-chunked-request-size";
69+
/** Used to locate blobstores by specified bucket names. Each property
70+
* file should contain a list of buckets associated with it, e.g.
71+
* s3proxy.bucket-locator.1 = data
72+
* s3proxy.bucket-locator.2 = metadata
73+
* s3proxy.bucket-locator.3 = other
74+
* When a request is made for the specified bucket, the backend defined
75+
* in that properties file is used. This allows using the same
76+
* credentials in multiple properties file and select the backend based
77+
* on the bucket names.
78+
*/
79+
public static final String PROPERTY_BUCKET_LOCATOR =
80+
"s3proxy.bucket-locator";
6981
/** When true, model eventual consistency using two storage backends. */
7082
public static final String PROPERTY_EVENTUAL_CONSISTENCY =
7183
"s3proxy.eventual-consistency";

0 commit comments

Comments
 (0)