Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 1e79b15

Browse files
committed
#83 Add property s3fs_path_style_access to allow to customize the url
1 parent 2624c0f commit 1e79b15

19 files changed

+158
-43
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This project provides a first API implementation, little optimized, but "complet
1414
<dependency>
1515
<groupId>com.upplication</groupId>
1616
<artifactId>s3fs</artifactId>
17-
<version>2.0.0</version>
17+
<version>2.2.0</version>
1818
</dependency>
1919
```
2020

@@ -79,6 +79,8 @@ Complete settings lists:
7979
* s3fs_socket_timeout
8080
* s3fs_user_agent
8181
* s3fs_amazon_s3_factory
82+
* s3fs_signer_override
83+
* s3fs_path_style_access
8284

8385
##### Set endpoint to reduce data latency in your applications
8486

@@ -138,7 +140,6 @@ Now you can inject in any spring component:
138140
```java
139141
@Autowired
140142
private FileSystem s3FileSystem;
141-
142143
```
143144

144145
##### What is new 2.0.0
@@ -147,6 +148,7 @@ private FileSystem s3FileSystem;
147148
* Removed META-INF/services/java.nio.file.spi.FileTypeDetector #78
148149
* Bucket are filestores and root directories for a bucket is the bucket itself.
149150
* getFileName for a root Path is ```null```
151+
* Added new method toURL to S3Path and can be customized with the property s3fs_path_style_access #83
150152
* Improved S3Path Tests
151153

152154
#### Features:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.upplication</groupId>
55
<artifactId>s3fs</artifactId>
66
<packaging>jar</packaging>
7-
<version>2.1.0</version>
7+
<version>2.2.0</version>
88
<name>s3fs</name>
99
<description>S3 filesystem provider for Java 7</description>
1010
<url>https://github.com/Upplication/Amazon-S3-FileSystem-NIO2</url>

src/main/java/com/upplication/s3fs/AmazonS3Factory.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
99
import com.amazonaws.metrics.RequestMetricCollector;
1010
import com.amazonaws.services.s3.AmazonS3;
11+
import com.amazonaws.services.s3.S3ClientOptions;
1112

1213
import java.net.URI;
1314
import java.util.Properties;
1415

16+
1517
/**
1618
* Factory base class to create a new AmazonS3 instance.
1719
*/
@@ -35,6 +37,7 @@ public abstract class AmazonS3Factory {
3537
public static final String SOCKET_TIMEOUT = "s3fs_socket_timeout";
3638
public static final String USER_AGENT = "s3fs_user_agent";
3739
public static final String SIGNER_OVERRIDE = "s3fs_signer_override";
40+
public static final String PATH_STYLE_ACCESS = "s3fs_path_style_access";
3841

3942
/**
4043
* Build a new Amazon S3 instance with the URI and the properties provided
@@ -50,6 +53,9 @@ public AmazonS3 getAmazonS3(URI uri, Properties props) {
5053
else
5154
client.setEndpoint(uri.getHost());
5255
}
56+
57+
client.setS3ClientOptions(getClientOptions(props));
58+
5359
return client;
5460
}
5561

@@ -84,6 +90,15 @@ protected RequestMetricCollector getRequestMetricsCollector(Properties props) {
8490
return requestMetricCollector;
8591
}
8692

93+
protected S3ClientOptions getClientOptions(Properties props) {
94+
S3ClientOptions.Builder builder = S3ClientOptions.builder();
95+
if (props.getProperty(PATH_STYLE_ACCESS) != null &&
96+
Boolean.parseBoolean(props.getProperty(PATH_STYLE_ACCESS)))
97+
builder.setPathStyleAccess(true);
98+
99+
return builder.build();
100+
}
101+
87102
protected ClientConfiguration getClientConfiguration(Properties props) {
88103
ClientConfiguration clientConfiguration = new ClientConfiguration();
89104
if (props.getProperty(CONNECTION_TIMEOUT) != null)

src/main/java/com/upplication/s3fs/S3FileSystemProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class S3FileSystemProvider extends FileSystemProvider {
7171
private static final ConcurrentMap<String, S3FileSystem> fileSystems = new ConcurrentHashMap<>();
7272
private static final List<String> PROPS_TO_OVERLOAD = Arrays.asList(ACCESS_KEY, SECRET_KEY, REQUEST_METRIC_COLLECTOR_CLASS, CONNECTION_TIMEOUT, MAX_CONNECTIONS, MAX_ERROR_RETRY, PROTOCOL, PROXY_DOMAIN,
7373
PROXY_HOST, PROXY_PASSWORD, PROXY_PORT, PROXY_USERNAME, PROXY_WORKSTATION, SOCKET_SEND_BUFFER_SIZE_HINT, SOCKET_RECEIVE_BUFFER_SIZE_HINT, SOCKET_TIMEOUT,
74-
USER_AGENT, AMAZON_S3_FACTORY_CLASS);
74+
USER_AGENT, AMAZON_S3_FACTORY_CLASS, SIGNER_OVERRIDE, PATH_STYLE_ACCESS);
7575

7676
private S3Utils s3Utils = new S3Utils();
7777
private Cache cache = new Cache();

src/main/java/com/upplication/s3fs/S3Path.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.IOException;
1010
import java.io.UnsupportedEncodingException;
1111
import java.net.URI;
12+
import java.net.URL;
1213
import java.net.URLDecoder;
1314
import java.nio.file.*;
1415
import java.util.Iterator;
@@ -60,8 +61,8 @@ public S3Path(S3FileSystem fileSystem, String first, String... more) {
6061

6162
List<String> pathsURI = Lists
6263
.newArrayList(Splitter.on(PATH_SEPARATOR)
63-
.omitEmptyStrings()
64-
.split(first));
64+
.omitEmptyStrings()
65+
.split(first));
6566

6667
if (hasBucket) { // absolute path
6768

@@ -468,6 +469,26 @@ public URI toUri() {
468469
}
469470
}
470471

472+
/**
473+
* Get the url for the s3Path.
474+
*
475+
* The url represents a Uniform Resource
476+
* Locator, a pointer to a "resource" on the World
477+
* Wide Web.
478+
*
479+
* All S3Path has a URL if is absolute
480+
*
481+
* @see com.amazonaws.services.s3.AmazonS3#getUrl(String, String)
482+
* @see S3Path#toUri() for unique resource identifier
483+
* @return URL or null if is not absoulte
484+
*/
485+
public URL toURL() {
486+
if (!this.isAbsolute())
487+
return null;
488+
489+
return this.getFileSystem().getClient().getUrl(this.fileStore.name(), this.getKey());
490+
}
491+
471492
@Override
472493
public Path toAbsolutePath() {
473494
if (isAbsolute()) {
@@ -507,7 +528,7 @@ public Iterator<Path> iterator() {
507528
}
508529

509530
List<String> paths = uriToList();
510-
String lastPath = paths.get(paths.size()-1);
531+
String lastPath = paths.get(paths.size() - 1);
511532

512533
for (String path : uriToList()) {
513534
String pathFinal = path + PATH_SEPARATOR;

src/test/java/com/upplication/s3fs/AmazonS3ClientFactoryTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import static com.amazonaws.SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY;
44
import static com.amazonaws.SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY;
55
import static com.upplication.s3fs.AmazonS3Factory.*;
6-
import static org.junit.Assert.assertEquals;
7-
import static org.junit.Assert.assertNull;
8-
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.*;
97

108
import java.net.URI;
119
import java.util.Properties;
@@ -43,6 +41,7 @@ public void neverTrustTheDefaults() {
4341
props.setProperty(SOCKET_TIMEOUT, "30");
4442
props.setProperty(USER_AGENT, "I-am-Groot");
4543
props.setProperty(SIGNER_OVERRIDE, "S3SignerType");
44+
props.setProperty(PATH_STYLE_ACCESS, "true");
4645
ExposingAmazonS3Client client = (ExposingAmazonS3Client) clientFactory.getAmazonS3(S3EndpointConstant.S3_GLOBAL_URI_TEST, props);
4746
AWSCredentialsProvider credentialsProvider = client.getAWSCredentialsProvider();
4847
AWSCredentials credentials = credentialsProvider.getCredentials();
@@ -65,6 +64,7 @@ public void neverTrustTheDefaults() {
6564
assertEquals(30, clientConfiguration.getSocketTimeout());
6665
assertEquals("I-am-Groot", clientConfiguration.getUserAgent());
6766
assertEquals("S3SignerType", clientConfiguration.getSignerOverride());
67+
assertTrue(client.getClientOptions().isPathStyleAccess());
6868
}
6969

7070
@Test
@@ -95,6 +95,7 @@ public void theDefaults() {
9595
assertEquals(50000, clientConfiguration.getSocketTimeout());
9696
assertTrue(clientConfiguration.getUserAgent().startsWith("aws-sdk-java"));
9797
assertNull(clientConfiguration.getSignerOverride());
98+
assertFalse(client.getClientOptions().isPathStyleAccess());
9899
}
99100

100101
@Test(expected = IllegalArgumentException.class)

src/test/java/com/upplication/s3fs/AmazonS3ClientIT.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static com.upplication.s3fs.util.EnvironmentBuilder.getRealEnv;
66
import static java.util.UUID.randomUUID;
77
import static org.junit.Assert.assertNotNull;
8-
import static org.junit.Assert.fail;
98

109
import java.io.ByteArrayInputStream;
1110
import java.io.IOException;
@@ -15,11 +14,9 @@
1514
import java.util.Map;
1615

1716
import org.junit.Before;
18-
import org.junit.Ignore;
1917

2018
import com.amazonaws.auth.BasicAWSCredentials;
2119
import com.amazonaws.services.s3.AmazonS3;
22-
import com.amazonaws.services.s3.model.AmazonS3Exception;
2320
import com.amazonaws.services.s3.model.ObjectMetadata;
2421
import com.amazonaws.services.s3.model.PutObjectResult;
2522
import com.upplication.s3fs.util.EnvironmentBuilder;

src/test/java/com/upplication/s3fs/FileSystemProvider/DeleteTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
import java.io.IOException;
1414
import java.net.URI;
1515
import java.nio.file.*;
16-
import java.util.Properties;
1716

1817
import static com.upplication.s3fs.AmazonS3Factory.ACCESS_KEY;
1918
import static com.upplication.s3fs.AmazonS3Factory.SECRET_KEY;
20-
import static org.junit.Assert.assertArrayEquals;
2119
import static org.junit.Assert.assertTrue;
22-
import static org.mockito.Mockito.*;
2320

2421
public class DeleteTest extends S3UnitTestBase {
2522

src/test/java/com/upplication/s3fs/FileSystemProvider/GetFileSystemIT.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import com.upplication.s3fs.S3FileSystemProvider;
55
import org.junit.Before;
66
import org.junit.Test;
7-
import org.mockito.ArgumentMatcher;
87

98
import java.io.IOException;
10-
import java.net.URI;
119
import java.nio.file.FileSystem;
1210
import java.nio.file.FileSystemNotFoundException;
1311
import java.nio.file.FileSystems;
@@ -18,8 +16,6 @@
1816
import static com.upplication.s3fs.AmazonS3Factory.SECRET_KEY;
1917
import static com.upplication.s3fs.util.S3EndpointConstant.S3_GLOBAL_URI_IT;
2018
import static org.junit.Assert.*;
21-
import static org.mockito.Matchers.argThat;
22-
import static org.mockito.Matchers.eq;
2319
import static org.mockito.Mockito.*;
2420

2521
public class GetFileSystemIT {

src/test/java/com/upplication/s3fs/FileSystemProvider/NewFileSystemTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import static org.mockito.Matchers.eq;
1515
import static org.mockito.Mockito.doCallRealMethod;
1616
import static org.mockito.Mockito.doReturn;
17-
import static org.mockito.Mockito.spy;
1817
import static org.mockito.Mockito.verify;
1918

2019
import java.io.IOException;

0 commit comments

Comments
 (0)