32
38

More than 5 years have passed since last update.

S3にjavaでアップロードとダウンロードメモ

Posted at

はじめに

javaからS3へのアップロード、ダウンロードのメモです。
proxy環境の場合は、proxy設定しないとタイムアウトになります。
それ以外は公式のサンプルで出来ました。

詳細は調査して、追記していきたいと思います。

jarの導入(maven)

pom.xml
    <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.112</version>
    </dependency>

定数の宣言

java
    static final String S3_ACCESS_KEY           = "";
    static final String S3_SECRET_KEY           = "";
    static final String S3_SERVICE_END_POINT    = "";
    static final String S3_REGION               = "";
    static final String S3_BUCKET_NAME          = "";

認証処理

java
    // 認証処理
    private static AmazonS3 auth(){
        System.out.println("auth start");

        // AWSの認証情報
        AWSCredentials credentials = new BasicAWSCredentials(S3_ACCESS_KEY, S3_SECRET_KEY);

        // クライアント設定
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProxyHost("[proxyHost]");
        clientConfig.setProxyPort([portNo]);

        // エンドポイント設定
        EndpointConfiguration endpointConfiguration = new EndpointConfiguration(S3_SERVICE_END_POINT,  S3_REGION);

        // S3アクセスクライアントの生成
        AmazonS3 client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withClientConfiguration(clientConfig)
                .withEndpointConfiguration(endpointConfiguration).build();

        System.out.println("auth end");
        return client;
    }

アップロード処理

java
    // アップロード処理
    private static void upload() throws Exception{
        System.out.println("upload start");

        // 認証処理
        AmazonS3 client = auth();

        File file = new File("[アップロードファイルパス]");
        FileInputStream fis = new FileInputStream(file);


        ObjectMetadata om = new ObjectMetadata();
        om.setContentLength(file.length());

        final PutObjectRequest putRequest = new PutObjectRequest(S3_BUCKET_NAME, file.getName(), fis, om);

        // 権限の設定
        putRequest.setCannedAcl(CannedAccessControlList.PublicRead);

        // アップロード
        client.putObject(putRequest);

        fis.close();

        System.out.println("upload end");
    }

ダウンロード処理

java
    // ダウンロード処理
    private static void download() throws Exception{
        System.out.println("download start");

        // 認証処理
        AmazonS3 client = auth();

        final GetObjectRequest getRequest = new GetObjectRequest(S3_BUCKET_NAME, "[ダウンロードファイル名");

        S3Object object = client.getObject(getRequest);

        FileOutputStream fos = new FileOutputStream(new File("[出力先パス]"));
        IOUtils.copy(object.getObjectContent(), fos);

        fos.close();

        System.out.println("download end");
    }
32
38
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
32
38