Amazon S3列出“目录" [英] Amazon S3 listing "directories"

查看:622
本文介绍了Amazon S3列出“目录"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过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":

公共列表getCommonPrefixes()

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.

每个公共前缀代表S3存储桶中具有以下特征的一组键: 被压缩并从对象摘要结果中省略.这 允许应用程序分层组织和浏览其密钥, 类似于文件系统将文件组织到目录中的方式.

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"

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

如果调用带有前缀="foo/"和定界符="/"的listObjects 此存储桶,返回的S3ObjectListing将在其中包含一个条目 通用前缀列表("foo/bar/"),并且所有键都不以开头 具有该公共前缀的对象将包含在对象摘要列表中.

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.

这篇关于Amazon S3列出“目录"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆