Module: AWSAccess
- Defined in:
- lib/laws/aws_utils.rb
Instance Method Summary collapse
- #configure_aws(key, id) ⇒ Object
- #load_config_file(file_name) ⇒ Object
- #s3_create_bucket(name) ⇒ Object
- #s3_delete(bucket_name, obj_key = nil) ⇒ Object
- #s3_download_object(bucket_name, s3_key, filepath = nil) ⇒ Object
- #s3_exists?(bucket_name, s3_key = nil) ⇒ Boolean
-
#s3_list_buckets ⇒ Object
S3 Logic#################################.
-
#s3_list_contents(bucket_name) ⇒ Object
TODO show more than just the item name.
- #s3_upload_object(bucket_name, filepath, s3_key = nil, create_bucket = false) ⇒ Object
- #sdb_create_domain(name) ⇒ Object
- #sdb_delete(domain_name, item_key = nil) ⇒ Object
- #sdb_insert(domain_name, key, attributes) ⇒ Object
- #sdb_list_domains ⇒ Object
-
#sdb_print_items(domain_name, attributes = [], where_clause = nil) ⇒ Object
TODO rescue query exceptions and print a better error.
Instance Method Details
#configure_aws(key, id) ⇒ Object
8 9 10 11 |
# File 'lib/laws/aws_utils.rb', line 8 def configure_aws(key, id) AWS.config(:access_key_id => key, :secret_access_key => id) end |
#load_config_file(file_name) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/laws/aws_utils.rb', line 13 def load_config_file(file_name) unless File.exist?(file_name) puts "Unable to locate config file #{file_name}." return false end config = YAML.load_file(file_name) configure_aws(config["aws_id"], config["aws_key"]) true end |
#s3_create_bucket(name) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/laws/aws_utils.rb', line 113 def s3_create_bucket(name) s3 = AWS::S3.new if s3.buckets[name].exists? puts "Bucket #{name} already exists in S3." return false end s3.buckets.create(name) true end |
#s3_delete(bucket_name, obj_key = nil) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/laws/aws_utils.rb', line 175 def s3_delete(bucket_name, obj_key = nil) bucket = AWS::S3.new.buckets[bucket_name] unless bucket.exists? puts "Unable to locate bucket #{bucket_name}." return false end if obj_key.nil? bucket.delete! #TODO consider adding force support else obj = bucket.objects[obj_key] unless obj.exists? puts "Unable to locate item #{obj_key} in bucket #{bucket_name}" return false end obj.delete end end |
#s3_download_object(bucket_name, s3_key, filepath = nil) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/laws/aws_utils.rb', line 158 def s3_download_object(bucket_name, s3_key, filepath = nil) filepath ||= "./#{s3_key}" bucket = AWS::S3.new.buckets[bucket_name] unless bucket.exists? puts "Unable to locate bucket #{bucket_name}." return false end obj = bucket.objects[s3_key] unless obj.exists? puts "Unable to locate item #{s3_key} in bucket #{bucket_name}." return false end File.open(filepath, 'w') {|f| f.write(obj.read)} end |
#s3_exists?(bucket_name, s3_key = nil) ⇒ Boolean
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/laws/aws_utils.rb', line 123 def s3_exists?(bucket_name, s3_key = nil) bucket = AWS::S3.new.buckets[bucket_name] unless bucket.exists? puts "Unable to locate bucket #{bucket_name}." return false end if s3_key.nil? true else bucket[s3_key].exists? end end |
#s3_list_buckets ⇒ Object
S3 Logic#################################
95 96 97 |
# File 'lib/laws/aws_utils.rb', line 95 def s3_list_buckets AWS::S3.new.buckets.each {|b| puts b.name} end |
#s3_list_contents(bucket_name) ⇒ Object
TODO show more than just the item name
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/laws/aws_utils.rb', line 100 def s3_list_contents(bucket_name) bucket = AWS::S3.new.buckets[bucket_name] unless bucket.exists? puts "Unable to locate bucket #{bucket_name}." return false end bucket.objects.each do |o| puts o.key end true end |
#s3_upload_object(bucket_name, filepath, s3_key = nil, create_bucket = false) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/laws/aws_utils.rb', line 137 def s3_upload_object(bucket_name, filepath, s3_key = nil, create_bucket = false) s3_key ||= File.basename(filepath) unless File.exists?(filepath) puts "Unable to locate file at #{filepath}." return false end s3 = AWS::S3.new bucket = s3.buckets[bucket_name] unless bucket.exists? if create_bucket s3.buckets.create(bucket_name) else puts "Unable to locate bucket #{bucket_name}." return false end end bucket.objects[s3_key].write(File.open(filepath){|f| f.read}) end |
#sdb_create_domain(name) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/laws/aws_utils.rb', line 51 def sdb_create_domain(name) db = AWS::SimpleDB.new if db.domains[name].exists? puts "Domain #{name} already exists in SimpleDB." return false end db.domains.create(name) true end |
#sdb_delete(domain_name, item_key = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/laws/aws_utils.rb', line 77 def sdb_delete(domain_name, item_key = nil) db = AWS::SimpleDB.new dom = db.domains[domain_name] unless dom.exists? puts "Unable to locate domain #{domain_name}." return false end if item_key.nil? dom.delete! #TODO consider adding force support else item = dom.items[item_key] item.delete end end |
#sdb_insert(domain_name, key, attributes) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/laws/aws_utils.rb', line 62 def sdb_insert(domain_name, key, attributes) db = AWS::SimpleDB.new dom = db.domains[domain_name] unless dom.exists? puts "Unable to locate domain #{domain_name}." return false end new_item = dom.items[key] attributes.each do |k, v| new_item.attributes[k] = v end true end |
#sdb_list_domains ⇒ Object
47 48 49 |
# File 'lib/laws/aws_utils.rb', line 47 def sdb_list_domains AWS::SimpleDB.new.domains.each {|d| puts d.name} end |
#sdb_print_items(domain_name, attributes = [], where_clause = nil) ⇒ Object
TODO rescue query exceptions and print a better error
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/laws/aws_utils.rb', line 25 def sdb_print_items(domain_name, attributes = [], where_clause = nil) db = AWS::SimpleDB.new dom = db.domains[domain_name] unless dom.exists? puts "Unable to locate domain #{domain_name}." return false end items = dom.items.select(attributes) items = items.where(where_clause) unless where_clause.nil? items.each do |i| print i.name print_attributes = attributes.empty? ? i.attributes.keys : attributes print_attributes.each do |a| print ",#{a}=#{i.attributes[a]}" end print "\n" end true end |