kopia lustrzana https://github.com/gaul/s3proxy
Strona:
Using S3Proxy in Java projects
Strony
Client compatibility list
Home
Middleware eventual consistency
Middleware alias blobstore
Middleware bucket locator
Middleware large object mocking
Middleware read only
Middleware regex
Middleware sharded backend
SSL support
Storage backend compatibility
Storage backend examples
Tips
Using S3Proxy in Java projects
17
Using S3Proxy in Java projects
Ohad Shai edytuje tę stronę 2020-09-15 16:44:50 +03:00
Spis treści
Maven artifacts
Java projects can include the latest S3Proxy release via Maven artifacts:
<dependency>
<groupId>org.gaul</groupId>
<artifactId>s3proxy</artifactId>
<version>1.7.1</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.7.2-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:
- 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 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 AwsClientBuilder.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());
}