刚上市的子文件夹中的S3存储桶 [英] Listing just the sub folders in an s3 bucket

查看:218
本文介绍了刚上市的子文件夹中的S3存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个S3结构如下:

I have an s3 structure as follows:

s3bucketname -> List of first level keys -> List of second level keys -> List of third level keys -> Actual file.

我需要做的是,鉴于s3bucket的名称和第一级key的条目,我需要所有的驻留在第一级别项第二级按键的名称。所以基本上,如果我们看它像一个文件夹中,我给出的根文件夹这是 s3bucketname 的名称和它的一个子的名字 subfolder1 ,我想列出所有驻留在 subfolder1 的文件夹。就在名字虽然不是完整的路径。

What I need to do is that given the name of the s3bucket and an entry for the first level key, I need the names of all the second level keys that reside under the first level keys. So essentially if we look at it like a folder, I am given the name of the root folder which is the s3bucketname and the name of one of its subfolders subfolder1, I would like to list all the folders that reside within subfolder1. Just the names though, not the complete path.

有人能指出如何使用Amazon的Java SDK做java开发?

Can somebody point out how to do it in java using amazon's java sdk?

感谢

推荐答案

我做了以下code这似乎是做工精细,你必须通过一个 preFIX ,并确保preFIX结尾的 / ,并指定您希望得到您的子目录列表中的分隔符。下面应该工作:

I did the following code which seems to work fine, you have to pass a prefix and make sure the prefix ends with /, and also specify the delimiter you want to get your list of sub-directories. The following should work:

public List<String> listKeysInDirectory(String bucketName, String prefix) {
    String delimiter = "/";
    if (!prefix.endsWith(delimiter)) {
        prefix += delimiter;
    }

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
            .withBucketName(bucketName).withPrefix(prefix)
            .withDelimiter(delimiter);
    ObjectListing objects = _client.listObjects(listObjectsRequest);
    return objects.getCommonPrefixes();
}

这篇关于刚上市的子文件夹中的S3存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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