AWS S3 - 仅列出根级别对象

时间:2016-11-25 10:14:12

标签: java amazon-web-services amazon-s3

我有一个具有以下文件夹结构的S3存储桶:

<bucket-name>
    folder1/
        ....
    folder2/
        ....
    foldern/
        ....

其中每个文件夹都包含文件,我只希望列出顶级文件夹folder1, folder2 etc.

我发现很多解决方案建议我使用"/"作为分隔符和空前缀,这正是我在下面的Java代码片段中尝试做的事情:

ListObjectsRequest listObjects = new ListObjectsRequest()
            .withDelimiter("/")
            .withPrefix("")
            .withBucketName(s3BucketName);
    ObjectListing objects = s3.listObjects(listObjects);
    for (S3ObjectSummary summ : objects.getObjectSummaries()) {
        System.out.println(summ.getKey());
    }

for循环仍然不打印任何键。有没有我做错了,或者这样做不是这样吗?

3 个答案:

答案 0 :(得分:1)

我设法列出了以下(在Java中)的顶级文件夹:

ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                                       .withBucketName(bucketName)
                                       .withDelimiter("/");
ObjectListing objectListing = client.listObjects(listObjectsRequest);
List<String> commonPrefixes = objectListing.getCommonPrefixes();

您只需设置分隔符并从公共前缀中读取。

我目前使用的SDK版本是1.10.14

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.10.14</version>
    </dependency>

答案 1 :(得分:0)

这是一个c#版本。应该很容易移植:

    public List<string> GetBucketRootItems(string bucketName, string regionName)
    {
        List<string> result = new List<string>();
        try
        {
            using (var client = new AmazonS3Client(RegionEndpoint.GetBySystemName(regionName)))
            {
                ListObjectsV2Request request = new ListObjectsV2Request
                {
                    BucketName = bucketName,
                    MaxKeys = 10000,
                    Delimiter = "/",
                    Prefix = ""
                };

                ListObjectsV2Response response;
                do
                {
                    response = client.ListObjectsV2(request);
                    result.AddRange(response.CommonPrefixes);
                    request.ContinuationToken = response.NextContinuationToken;
                }
                while (response.IsTruncated == true);
            }
        }
        catch (AmazonS3Exception amazonS3Exception)
        {
            if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
            {
                log.Error($"Check the provided AWS Credentials.");
            }
            else
            {
                log.Error($"Error occurred. Message:'{amazonS3Exception.Message}' when listing objects", amazonS3Exception);
            }
        }
        return result;
    }

这似乎有效。我得到的条目不到10000条

答案 2 :(得分:0)

如果您需要列出顶级directories

public List<String> listTopLevelDirectories(AmazonS3 amazonS3, String bucketName) {

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
            .withBucketName(bucketName)
            .withDelimiter("/");
    return amazonS3.listObjects(listObjectsRequest).getCommonPrefixes();

}