I have a AWS S3 with the following structure or hierarchy:
我有一个具有以下结构或层次结构的AWS S3:
-
customer/name/firstname/123.gz
客户/名称/姓名/ 123.gz
-
customer/name/firstname/456.gz
客户/名称/姓名/ 456.gz
-
customer/name/firstname/789.gz
客户/名称/姓名/ 789.gz
I need to get count of all the gz files in customer/name/firstname using java sdk.
我需要使用java sdk来计算customer / name / firstname中的所有gz文件。
Can I please have a Java code on how to do it?
我可以请一个关于如何做的Java代码吗?
1 个解决方案
#1
1
There are several ways to get list of files from S3
. This is one of them:
有几种方法可以从S3获取文件列表。这是其中之一:
/**
* @bucketName bucket name (i.e. customer)
* @path path within given bucket (i.e. name/firstname)
* @pattern pattern that matches required files (i.e. "\\w+\\.gz")
*/
private List<String> getFileList(String bucketName, String path, Pattern pattern) throws AmazonS3Exception {
ListObjectsV2Request request = createRequest(bucketName, path);
return s3.listObjectsV2(request).getObjectSummaries().stream()
.map(file -> FilenameUtils.getName(file.getKey()))
.filter(fileName -> pattern.matcher(fileName).matches())
.sorted()
.collect(Collectors.toList());
}
private static ListObjectsV2Request createRequest(String bucketName, String path) {
ListObjectsV2Request request = new ListObjectsV2Request();
request.setPrefix(path);
request.withBucketName(bucketName);
return request;
}
P.S. I suppose that you already have S3
credentials in your home directory and successfully initialized AmazonS3 s3
instance.
附:我想您已在主目录中拥有S3凭据并成功初始化了AmazonS3 s3实例。
#1
1
There are several ways to get list of files from S3
. This is one of them:
有几种方法可以从S3获取文件列表。这是其中之一:
/**
* @bucketName bucket name (i.e. customer)
* @path path within given bucket (i.e. name/firstname)
* @pattern pattern that matches required files (i.e. "\\w+\\.gz")
*/
private List<String> getFileList(String bucketName, String path, Pattern pattern) throws AmazonS3Exception {
ListObjectsV2Request request = createRequest(bucketName, path);
return s3.listObjectsV2(request).getObjectSummaries().stream()
.map(file -> FilenameUtils.getName(file.getKey()))
.filter(fileName -> pattern.matcher(fileName).matches())
.sorted()
.collect(Collectors.toList());
}
private static ListObjectsV2Request createRequest(String bucketName, String path) {
ListObjectsV2Request request = new ListObjectsV2Request();
request.setPrefix(path);
request.withBucketName(bucketName);
return request;
}
P.S. I suppose that you already have S3
credentials in your home directory and successfully initialized AmazonS3 s3
instance.
附:我想您已在主目录中拥有S3凭据并成功初始化了AmazonS3 s3实例。