简体   繁体   English

检索S3中仅根对象(文件夹)的列表-AWS SDK V3 PHP

[英]Retrieve list of only root objects (folders) in S3 - aws sdk v3 php

Given my S3 bucket that contains images in a structure like so: 给定我的S3存储桶,其中存储的图像具有如下结构:

root/portraits/portrait_001.jpg
root/landscapes/landscape_001.jpg

where root is my bucket, and there are no other files in my root, just those folders (objects), how do I retrieve a list of just those objects? root是我的存储桶,root中没有其他文件,只有那些文件夹(对象),如何检索这些对象的列表?

portraits/
landscapes/

I am familiar with using the Delimiter and Prefix in the ListObjects call. 我熟悉在ListObjects调用中使用定界符和前缀。

If I do the following, I get no results: 如果执行以下操作,则不会获得任何结果:

$objects = $s3->getIterator('ListObjects', array(
    'Bucket' => $bucket,
    'Delimiter' => '/',
));

foreach($objects as $object)
    echo $object['Key'] . "\n";

If I don't use a Delimiter, I get everything, obviously. 如果我不使用定界符,很明显,我得到了所有东西。

I cannot use a prefix because the objects I desire are root-level. 我不能使用前缀,因为我想要的对象是根级别的。 Otherwise, I have no problem using the prefix to say, list just the files in 'portraits/' 否则,使用前缀说就没问题,只列出“ portraits /”中的文件

From my searches, I've only managed to find solutions from previous years that only apply to the aws php sdk v1 or v2, and I have had no luck in trying those (v3 is quite different) 从我的搜索中,我仅设法找到了仅适用于aws php sdk v1或v2的前几年的解决方案,而尝试这些方法也没有运气(v3完全不同)

Any suggestions? 有什么建议么? I feel like I'm missing something simple, but searching through the documentation, I can't find anything to help me. 我觉得我缺少一些简单的东西,但是在搜索文档时,找不到任何可以帮助我的东西。 As a last resort, I'll just have to stick with manually declaring an array 作为最后的选择,我只需要坚持手动声明一个数组

$categories = ['portraits/', 'landscapes/']

But that isn't ideal in the case where I want to add more categories in the future, and not have to worry about adding another category manually. 但这在将来我想添加更多类别,而不必担心手动添加另一个类别的情况下并不理想。

Any help would be greatly appreciated :) 任何帮助将不胜感激 :)

Edit - Solution 编辑-解决方案

I must have been looking in the wrong places during my object dumps, but eventually saw the Common Prefixes in the returned result from a ListObjects call with a delimiter of '/', like so: 在对象转储期间,我一定一直在找错地方,但是最终在使用'/'分隔符的ListObjects调用返回的结果中看到了公共前缀,如下所示:

$s3->listObjects(array('Bucket' => $bucket, 'Delimiter' => '/'));

Directories do not actually exist in Amazon S3. 目录实际上在Amazon S3中不存在。 However, the Management Console allows the creation of folders, and paths are supported to give the illusion of directories. 但是,管理控制台允许创建文件夹,并且支持路径以使目录产生错觉。

For example, object bar.jpg stored in the foo directory has a path of /foo/bar.jpg . 例如,存储在foo目录中的对象bar.jpg的路径为/foo/bar.jpg The trick is that the object is actually called foo/bar.jpg rather than just bar.jpg . 诀窍在于,该对象实际上称为foo/bar.jpg而不仅仅是bar.jpg Most users wouldn't even notice the difference. 大多数用户甚至都不会注意到差异。

From the API, the ability to list directories is provided via the concept of CommonPrefixes , which look the same as directory paths and consist of the portion of object names ('keys') before the final slash. 从API中,通过CommonPrefixes的概念提供了列出目录的功能, CommonPrefixes外观与目录路径相同,并且由最后斜杠之前的对象名称(“键”)组成。

See: Listing Keys Hierarchically Using a Prefix and Delimiter 请参阅: 使用前缀和定界符分层列出密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM