Class: AwsRegion::AwsBucket
- Inherits:
-
Object
- Object
- AwsRegion::AwsBucket
- Defined in:
- lib/aws_region.rb
Instance Attribute Summary collapse
-
#region ⇒ Object
Returns the value of attribute region.
Instance Method Summary collapse
- #delete ⇒ Object
- #delete_all_objects ⇒ Object
- #delete_object(options = {}) ⇒ Object
- #find(options = {}) ⇒ Object
- #get(options = {}) ⇒ Object
-
#initialize(region, options = {}) ⇒ AwsBucket
constructor
A new instance of AwsBucket.
- #put(local_file_path, aws_path, options = {}) ⇒ Object
- #put_file(filename, file_identity) ⇒ Object
Constructor Details
#initialize(region, options = {}) ⇒ AwsBucket
Returns a new instance of AwsBucket.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/aws_region.rb', line 107 def initialize(region, ={}) @region = region if .has_key?(:id) @id = [:id] elsif .has_key?(:bucket) bucket = [:bucket] if @region.find_buckets({bucket: bucket}).length <= 0 @region.s3.create_bucket({:bucket => bucket, :create_bucket_configuration => {:location_constraint => @region.region}}) if @region.find_buckets({bucket: bucket}).length <= 0 raise "Error creating bucket: #{bucket} in region: #{@region.region}" end end @id = bucket end end |
Instance Attribute Details
#region ⇒ Object
Returns the value of attribute region.
106 107 108 |
# File 'lib/aws_region.rb', line 106 def region @region end |
Instance Method Details
#delete ⇒ Object
123 124 125 |
# File 'lib/aws_region.rb', line 123 def delete @region.s3.delete_bucket({bucket: @id}) end |
#delete_all_objects ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/aws_region.rb', line 206 def delete_all_objects response = @region.s3.list_objects({:bucket => @id}) response[:contents].each do |obj| @region.s3.delete_object( :bucket => @id, :key => obj[:key]) end end |
#delete_object(options = {}) ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/aws_region.rb', line 197 def delete_object(={}) # deletes from s3 an object in :bucket at :s3_path_to_object s3_path_to_object = [:s3_path_to_object] puts "s3 delete #{s3_path_to_object}" @region.s3.delete_object( :bucket => @id, :key => s3_path_to_object) puts "s3 deleted." end |
#find(options = {}) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/aws_region.rb', line 154 def find(={}) # prefix is something like: hchd-A-A-Items # This will return in an array of strings the names of all objects in s3 in # the :aws_path under :bucket starting with passed-in prefix # example: :bucket=>'mazama-inventory', :aws_path=>'development', :prefix=>'broadhead' # would return array of names of objects in said bucket # matching (in regex terms) development/broadhead.* # return empty array if no matching objects exist aws_path = [:aws_path] prefix = [:prefix] aws_path = '' if aws_path.nil? aws_path = aws_path[0..-2] if aws_path[-1..-1] == '/' puts "s3 searching bucket:#{@id} for #{aws_path}/#{prefix}" objects = @region.s3.list_objects(:bucket => @id, :prefix => "#{aws_path}/#{prefix}") f = objects.contents.collect(&:key) puts "s3 searched got: #{f.inspect}" f end |
#get(options = {}) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/aws_region.rb', line 174 def get(={}) # writes to local file an s3 object in :bucket at :s3_path_to_object to :dest_file_path # example: get_object_as_local_file( {:bucket=>'mazama-inventory', # :s3_path_to_object=>development/myfile.txt', # :dest_file_path=>'/tmp/foo.txt'}) # would write to local /tmp/foo.txt a file retrieved from s3 in 'mazama-inventory' bucket # at development/myfile.txt s3_path_to_object = [:s3_path_to_object] dest_file_path = [:dest_file_path] File.delete dest_file_path if File.exists?(dest_file_path) puts "s3 get bucket:#{@id} path:#{s3_path_to_object} dest:#{dest_file_path}" response = @region.s3.get_object(:bucket => @id, :key => s3_path_to_object) response.body.rewind # I DO NOT KNOW what happens if the body is "too big". I didn't see a method in the # API to chunk it out... but perhaps response.body does this already. File.open(dest_file_path, 'wb') do |file| response.body.each { |chunk| file.write chunk } end puts "s3 got " + `ls -l #{dest_file_path}`.strip nil end |
#put(local_file_path, aws_path, options = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/aws_region.rb', line 137 def put(local_file_path, aws_path, ={}) # puts a local file to an s3 object in bucket on path # example: put_local_file {:bucket=>"bucket", :local_file_path=>"/tmp/bar/foo.txt", :aws_path=>"b"} # would make an s3 object named foo.txt in bucket/b aws_path = aws_path[0..-2] if aws_path[-1..-1] == '/' s3_path = "#{aws_path}/#{File.basename(local_file_path)}" puts "s3 writing #{local_file_path} to bucket #{@id} path: #{aws_path} s3 path: #{s3_path}" f = File.open local_file_path, 'rb' [:bucket] = @id [:key] = s3_path [:body] = f [:storage_class] = 'REDUCED_REDUNDANCY' result = @region.s3.put_object(params=) f.close result end |
#put_file(filename, file_identity) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/aws_region.rb', line 126 def put_file(filename, file_identity) File.open(filename, 'r') do |reading_file| resp = @region.s3.put_object( acl: "bucket-owner-full-control", body: reading_file, bucket: @id, key: file_identity ) end end |