タイトル : S3 バケットリスト AWS SDK for Java を試す 2025
更新日 : 2025-10-12
カテゴリ : プログラミング
タグ :
aws   
java   

AWS SDK for Java を試す 2025

ソース

package com.skiss.awstest;

import java.util.Iterator;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Bucket;
import software.amazon.awssdk.services.s3.paginators.ListBucketsIterable;

public class App 
{
    public static void main( String[] args )
    {
        S3Client s3 = S3Client.builder()
            .region(Region.AP_NORTHEAST_1)
            .build();

        listAllBuckets(s3);
    }

    public static void listAllBuckets(S3Client s3) {
        ListBucketsIterable response = s3.listBucketsPaginator();

        System.out.println("バケット名 : ");
        for(Iterator<Bucket>itr = response.buckets().iterator(); itr.hasNext();){
			Bucket b = itr.next();
			System.out.println("  " + b.name());
        }
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.skiss.awstest</groupId>
  <artifactId>s3_01</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>s3_01</name>
  <url>http://maven.apache.org</url>
  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencyManagement>
      <dependencies>
          <dependency>
          <groupId>software.amazon.awssdk</groupId>
          <artifactId>bom</artifactId>
          <version>2.32.9</version>
          <type>pom</type>
          <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>s3</artifactId>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.21</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2</version>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <finalName>aws_app</finalName>
            <appendAssemblyId>false</appendAssemblyId>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
              <manifest><mainClass>com.skiss.awstest.App/App</mainClass></manifest>
            </archive>
          </configuration>
        </plugin>
    </plugins>
</build>
</project>

実行・確認

$ mvn compile
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< com.skiss.awstest:s3_01 >-----------------------
[INFO] Building s3_01 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ s3_01 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/xxx/work/AWS/LocalStack/SDKforJava/Try2/s3_01/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ s3_01 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.271 s
[INFO] Finished at: 2025-10-12T07:21:29+09:00
[INFO] ------------------------------------------------------------------------
$

$ mvn package
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< com.skiss.awstest:s3_01 >-----------------------
[INFO] Building s3_01 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ s3_01 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/xxx/work/AWS/LocalStack/SDKforJava/Try2/s3_01/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ s3_01 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ s3_01 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/xxx/work/AWS/LocalStack/SDKforJava/Try2/s3_01/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ s3_01 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ s3_01 ---
[INFO] Surefire report directory: /home/xxx/work/AWS/LocalStack/SDKforJava/Try2/s3_01/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.skiss.awstest.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ s3_01 ---
[INFO] Building jar: /home/xxx/work/AWS/LocalStack/SDKforJava/Try2/s3_01/target/s3_01-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-assembly-plugin:2.2:single (make-assembly) @ s3_01 ---
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] software/ already added, skipping
[INFO] software/amazon/ already added, skipping
[INFO] software/amazon/awssdk/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/maven/software.amazon.awssdk/ already added, skipping
(省略)
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/maven/org.slf4j/ already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/maven/ already added, skipping
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: /home/xxx/work/AWS/LocalStack/SDKforJava/Try2/s3_01/target/aws_app.jar, it will become the file for main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: /home/xxx/work/AWS/LocalStack/SDKforJava/Try2/s3_01/target/s3_01-1.0-SNAPSHOT.jar
with assembly file: /home/xxx/work/AWS/LocalStack/SDKforJava/Try2/s3_01/target/aws_app.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.278 s
[INFO] Finished at: 2025-10-12T07:22:09+09:00
[INFO] ------------------------------------------------------------------------
$ java -classpath target/aws_app.jar com.skiss.awstest.App
バケット名:
  sample-bucket-1
  sample-bucket-2
$

$ aws s3 ls
2025-10-12 02:41:26 sample-bucket-1
2025-10-12 02:41:37 sample-bucket-2
$