AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("Region name").build();
//Get the list of files under the bucket on S3
ObjectListing objListing = s3Client.listObjects("Bucket name");
List<S3ObjectSummary> objList = objListing.getObjectSummaries();
try {
//Process files on S3
for (S3ObjectSummary obj : objList) {
//Since objList contains all folders and file information under the bucket, it is necessary to narrow down by the target path.
//Do not download if the target path is not included or the size is 0
if (!StringUtils.contains(obj.getKey(), "Target path") || obj.getSize() == 0) {
continue;
}
//Download process below
// obj.getKey()Is"Target path/file name"Has become
GetObjectRequest request = new GetObjectRequest("Bucket name", obj.getKey());
//Only the file name
String fileName = obj.getKey().replace("Target path", "");
//Generate download destination file
File file = new File(fileName);
if (s3Client.getObject(request, file) == null) {
//Download failure
}
}
} catch (IOException e) {
throw e;
}
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("Region name").build();
File file = new File("File name to be uploaded");
PutObjectRequest request = new PutObjectRequest("Bucket name", "Target path" + file.getName(), file);
request.setCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(request);
} catch (Exception e) {
throw e;
}
Recommended Posts