Class: AwsRegion::AwsBucket
Overview
Methods for dealing with S3 buckets
Instance Attribute Summary collapse
-
#region ⇒ Object
Returns the value of attribute region.
Instance Method Summary collapse
-
#delete ⇒ AwsPageableResponse
Delete this bucket instance.
-
#delete_all_objects ⇒ Boolean
delete all objects in a bucket.
-
#delete_object(options = {}) ⇒ Boolean
deletes from s3 an object at :s3_path_to_object * :s3_path_to_object.
-
#find(options = {}) ⇒ Array<Hash>
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: :aws_path=>‘development’, :prefix=>‘broadhead’ would return array of names of objects in said bucket matching (in regex terms) development/broadhead.* * :aws_path - first part of S3 path to search * :prefix - Actually suffix of path to search.
-
#get(options = {}) ⇒ Boolean
writes contents of S3 object to local file example: get( :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 at development/myfile.txt * :s3_path_to_object - S3 object path * :dest_file_path - local file were file will be written.
-
#initialize(region, options = {}) ⇒ AwsBucket
constructor
Constructs a bucket instance from an existing bucket, or creates a new one with the name * :id - id of existing bucket * :bucket - Name of bucket to find or create.
-
#put(local_file_path, aws_path, options = {}) ⇒ Object
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.
-
#put_file(filename, file_identity) ⇒ AwsPageableResponse
Put a local file to this bucket.
Methods inherited from AwsBase
Constructor Details
#initialize(region, options = {}) ⇒ AwsBucket
Constructs a bucket instance from an existing bucket, or creates a new one with the name
-
:id - id of existing bucket
-
:bucket - Name of bucket to find or create
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/aws_region.rb', line 183 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.
176 177 178 |
# File 'lib/aws_region.rb', line 176 def region @region end |
Instance Method Details
#delete ⇒ AwsPageableResponse
Delete this bucket instance
202 203 204 |
# File 'lib/aws_region.rb', line 202 def delete @region.s3.delete_bucket({bucket: @id}) end |
#delete_all_objects ⇒ Boolean
delete all objects in a bucket
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/aws_region.rb', line 309 def delete_all_objects begin response = @region.s3.list_objects({:bucket => @id}) response[:contents].each do |obj| @region.s3.delete_object(:bucket => @id, :key => obj[:key]) end rescue Exception => e return false end true end |
#delete_object(options = {}) ⇒ Boolean
deletes from s3 an object at :s3_path_to_object
-
:s3_path_to_object
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/aws_region.rb', line 295 def delete_object(={}) begin s3_path_to_object = [:s3_path_to_object] log "s3 delete #{s3_path_to_object}" @region.s3.delete_object(:bucket => @id, :key => s3_path_to_object) rescue Exception => e return false end true end |
#find(options = {}) ⇒ Array<Hash>
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: :aws_path=>‘development’, :prefix=>‘broadhead’
would return array of names of objects in said bucket
matching (in regex terms) development/broadhead.*
-
:aws_path - first part of S3 path to search
-
:prefix - Actually suffix of path to search.
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/aws_region.rb', line 251 def find(={}) aws_path = [:aws_path] prefix = [:prefix] aws_path = '' if aws_path.nil? aws_path = aws_path[0..-2] if aws_path[-1..-1] == '/' log "s3 searching bucket:#{@id} for #{aws_path}/#{prefix}" objects = @region.s3.list_objects(:bucket => @id, :prefix => "#{aws_path}/#{prefix}") f = objects.contents.collect(&:key) log "s3 searched got: #{f.inspect}" f end |
#get(options = {}) ⇒ Boolean
writes contents of S3 object to local file example: get( :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
at development/myfile.txt
-
:s3_path_to_object - S3 object path
-
:dest_file_path - local file were file will be written
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/aws_region.rb', line 273 def get(={}) begin 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) log "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 File.open(dest_file_path, 'wb') do |file| response.body.each { |chunk| file.write chunk } end rescue Exception => e return false end true end |
#put(local_file_path, aws_path, options = {}) ⇒ Object
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
227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/aws_region.rb', line 227 def put(local_file_path, aws_path, ={}) aws_path = aws_path[0..-2] if aws_path[-1..-1] == '/' s3_path = "#{aws_path}/#{File.basename(local_file_path)}" log "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) ⇒ AwsPageableResponse
Put a local file to this bucket
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/aws_region.rb', line 210 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 |