5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SDK for Java を使って S3 に接続するごく簡単なサンプル

Last updated at Posted at 2015-02-18

シンプルなサンプルなので、Amazon Web Service (AWS) に興味のある人なら説明の必要もないでしょうね。

S3 に接続して Bucket一覧を表示するだけのサンプルです。

詳しい説明はこちら http://bonk.red/articles/AWS/aws_sdk_java.html

S3Conn.java
/*
 *  Connect to S3 and list bucket names.
 *    Environ variables "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION" must be defined.
 *       Visit http://bonk.red for details
 */
//import java.util.HashSet;
import java.util.List;
//import java.util.Set;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class S3Conn {
  public static AmazonS3 s3 = null;

  public static void main(String args[]) {
    AWSCredentials credentials = null;
    System.out.println("S3Conn main started.");
    try {
      credentials = new ProfileCredentialsProvider().getCredentials();
      s3  = new AmazonS3Client(credentials);
      List<Bucket> buckets = s3.listBuckets();
      for (int i = 0; i < buckets.size(); i++) {
        System.out.println(buckets.get(i).getName());
      }
    }
    catch (Exception e) {
      System.out.println(e.getMessage());
    }
    System.out.println("Done.");
  }
}
5
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?