Amazon S3列出“目录" [英] Amazon S3 listing "directories"
问题描述
我已经通过AWS S3管理控制台在S3中创建了一个层次结构.如果我运行以下代码以列出存储桶:
I've created a hierarchy in S3 via the AWS S3 Management Console. If I run the following code to list the bucket:
AmazonS3 s3 = new AmazonS3Client(CRED);
ListObjectsRequest lor = new ListObjectsRequest()
.withBucketName("myBucket")
.withPrefix("code/");
ObjectListing objectListing = s3.listObjects(lor);
for (S3ObjectSummary summary: objectListing.getObjectSummaries()) {
System.out.println(summary.getKey());
}
我得到:
code/
code/03000000-0001-0000-0000-000000000000/
code/03000000-0001-0000-0000-000000000000/special.js
code/03000000-0001-0000-0000-000000000000/test.js
code/03000000-0002-0000-0000-000000000000/
这正是我所期望的.如果我添加一个定界符,那么我只将内容直接列在"code/"下,那么我现在就不会得到任何子目录"了.
Which is exactly what I would expect. If I add a delimiter though, so that I only list the content directly under "code/" I now don't get any sub "directories" back.
将上面的行(末尾添加withDelimiter())更改为:
Change line above (add withDelimiter() on the end) to:
ListObjectsRequest lor = new ListObjectsRequest().withBucketName("myBucket")
.withPrefix("code/")
.withDelimiter("/");
我现在只得到:
code/
我知道S3没有目录",而是定界的键,但是这种行为看起来很奇怪吗?我如何列出仅在代码"下方的内容?
I know that S3 doesn't have "directories", instead delimited keys, but this behaviour seems odd? How would I list what is only immediately below "code"?
推荐答案
如果您的密钥中没有内容,S3会将其视为通用前缀":
Where you have keys that have no content S3 considers them "Common Prefixes":