本文介绍了扣除preFIX在ObjectListing导致Java客户端的S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个S3桶以下层次:

I have a S3 bucket with following hierarchy:

bucketName
    folder1
       file1

我想获得从folder1中的所有文件。我试着做以下操作:

I wanted to get all the files from folder1. I tried to do following:

ObjectListing ol = s3Client.listObjects("bucketName", "folder1");
List<S3ObjectSummary> summaries = ol.getObjectSummaries();

问题是,摘要中包含文件夹1 / 文件夹1 /文件1 。凡为我希望能得到公正文件夹1 /文件1

The problem is that summaries contains folder1/ and folder1/file1. Where as I was hoping to get just folder1/file1.

环顾互联网,我也尝试以下操作:

Looking around at the internet, I also tried following:

ListObjectsRequest req = new ListObjectsRequest().withBucketBucketName("bucketName").withPrefix("folder1/").withDelimiter("/");

但是,这一次我没有结果回来 getObjectSummaries 通话。当我删除 withDelimiter 从上面我同时获得文件夹1 \ 文件夹1 \文件1 回来。

But this time I got no results back for getObjectSummaries call. When I remove withDelimiter from above I get both folder1\ and folder1\file1 back.

有没有办法只得到文件夹1 \文件1 回来?

Is there any way to just get folder1\file1 back?

请让我知道。谢谢!

推荐答案

使用以preFIX withMarker 在一起:

ListObjectsRequest req = new ListObjectsRequest().withBucketName("bucketName").withPrefix("folder1/").withMarker("folder1/");

这工作,因为首先你过滤以preFIX 并获得所有文件夹1 / *键,包括文件夹1 /,然后用 withMarker( 文件夹1 /)指定要获取的字典顺序后,按键文件夹1 /作为记录在的:

This works because first you filter withPrefix and obtain all folder1/* keys, including folder1/, and then with withMarker("folder1/") you specify to get the keys that are lexicographically after "folder1/" as documented in the javadoc:

该列表将只包括标记之后发生的字典序键。

此外,如果文件夹1包含其他的子文件夹,你只能得到直接孩子,使用 withDelimiter

Furthermore, if folder1 contains other subfolders, you can get the direct children only, using withDelimiter:

ListObjectsRequest req = new ListObjectsRequest().withBucketName("bucketName").withPrefix("folder1/").withMarker("folder1/").withDelimiter("/");

这工作,因为分隔符/使所有子文件夹被卷起文件夹1 /但是你忽略了这个结果与标志。 javadoc中说,对 withDelimiter

This works because the delimiter "/" makes all subfolders to be rolled up to "folder1/" but you ignore this result with the marker. The javadoc says for withDelimiter:

获取可选定界符参数导致包含之间的preFIX和分隔符的第一次出现相同的字符串被组合成一个单一的结果元素(...)键。最常用的分隔符是/,它模拟类似于文件系统目录结构分层组织

-

在任何情况下,文件夹1 / 只列出来,因为你一定通过Web控制台创建它。如果你不直接创建文件夹,但把对象编程,如把FOLDER2 /文件2 文件夹将不被实际创建为一个独立的对象,因此它不能上市。

In any case, folder1/ is only listed because you surely created it through the web console. If you don't create folders directly but put objects programmatically, like put folder2/file2 the folder won't be actually created as an independent object and so it cannot be listed.

这篇关于扣除preFIX在ObjectListing导致Java客户端的S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 21:27