问题描述
在其SDK版本1中,Amazon提供了一些非常有用的方法,可用于使用Tree,ChildCollection,LeafNode,BranchNode等来探索存储桶的内容.不幸的是,我很难用以下方法来复制其功能: SDK的第2版,其中似乎未包含此类方法.理想情况下,我想做类似于以下示例的操作,该示例取自 v1 SDK .
In version 1 of their SDK, Amazon provided some really useful methods that could be used to explore the contents of buckets using Tree, ChildCollection, LeafNode, BranchNode, etc. Unfortunately, I've had a difficulty time replicating their functionality with version 2 of the SDK, which doesn't seem to include such methods. Ideally, I'd like to do something similar to the example below, which is taken from the v1 SDK.
tree = bucket.as_tree
directories = tree.children.select(&:branch?).collect(&:prefix)
#=> ['photos', 'videos']
files = tree.children.select(&:leaf?).collect(&:key)
#=> ['README.txt']
关于如何实现这一目标的任何想法?
Any ideas on how one might achieve this?
推荐答案
树和分支功能通过列出带有前缀和定界符的存储桶中的对象来工作.前缀指定当前的文件夹",定界符应为'/',以防止返回嵌套键.
The tree and branch functionality work by listing objects in a bucket with a prefix and delimiter.The prefix specifies the current "folder" and the delimiter should be a '/' to prevent nested keys from being returned.
例如,要列出存储桶的"photos/family/"文件夹中的所有文件"和文件夹":
For example, to list all of the "files" and "folders" inside the "photos/family/" folder of a bucket:
s3 = Aws::S3::Client.new
resp = s3.list_objects(bucket:'bucket-name', prefix:'photos/family/', delimiter:'/')
# the list of "files"
resp.contents.map(&:key)
#=> ['photos/family/summer_vacation.jpg', 'photos/family/parents.jpg']
# the list of "folders"
resp.common_prefixes
#=> ['photos/family/portraits/', 'photos/family/disney_land/']
内容是响应中的文件或叶节点. common_prefixes是目录.如果要继续查看"photos/family/portraits/"中的文件和文件夹,请再次#list_objects
使用不同的前缀:
The contents are the files, or leaf nodes in a response. The common_prefixes are the directories. If you want to continue down to see the files and folder inside "photos/family/portraits/", then just #list_objects
again with a different prefix:
resp = s3.list_objects(bucket:'bucket-name', prefix:'photos/family/portraits/', delimiter:'/')
这篇关于Amazon AWS:如何从AWS Ruby SDK v2中的AWS Ruby SDK v1复制树/分支功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!