Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow differentiating blobstores by buckets #376

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions src/main/java/org/gaul/s3proxy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -101,8 +103,10 @@ public static void main(String[] args) throws Exception {
.build();
ExecutorService executorService = DynamicExecutors.newScalingThreadPool(
1, 20, 60 * 1000, factory);
ImmutableMap.Builder<String, Map.Entry<String, BlobStore>> locators =
ImmutableMap.Builder<Map.Entry<String, String>,
Map.Entry<String, BlobStore>> locators =
ImmutableMap.builder();
Set<Map.Entry<String, String>> parsedLocations = new HashSet<>();
for (File propertiesFile : options.propertiesFiles) {
Properties properties = new Properties();
try (InputStream is = new FileInputStream(propertiesFile)) {
Expand All @@ -117,14 +121,42 @@ public static void main(String[] args) throws Exception {

String s3ProxyAuthorizationString = properties.getProperty(
S3ProxyConstants.PROPERTY_AUTHORIZATION);
ImmutableList.Builder<String> locatorBuckets =
new ImmutableList.Builder<>();
for (String key : properties.stringPropertyNames()) {
if (key.startsWith(S3ProxyConstants.PROPERTY_BUCKET_LOCATOR)) {
locatorBuckets.add(properties.getProperty(key));
}
}
ImmutableList<String> buckets = locatorBuckets.build();
String localIdentity = null;
String localCredential = null;
if (AuthenticationType.fromString(s3ProxyAuthorizationString) !=
AuthenticationType.NONE) {
String localIdentity = properties.getProperty(
localIdentity = properties.getProperty(
S3ProxyConstants.PROPERTY_IDENTITY);
String localCredential = properties.getProperty(
localCredential = properties.getProperty(
S3ProxyConstants.PROPERTY_CREDENTIAL);
locators.put(localIdentity, Maps.immutableEntry(
localCredential, blobStore));
Map.Entry<String, String> key = Maps.immutableEntry(
localIdentity, null);
if (parsedLocations.add(key)) {
locators.put(key, Maps.immutableEntry(
localCredential, blobStore));
}
}
if (!buckets.isEmpty()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to check whether buckets is empty? The foreach loop should handle this already.

for (String bucket : buckets) {
Map.Entry<String, String> key = Maps.immutableEntry(
localIdentity, bucket);
if (!parsedLocations.add(key)) {
System.err.printf("The same bucket locator cannot be" +
" used in two properties files: %s\n",
bucket);
System.exit(1);
}
locators.put(key,
Maps.immutableEntry(localCredential, blobStore));
}
}

S3Proxy.Builder s3ProxyBuilder2 = S3Proxy.Builder
Expand All @@ -149,23 +181,22 @@ public static void main(String[] args) throws Exception {
throw e;
}

final Map<String, Map.Entry<String, BlobStore>> locator =
locators.build();
final Map<Map.Entry<String, String>, Map.Entry<String, BlobStore>>
locator = locators.build();
if (!locator.isEmpty()) {
s3Proxy.setBlobStoreLocator(new BlobStoreLocator() {
@Override
public Map.Entry<String, BlobStore> locateBlobStore(
String identity, String container, String blob) {
Map.Entry<String, BlobStore> entry = locator.get(
Maps.immutableEntry(identity, container));
if (entry != null) {
return entry;
}
if (identity == null) {
if (locator.size() == 1) {
return locator.entrySet().iterator().next()
.getValue();
}
throw new IllegalArgumentException(
"cannot use anonymous access with multiple" +
" backends");
return locator.entrySet().iterator().next().getValue();
}
return locator.get(identity);
return locator.get(Maps.immutableEntry(identity, null));
}
});
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/gaul/s3proxy/S3ProxyConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public final class S3ProxyConstants {
"s3proxy.max-single-part-object-size";
public static final String PROPERTY_V4_MAX_NON_CHUNKED_REQUEST_SIZE =
"s3proxy.v4-max-non-chunked-request-size";
/** Used to locate blobstores by specified bucket names. Each property
* file should contain a list of buckets associated with it, e.g.
* s3proxy.bucket-locator.1 = data
* s3proxy.bucket-locator.2 = metadata
* s3proxy.bucket-locator.3 = other
* When a request is made for the specified bucket, the backend defined
* in that properties file is used. This allows using the same
* credentials in multiple properties file and select the backend based
* on the bucket names.
*/
public static final String PROPERTY_BUCKET_LOCATOR =
"s3proxy.bucket-locator";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unclear how to use this feature -- can you add some more documentation? Does each properties file have a unique identifier? Is it used for something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanded the description. The bucket-locator property specifies the bucket names associated with the backend specified in the properties file. Different properties files can specify different buckets, allowing a client to use the same S3Proxy credentials, but have the requests route to different backends.

/** When true, model eventual consistency using two storage backends. */
public static final String PROPERTY_EVENTUAL_CONSISTENCY =
"s3proxy.eventual-consistency";
Expand Down