How to enumerate the items in an AWS S3 bucket with Ruby

You can iterate through the items in an S3 bucket and print the names with this Ruby snippet. It assumes you already have the AWS Ruby SDK installed. Remember to set the details for your_access_key, your_secret_access_key and bucket_name as appropriate for your bucket.

require 'rubygems'
require 'aws-sdk'

s3 = AWS::S3.new(
  :access_key_id => 'your_access_key',
  :secret_access_key => 'your_secret_access_key')

bucket = s3.buckets['bucket_name']
puts bucket.name

bucket.objects.each do |obj|
  puts obj.key + ' ' + obj.last_modified.to_s
end

Tags: