簡體   English   中英

使用aws-sdk gem列出s3中的所有文件

[英]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']

顯然這些是虛構的,因為我無法訪問您的存儲桶,但請查看ObjectCollectionTree以獲取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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM