LoginSignup
2
1

More than 1 year has passed since last update.

Minioを用いてAWSアカウントなしでS3接続(javaSDK)を確認する方法

Posted at

概要

この記事では、AWSのアカウントなしでもAWS S3 接続を確認できる方法を記事にしてみました。
Minio自体は、AWS S3 と互換性があるマルチクラウドオブジェクトストレージのOSSとして知られています。
現場の業務上 S3 に対してJavaを用いて接続する必要があり自宅で無料で接続確認がしたくてminioを採用しました。
使用方法をwebで調べたのですが、出てくるのはdockerを用いて簡単に構築することができることやMinioが提供しているライブラリを用いて接続する方法しかなく、AWS SDKで接続する方法が見つからなかったので今回挑戦し成功したので記事にしてみました。

minioって何?っていう方は以下の記事や公式のサイトを見ると理解できると思います。
https://min.io/
https://dev.classmethod.jp/articles/s3-compatible-storage-minio/

aws s3って何?という方は以下の記事や公式のサイトを見ると理解できると思います。
https://aws.amazon.com/jp/s3/
https://business.ntt-east.co.jp/content/cloudsolution/column-try-43.html

必要なもの

dockerDesktop
eclipse
aws SDK jarファイル

方法

以下の記事より、dockerDesktopよりminioの環境を構築する
https://dev.classmethod.jp/articles/s3-compatible-storage-minio/

以下のサイトを参考にソースコードを作成しました。
https://docs.min.io/docs/how-to-use-aws-sdk-for-java-with-minio-server.html

package minioS3;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;

public class Main {

    private static String bucketName = "testbucket";
    private static String keyName = "hosts";
//指定したい自身のパソコン内のパスを記入する 今回はホストファイルを指定して簡易動作確認します。
    private static String uploadFileName = "/etc/hosts";

    public static void main(String[] args) throws IOException {

//YOUR-ACCESSKEYID YOUR-SECRETACCESSKEYにminioに指定した値を入れる
        AWSCredentials credentials = new BasicAWSCredentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY");
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setSignerOverride("AWSS3V4SignerType");

//EndpointにMinioで指定した値を入れる リージョンの値はそのままで問題ないです。
        AmazonS3 s3Client = AmazonS3ClientBuilder
                .standard()
                .withEndpointConfiguration(
                        new AwsClientBuilder.EndpointConfiguration("http://localhost:9000", Regions.US_EAST_1.name()))
                .withPathStyleAccessEnabled(true)
                .withClientConfiguration(clientConfiguration)
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .build();

        try {
            System.out.println("Uploading a new object to S3 from a file\n");
            File file = new File(uploadFileName);
            // Upload file
            s3Client.putObject(new PutObjectRequest(bucketName, keyName, file));

            // Download file
            GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, keyName);
            S3Object objectPortion = s3Client.getObject(rangeObjectRequest);
            System.out.println("Printing bytes retrieved:");
            displayTextInputStream(objectPortion.getObjectContent());
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
                    + "to Amazon S3, but was rejected with an error response" + " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());

        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to "
                    + "communicate with S3, " + "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());

        }
    }

    private static void displayTextInputStream(InputStream input) throws IOException {
        // Read one text line at a time and display.
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while (true) {
            String line = reader.readLine();
            if (line == null)
                break;

            System.out.println("    " + line);
        }
        System.out.println();
    }

}

まとめ

awsは素晴らしいサービスですが、一歩間違うと大金を支払う可能性が出てくるので使うのが怖いですよね。
minioは他にも工夫すれば、スクレイピングの情報を溜めておくのに使えたりサイトのページとして使えるので応用を効かせると
夢が広がります。

今回、人生で初めてQiita投稿しました。
今後も、現場での知識や気になった技術などの投稿をしていきたいと思います。

2
1
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
2
1