我在Slashdot和其他地方发现了很多文档,声称S3没有桶中子目录的概念.这基本上是正确的.当您要删除文件时,您会发现必须先使用以下方法找到它们:
//assume we are looking for all files in 'stuff' directory files = s3.listObjects(bucket,'stuff/',null)
现在,如果你删除这些文件,你仍然会留下一些看起来非常像桶中的子目录的东西.你仍会看到’stuff /’列出.所以这让我怀疑是否真的没有子目录.事实证明,确实没有真正的子目录,但有些文件伪装成子目录并显示在列表中.通过一点点洞察我确定这是另一个具有密钥名称的S3对象,其中特殊字符串_ $folder $附加到密钥.所以你可以通过执行以下操作来删除它(假设上面的例子):
s3.deleteObject(bucket,'stuff_$folder$')
现在,您将不再看到该存储桶中的内容列出的任何子目录.虽然我没有对此进行测试,但我认为在尝试删除密钥’stuff_ $folder $’之前,stuff /文件夹必须已经为空.让我感到惊讶的是,在这里的所有帖子中都没有提到过,所以任何试图删除整个子目录的人都可能有子目录本身仍然存在!
如果你回到我原来的listObjects调用并改为:
files = s3.listObjects(bucket,'stuff',null) //note,no trailing slash
您将在结果中看到stuff_ $folder $.我的问题是你也可能得到以“东西”开头但不包含在“子目录”中的其他对象.所以你必须要小心.所以我的偏好是将’stuff /’作为键,然后分别处理’stuff_ $folder_’对象.
这引出了一个最后的问题.我似乎无法清楚地解释listObjects(bucket,key,delimiter)调用中的最终参数意味着什么.什么是“分隔符”.它似乎不是指“文件分隔符”(如’/’).我搜索过,似乎无法找到一个例子来说明这意味着什么或如何使用它.我想知道,因为无论如何还有提高listObjects的实用性和灵活性,我想知道.有人可以举例说明分隔符参数的用法和含义吗?我确信它很简单,我找不到一个很好的例子.
解决方法
Groups of keys that share a common prefix terminated by a special
delimiter can now be rolled-up by that prefix for the purposes of
listing. This allows applications to browse their keys hierarchically,
much like how you would navigate through directories in a filesystem.For example,if you had a bucket that contained the following keys
(named with embedded slash delimiters to simulate directories)
photos/2006/index.html photos/2006/January/img0001.jpg …
photos/2006/January/img0999.jpg photos/2006/February/img1000.jpg … A
list query with Prefix=”photos/2006/” and Delimiter=”/” would return
the keys and “subdirectories” at the photos/2006 level (index.html,
January,February,…) but would not include any of the .jpg keys at
deeper levels.
所以想想它就足够了.你的分隔符可以是.html,.jpg或类似的东西.