[英]AWS S3 - List only root level objects
I have an S3 bucket with the following folder structure:我有一个具有以下文件夹结构的 S3 存储桶:
<bucket-name>
folder1/
....
folder2/
....
foldern/
....
Each of these folders has files in it, I only want the top level folders listed folder1, folder2 etc.
这些文件夹中的每一个都有文件,我只想要列出文件夹 1 folder1, folder2 etc.
文件夹 2 folder1, folder2 etc.
的顶级文件夹folder1, folder2 etc.
I have found a lot of solutions suggesting I use "/"
as a delimiter and an empty prefix, which is exactly what I'm trying to do in the Java snippet below:我发现很多解决方案建议我使用"/"
作为分隔符和一个空前缀,这正是我在下面的 Java 代码段中尝试做的:
ListObjectsRequest listObjects = new ListObjectsRequest()
.withDelimiter("/")
.withPrefix("")
.withBucketName(s3BucketName);
ObjectListing objects = s3.listObjects(listObjects);
for (S3ObjectSummary summ : objects.getObjectSummaries()) {
System.out.println(summ.getKey());
}
The for loop still prints no keys. for 循环仍然不打印任何键。 Is there something I am doing wrong, or is this not done that way at all?是我做错了什么,还是根本没有这样做?
I managed to list the top level folders with the following (in Java):我设法列出了具有以下内容的顶级文件夹(在 Java 中):
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName)
.withDelimiter("/");
ObjectListing objectListing = client.listObjects(listObjectsRequest);
List<String> commonPrefixes = objectListing.getCommonPrefixes();
You only need to set the delimiter and read from common prefixes.您只需要设置分隔符并从公共前缀中读取。
The SDK version I am currently using is 1.10.14我目前使用的SDK版本是1.10.14
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.10.14</version>
</dependency>
Here is ac# version.这是 ac# 版本。 Should be very easy to port:应该很容易移植:
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;
}
This appears to work.这似乎有效。 I got just under 10000 entries我收到了不到 10000 个条目
If you need to list the top level directories
:如果您需要列出顶级directories
:
public List<String> listTopLevelDirectories(AmazonS3 amazonS3, String bucketName) {
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName)
.withDelimiter("/");
return amazonS3.listObjects(listObjectsRequest).getCommonPrefixes();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.