![](/img/trans.png)
[英]How to list all files in an s3 folder using AWS-SDK gem in ruby on rails
[英]list all the files from s3 using aws-sdk gem
我有以下情况。 在我的情况下考虑aws s3文件夹结构如下
- videos
- my_videos
- college
我已经在college
上传了视频文件myfirst_day.mp4
,因为这个相关形成的关键是"videos/my_videos/college/myfirst_day.mp4"
现在我必须列出videos/my_videos/college
目录中的所有文件。 我该怎么做。
为此,我使用aws-sdk gem
您可以简单地迭代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
,它将列出LeafNode
(文件)和BranchNode
(目录)。 然后BranchNode
也会响应children
BranchNode
,这样你就可以根据需要进行递归。
要获得suffix
(例如只是文件名),您可以修补这些内容。
class LeafNode
def suffix
@member.key.split(delimiter).pop
end
end
class S3Object
def suffix
@key.split("/").pop
end
end
我没有以任何方式完全测试这些,但如果它嵌套在分支内,它们应该只返回文件名本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.