Class: RainForest::S3
- Inherits:
-
Object
- Object
- RainForest::S3
- Defined in:
- lib/rain_forest/s3.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
- .content_length(storage_key) ⇒ Object
- .copy(source_key, dest_key) ⇒ Object
- .delete_objects(prefix) ⇒ Object
- .move(source_key, dest_key) ⇒ Object
- .read(storage_key) ⇒ Object
- .set_index_document(index_document) ⇒ Object
- .write(storage_key, data, options = {}) ⇒ Object
Instance Method Summary collapse
- #content_length(storage_key) ⇒ Object
- #copy(source_key, dest_key) ⇒ Object
- #delete_objects(prefix) ⇒ Object
-
#initialize ⇒ S3
constructor
A new instance of S3.
- #move(source_key, dest_key) ⇒ Object
- #read(storage_key) ⇒ Object
- #set_index_document(index_document) ⇒ Object
- #write(storage_key, data, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ S3
Returns a new instance of S3.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rain_forest/s3.rb', line 7 def initialize region = ENV['RAIN_FOREST_AWS_REGION'] region = ENV['rain_forest_aws_region'] if region.nil?#fallback to older config akid = ENV['RAIN_FOREST_AWS_AKID'] akid = ENV['rain_forest_aws_akid'] if akid.nil?#fallback to older config secret = ENV['RAIN_FOREST_AWS_SECRET'] secret = ENV['rain_forest_aws_secret'] if secret.nil?#fallback to older config bucket = ENV['RAIN_FOREST_AWS_BUCKET'] bucket = ENV['rain_forest_aws_bucket'] if bucket.nil?#fallback to older config credentials = ::Aws::Credentials.new(akid, secret) @bucket = ENV['RAIN_FOREST_AWS_BUCKET'] @client = ::Aws::S3::Client.new(region: region, credentials: credentials) end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
5 6 7 |
# File 'lib/rain_forest/s3.rb', line 5 def bucket @bucket end |
#client ⇒ Object
Returns the value of attribute client.
5 6 7 |
# File 'lib/rain_forest/s3.rb', line 5 def client @client end |
Class Method Details
.content_length(storage_key) ⇒ Object
41 42 43 |
# File 'lib/rain_forest/s3.rb', line 41 def self.content_length(storage_key) self.new.content_length(storage_key) end |
.copy(source_key, dest_key) ⇒ Object
33 34 35 |
# File 'lib/rain_forest/s3.rb', line 33 def self.copy(source_key, dest_key) self.new.copy(source_key, dest_key) end |
.delete_objects(prefix) ⇒ Object
45 46 47 |
# File 'lib/rain_forest/s3.rb', line 45 def self.delete_objects(prefix) self.new.delete_objects(prefix) end |
.move(source_key, dest_key) ⇒ Object
37 38 39 |
# File 'lib/rain_forest/s3.rb', line 37 def self.move(source_key, dest_key) self.new.move(source_key, dest_key) end |
.read(storage_key) ⇒ Object
29 30 31 |
# File 'lib/rain_forest/s3.rb', line 29 def self.read(storage_key) self.new.read(storage_key) end |
.set_index_document(index_document) ⇒ Object
49 50 51 |
# File 'lib/rain_forest/s3.rb', line 49 def self.set_index_document(index_document) self.new.set_index_document(index_document) end |
.write(storage_key, data, options = {}) ⇒ Object
25 26 27 |
# File 'lib/rain_forest/s3.rb', line 25 def self.write(storage_key, data, ={}) self.new.write(storage_key, data, ) end |
Instance Method Details
#content_length(storage_key) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/rain_forest/s3.rb', line 100 def content_length(storage_key) begin resp = @client.head_object(bucket: @bucket, key: storage_key) return resp.content_length rescue Exception return -1 end end |
#copy(source_key, dest_key) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/rain_forest/s3.rb', line 79 def copy(source_key, dest_key) begin @client.copy_object(bucket: @bucket, copy_source: "#{@bucket}/#{source_key}", key: dest_key) rescue Exception => e return false, e. end return true, nil end |
#delete_objects(prefix) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rain_forest/s3.rb', line 109 def delete_objects(prefix) begin objects_deleted = 0 resp = @client.list_objects(bucket: @bucket, prefix: prefix) if resp.contents.length > 0 objects = resp.contents.map{|f| {key: f.key}} resp = @client.delete_objects(bucket: @bucket, delete: {objects: objects}) objects_deleted += resp.deleted.length end return objects_deleted rescue Exception return -1 end end |
#move(source_key, dest_key) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rain_forest/s3.rb', line 89 def move(source_key, dest_key) begin @client.copy_object(bucket: @bucket, copy_source: "#{@bucket}/#{source_key}", key: dest_key) @client.delete_object(bucket: @bucket, key: source_key) rescue Exception => e return false, e. end return true, nil end |
#read(storage_key) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/rain_forest/s3.rb', line 70 def read(storage_key) begin resp = @client.get_object(bucket: @bucket, key: storage_key) return true, resp.body.read rescue Exception => e return false, e. end end |
#set_index_document(index_document) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rain_forest/s3.rb', line 126 def set_index_document(index_document) begin resp = @client.put_bucket_website( bucket: @bucket, website_configuration: { index_document: { suffix: index_document } } ) rescue Exception => e return false, e. end return true, nil end |
#write(storage_key, data, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rain_forest/s3.rb', line 53 def write(storage_key, data, ={}) attrs = { bucket: @bucket, key: storage_key, body: data, acl: 'public-read' }.merge() begin @client.put_object(attrs) rescue Exception => e return false, e. end return true, nil end |