gpt4 book ai didi

java - 亚马逊 S3 list "directories"

转载 作者:IT老高 更新时间:2023-10-28 20:34:32 25 4
gpt4 key购买 nike

我通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com