Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitsaxena/9951575 to your computer and use it in GitHub Desktop.
Save amitsaxena/9951575 to your computer and use it in GitHub Desktop.
Get file size of all the files at a given path on Amazon S3 - recursively, and dump in a CSV
# Using aws-sdk gem
@access_key = "access_key"
@secret_key = "secret_access_key"
@bucket = "my_bucket"
AWS.config(
:access_key_id => @access_key,
:secret_access_key => @secret_key
)
@s3 = AWS::S3.new
@path = "path/to/folder"
tree = @s3.buckets[@bucket].as_tree(:prefix => @path)
directories = tree.children.select(&:branch?).collect(&:prefix)
files = tree.children.select(&:leaf?).collect(&:key)
while directories.size > 0
dir = directories.pop
files = files + @s3.buckets[@bucket].as_tree(:prefix => dir).children.select(&:leaf?).collect(&:key)
directories = directories + @s3.buckets[@bucket].as_tree(:prefix => dir).children.select(&:branch?).collect(&:prefix)
end
puts "Total Files: #{files.count}"
f=File.open("/tmp/s3_sizes.csv", "w")
files.each do |file|
size = @s3.buckets[@bucket].objects[file].content_length
f.puts size
end
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment