java - 亚马逊 S3 list "directories"

标签 java amazon-web-services amazon-s3

我通过 AWS S3 管理控制台在 S3 中创建了一个层次结构。如果我运行以下代码来列出存储桶:

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/ 

这正是我所期望的。如果我添加一个分隔符,那么我只在“代码/”下直接列出内容,我现在不会得到任何子“目录”。

将上面的行(在末尾添加 withDelimiter())更改为:

ListObjectsRequest lor = new ListObjectsRequest().withBucketName("myBucket")
                                                 .withPrefix("code/")
                                                 .withDelimiter("/");

而我现在只得到:

code/ 

我知道 S3 没有“目录”,而是分隔键,但这种行为似乎很奇怪?我如何列出仅在“代码”下方的内容?

最佳答案

如果您有没有内容的键,S3 将它们视为“通用前缀”:

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/ObjectListing.html#getCommonPrefixes%28%29

public List getCommonPrefixes()

Gets the common prefixes included in this object listing. Common prefixes are only present if a delimiter was specified in the original request.

Each common prefix represents a set of keys in the S3 bucket that have been condensed and omitted from the object summary results. This allows applications to organize and browse their keys hierarchically, similar to how a file system organizes files into directories.

For example, consider a bucket that contains the following keys:

"foo/bar/baz"
"foo/bar/bash"
"foo/bar/bang"
"foo/boo"

If calling listObjects with the prefix="foo/" and the delimiter="/" on this bucket, the returned S3ObjectListing will contain one entry in the common prefixes list ("foo/bar/") and none of the keys beginning with that common prefix will be included in the object summaries list.

Returns: The list of common prefixes included in this object listing, which might be an empty list if no common prefixes were found.

关于java - 亚马逊 S3 list "directories",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17561432/

相关文章:

Java this 和泛型?

java - 亚马逊 AWS Signature V4 Java 给出错误的编码

java - Kony FFI 整合问题

upload - Dropbox 如何将数据上传到其服务器?

ruby-on-rails - 配置环境以在本地和Heroku上的Amazon上使用文件系统

java - 从十六进制转换为二进制,反之亦然。艰难的道路

java - 无法使用 Selenium 获取 WebElement 标签名称

java - 数组未定位右行 [Java]

MongoDB Compass 连接问题与 Ec2 实例访问亚马逊 documentDB 集群

java - AWS ResetException - 无法重置请求输入流