Skip to content

Instantly share code, notes, and snippets.

@rubyconvict
Created December 12, 2015 18:51
Show Gist options
  • Save rubyconvict/7ab617b805a5ed2d0e1d to your computer and use it in GitHub Desktop.
Save rubyconvict/7ab617b805a5ed2d0e1d to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'dotenv'
Dotenv.load
require 'aws-sdk'
# USAGE:
# Put credentials in .env file, wrapped in single quotes. DO NOT COMMMIT this file to git!
# Wrap values in single quotes and escape all quotes inside strings with \.
# bundle exec ruby script/dev/example_s3.rb
=begin must be set
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_BUCKET_NAME=
AWS_REGION=
=end
s3 = Aws::S3::Client.new
resp = s3.list_buckets
puts resp.buckets.map(&:name)
resp = s3.list_objects(bucket: ENV['AWS_BUCKET_NAME'])
puts resp.inspect
#resp = resp.next_page # send a request for the next response page
#resp = resp.next_page until resp.last_page?
s3_resource = Aws::S3::Resource.new
# reference an existing bucket by name
bucket = s3_resource.bucket(ENV['AWS_BUCKET_NAME'])
# enumerate every object in a bucket
# bucket.objects.each do |obj|
# puts "#{obj.key} => #{obj.etag}"
# end
# batch operations, delete objects in batches of 1k
# bucket.objects(prefix: '/tmp-files/').delete
# bucket.objects.delete
# single object operations
# obj = bucket.object('hello')
# obj.put(body:'Hello World!')
# obj.etag
# obj.delete
# remove bucket
# bucket.delete!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment