Skip to content

Using S3Proxy in Java projects

Adrian Woodhead edited this page Mar 2, 2018 · 18 revisions

Maven artifacts

Java projects can include the latest S3Proxy release via Maven artifacts:

<dependency>
    <groupId>org.gaul</groupId>
    <artifactId>s3proxy</artifactId>
    <version>1.5.5</version>
</dependency>

Sonatype also provides pre-release snapshots:

<repositories>
    <repository>
        <id>apache-snapshots</id>
        <url>https://repository.apache.org/content/repositories/snapshots</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>sonatype-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

...

<dependency>
    <groupId>org.gaul</groupId>
    <artifactId>s3proxy</artifactId>
    <version>1.6.0-SNAPSHOT</version>
</dependency>

Usage

Instantiate S3Proxy by creating a backend BlobStore object and a frontend S3Proxy object. An example configuring the filesystem backend and listening on port 8080:

Properties properties = new Properties();
properties.setProperty("jclouds.filesystem.basedir", "/tmp/blobstore");

BlobStoreContext context = ContextBuilder
        .newBuilder("filesystem")
        .credentials("identity", "credential")
        .overrides(properties)
        .build(BlobStoreContext.class);

S3Proxy s3Proxy = S3Proxy.builder()
        .blobStore(context.getBlobStore())
        .endpoint(URI.create("http://127.0.0.1:8080"))
        .build();

s3Proxy.start();
while (!s3Proxy.getState().equals(AbstractLifeCycle.STARTED)) {
    Thread.sleep(1);
}

The S3Proxy Main class and unit tests demonstrate more complicated configurations:

S3Proxy Junit Rule

The S3Proxy Junit Rule provides a way to write JUnit tests for classes that require the S3 API without actually using S3. It utilizes S3Proxy under the covers to provide a "bare minimum", easy to use version of S3 that plugs into the JUnit lifecycle by starting the proxy before each test and shutting it down afterwards.

Example

@Rule
public S3ProxyRule s3Proxy = S3ProxyRule.builder()
                   .withCredentials("access", "secret")
                   .build();

@Test
public void example() {
    //set up an AWS S3 client using the rule as an endpoint
    AmazonS3 s3Client = AmazonS3ClientBuilder
            .standard()
            .withCredentials(
              new AWSStaticCredentialsProvider(
                new BasicAWSCredentials(s3Proxy.getAccessKey(), 
                                        s3Proxy.getSecretKey())))
            .withEndpointConfiguration(
              new EndpointConfiguration(s3Proxy.getUri().toString(), 
                                        Regions.US_EAST_1.getName()))
            .build();
    s3Client.createBucket("test_bucket");
    
    //exercise your code that you want to test, using the above client
    myS3Uploader.uploadFile(s3Client, someFile);

    //verify the expected results of the above code
    List<S3ObjectSummary> summaries = s3Client.listObjects("test_bucket")
                                      .getObjectSummaries();
    assertThat(summaries).hasSize(1);
    assertThat(summaries.get(0).getSize()).isEqualTo(someFile.length());
}