0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

S3からファイル取得

Posted at

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);
}
}
}
}
}
}


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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?