LoginSignup
5
2

More than 5 years have passed since last update.

【Java】S3アップロード・ダウンロードのテストを"S3 ninja"を使って行う

Last updated at Posted at 2017-02-17

S3のテスト用ダミーサーバーはFake S3というのがあると聞きましたがRubyらしく、私Java屋なのでJavaでも無いかなーと探したらS3 ninjaというのがありました。

S3 ninja

ninja.jpg

というわけで使ってみましょう。そうしましょう。
Fake S3はまた今度。

テストコードを書いてみる

通常はダウンロードして使うようですが、面倒なので、今回はmavenでインストールして、Junitで無理やり起動させます。

pom.xml

pom.xml
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.91</version>
        </dependency>
        <dependency>
            <groupId>com.scireum</groupId>
            <artifactId>s3ninja</artifactId>
            <version>2.7</version>
        </dependency>

JUnitコード

Test.java
    static {
        //必要なフォルダを掘る
        try {
            Files.createDirectories(Paths.get("data/s3"));
        } catch (IOException e) {
        }

        //s3ninja起動
        Thread thread = new Thread(() -> {
            try {
                Method main = Class.forName("IPL").getMethod("main", new Class[] { String[].class });
                main.invoke(null, (Object) new String[0]);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        thread.start();

        try {
            Thread.sleep(1000);//念のため
        } catch (InterruptedException e) {
        }

    }

    @Test
    public void test() throws IOException {
        AmazonS3 s3 = AmazonS3Client.builder()
                .withEndpointConfiguration(new EndpointConfiguration("http://localhost:9444/s3", null))
                .withCredentials(new AWSStaticCredentialsProvider(
                        new BasicAWSCredentials("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")))
                .withPathStyleAccessEnabled(true)
                .withClientConfiguration(new ClientConfiguration().withSignerOverride("S3SignerType"))
                .build();

        //アップロード
        s3.putObject(new PutObjectRequest("bucket", "s3test.txt", new File("test.txt")));

        //ダウンロード
        try (S3Object s3Object = s3.getObject(new GetObjectRequest("bucket", "s3test.txt"));
                InputStream input = s3Object.getObjectContent()) {
            assertThat(IOUtils.toByteArray(input), is(Files.readAllBytes(Paths.get("test.txt"))));
        }

    }

説明

  • 初期処理
    • 利用するには./data/s3というフォルダが必要らしいので掘ります。
    • S3 ninjaを起動するために、IPL#mainを呼びたいのですが、デフォルトパッケージのクラスはimport出来ないのでreflectionで呼び出します。
  • AmazonS3Client生成
    • endpoint設定します。
    • Credentialsに必要な値はソースコードを参考にしてください。S3 ninjaを普通に起動してweb画面開いたら書いてあります。
    • PathStyleAccessEnabledをtrueにしないとダメみたいです。
    • "S3SignerType"というのを設定しないとダメみたいです。
  • テスト
    • ファイルをアップロードして、それをダウンロードして元ファイルと比べてます。

参考:
ローカルS3なるninjaを入れてみた

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2