我有以下情况。 在我的情况下考虑 aws s3文件夹结构如下
- videos
- my_videos
- college
我已在myfirst_day.mp4
上传了视频文件college
,因为此相关的已形成密钥为"videos/my_videos/college/myfirst_day.mp4"
现在我必须列出videos/my_videos/college
目录中的所有文件。
我怎么能这样做。
为此我使用 aws-sdk gem
答案 0 :(得分:5)
您可以简单地迭代bucket
objects
并使用with_prefix
方法
s3.buckets[YOUR BUCKET NAME].objects.with_prefix('videos/my_videos/college').each.collect(&:key)
#=> ["videos/my_videos/college/myfirst_day.mp4"]
或使用as_tree
方法
s3.buckets[YOUR BUCKET NAME].as_tree(prefix:'videos/my_videos/college').select(&:leaf?).collect(&:key)
#=> ['videos/my_videos/college/myfirst_day.mp4']
显然这些都是虚构的,因为我无法访问您的广告素材,但请查看ObjectCollection
和Tree
以了解AWSSDK中的更多方法。
有很多方法可用于存储桶遍历,例如Tree
响应children
,它将列出LeafNode
s(文件)和BranchNode
(目录) 。然后,BranchNode
也会回复children
,以便您可以根据需要进行递归。
要获取suffix
(例如只是文件名),您可以将其修补。
class LeafNode
def suffix
@member.key.split(delimiter).pop
end
end
class S3Object
def suffix
@key.split("/").pop
end
end
我没有以任何方式对这些进行全面测试,但如果它嵌套在分支内,它们应该只返回文件名本身。