import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.zip.*;
public class S3ZipCsvProcessor {
private static final String BUCKET_NAME = "your-bucket-name";
private static final String PREFIX = "your-folder-path/"; // 末尾 / 必須
private static final String TARGET_ZIP_NAME = "target.zip";
private static final String TARGET_CSV_NAME = "result.csv";
public static void main(String[] args) throws IOException {
Region region = Region.AP_NORTHEAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.credentialsProvider(ProfileCredentialsProvider.create())
.build();
// ① フォルダ一覧取得
List<String> folders = listFolders(s3, BUCKET_NAME, PREFIX);
if (folders.isEmpty()) {
throw new RuntimeException("フォルダが存在しません");
}
// ② 最大番号のフォルダを特定
int max = folders.stream()
.map(f -> f.replace(PREFIX, "").replace("/", ""))
.filter(f -> f.matches("\\d+"))
.mapToInt(Integer::parseInt)
.max()
.orElseThrow();
String maxFolderPrefix = PREFIX + max + "/";
// ③ ZIPファイルの存在確認・取得
String zipKey = maxFolderPrefix + TARGET_ZIP_NAME;
Path zipPath = Paths.get("temp.zip");
s3.getObject(GetObjectRequest.builder().bucket(BUCKET_NAME).key(zipKey).build(),
ResponseTransformer.toFile(zipPath));
// ④ 解凍
Path unzipDir = Paths.get("unzipped");
unzip(zipPath, unzipDir);
// ⑤ 特定CSV取得
Path csvPath = unzipDir.resolve(TARGET_CSV_NAME);
if (!Files.exists(csvPath)) {
throw new RuntimeException("CSVが見つかりません: " + TARGET_CSV_NAME);
}
System.out.println("CSVファイル取得成功: " + csvPath.toAbsolutePath());
// 後処理(任意)
// Files.delete(zipPath);
}
private static List<String> listFolders(S3Client s3, String bucket, String prefix) {
ListObjectsV2Request request = ListObjectsV2Request.builder()
.bucket(bucket)
.prefix(prefix)
.delimiter("/") // フォルダ区切り
.build();
ListObjectsV2Response response = s3.listObjectsV2(request);
return response.commonPrefixes().stream()
.map(CommonPrefix::prefix)
.toList();
}
private static void unzip(Path zipFilePath, Path targetDir) throws IOException {
Files.createDirectories(targetDir);
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath.toFile()))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
Path outPath = targetDir.resolve(entry.getName());
if (entry.isDirectory()) {
Files.createDirectories(outPath);
} else {
Files.createDirectories(outPath.getParent());
try (OutputStream os = Files.newOutputStream(outPath)) {
zis.transferTo(os);
}
}
}
}
}
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme