Repository navigation
Using S3Proxy in Java projects
Java projects can include the latest S3Proxy release from Maven Central:
<dependency>
<groupId>org.gaul</groupId>
<artifactId>s3proxy</artifactId>
<version>4.2.0</version>
</dependency>The releases page lists the current version. S3Proxy requires Java 17 or newer.
Pre-release snapshots come from the Central Portal's snapshot repository:
<repositories>
<repository>
<id>central-portal-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
...
<dependency>
<groupId>org.gaul</groupId>
<artifactId>s3proxy</artifactId>
<version>4.3.0-SNAPSHOT</version>
</dependency>Create a backend BlobStore with BlobStores.create and hand it to an S3Proxy. This example stores objects on the local filesystem and listens on port 8080 without authentication:
import java.net.URI;
import java.util.Properties;
import org.gaul.s3proxy.BlobStores;
import org.gaul.s3proxy.S3Proxy;
import org.gaul.s3proxy.blobstore.BlobStore;
Properties properties = new Properties();
properties.setProperty("jclouds.filesystem.basedir", "/tmp/blobstore");
BlobStore blobStore = BlobStores.create("filesystem", properties);
S3Proxy s3Proxy = S3Proxy.builder()
.blobStore(blobStore)
.endpoint(URI.create("http://127.0.0.1:8080"))
.build();
s3Proxy.start();
// ...
s3Proxy.stop();The provider name and the properties are the same ones a properties file uses; see the storage backend examples. The backend's credentials go in the jclouds.identity, jclouds.credential and jclouds.session-token properties. A deployment whose credentials expire passes a Supplier<Credentials> as a third argument instead, which the store asks again for every request.
To require clients to sign their requests, add awsAuthentication to the builder:
S3Proxy s3Proxy = S3Proxy.builder()
.blobStore(blobStore)
.endpoint(URI.create("http://127.0.0.1:8080"))
.awsAuthentication(AuthenticationType.AWS_V4, "access", "secret")
.build();An endpoint with port 0 picks a free port, which s3Proxy.getPort() reports after start().
The S3Proxy Main class and unit tests demonstrate more complicated configurations:
- https://github.com/gaul/s3proxy/blob/master/src/main/java/org/gaul/s3proxy/Main.java
- https://github.com/gaul/s3proxy/blob/master/src/test/java/org/gaul/s3proxy/AwsSdkTest.java
S3Proxy ships a JUnit 5 extension and a JUnit 4 rule for testing code that needs the S3 API without using S3. Each starts an S3Proxy over a temporary directory before every test, and stops it and empties the directory afterwards.
@RegisterExtension
static final S3ProxyExtension s3Proxy = S3ProxyExtension.builder()
.withCredentials("access", "secret")
.build();
@Test
void example() {
// an AWS SDK for Java 2.x client pointed at the extension
S3Client s3Client = S3Client.builder()
.endpointOverride(s3Proxy.getUri())
.region(Region.US_EAST_1)
.forcePathStyle(true)
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(s3Proxy.getAccessKey(),
s3Proxy.getSecretKey())))
.build();
s3Client.createBucket(b -> b.bucket("test-bucket"));
// exercise the code under test with the client
myS3Uploader.uploadFile(s3Client, someFile);
// verify what it did
List<S3Object> objects = s3Client.listObjectsV2(
b -> b.bucket("test-bucket")).contents();
assertThat(objects).hasSize(1);
assertThat(objects.get(0).size()).isEqualTo(someFile.length());
}@Rule
public S3ProxyRule s3Proxy = S3ProxyRule.builder()
.withCredentials("access", "secret")
.build();S3ProxyRule offers the same builder and the same getUri(), getAccessKey() and getSecretKey() accessors.
-
withCredentials(accessKey, secretKey)requires clients to sign with these; without it the proxy accepts unsigned requests. An overload takes anAuthenticationTypefirst. -
withPort(port)fixes the port; by default a free one is chosen. -
withBlobStoreProvider(provider)selects a backend other thanfilesystem, such astransient. -
withSecretStore(path, password)adds an HTTPS endpoint from a keystore, reported bygetSecureUri(). -
ignoreUnknownHeaders()acceptsx-amz-*headers that S3Proxy does not implement.