Class: DigitalOpera::Services::S3
- Defined in:
- lib/digital_opera/services/s3.rb
Class Method Summary collapse
Instance Method Summary collapse
- #access_key ⇒ Object
- #base_url ⇒ Object
- #bucket ⇒ Object
- #bucket_name ⇒ Object
- #delete ⇒ Object
- #document ⇒ Object
- #document_name ⇒ Object
- #expiration(time = 10.hours) ⇒ Object
- #get_key(url) ⇒ Object
-
#initialize(document, options = {}) ⇒ S3
constructor
A new instance of S3.
- #key ⇒ Object
- #policy ⇒ Object
- #policy_data ⇒ Object
- #private_url ⇒ Object
- #public_url ⇒ Object
- #s3_object ⇒ Object
- #secret_access_key ⇒ Object
- #signature ⇒ Object
- #upload_key ⇒ Object
Constructor Details
#initialize(document, options = {}) ⇒ S3
Returns a new instance of S3.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/digital_opera/services/s3.rb', line 7 def initialize(document, ={}) throw 'AWS SDK is required' if !defined?(AWS) || AWS.nil? if document.class == String @key = self.get_key(document) else @document = document end @options = { acl: 'authenticated-read' }.merge() end |
Class Method Details
.access_key ⇒ Object
139 140 141 |
# File 'lib/digital_opera/services/s3.rb', line 139 def self.access_key ENV["S3_ACCESS_KEY"] end |
.bucket_name ⇒ Object
135 136 137 |
# File 'lib/digital_opera/services/s3.rb', line 135 def self.bucket_name ENV["S3_BUCKET_NAME"] end |
.secret_access_key ⇒ Object
143 144 145 |
# File 'lib/digital_opera/services/s3.rb', line 143 def self.secret_access_key ENV["S3_SECRET_ACCESS_KEY"] end |
Instance Method Details
#access_key ⇒ Object
115 116 117 |
# File 'lib/digital_opera/services/s3.rb', line 115 def access_key self.class.access_key end |
#base_url ⇒ Object
25 26 27 |
# File 'lib/digital_opera/services/s3.rb', line 25 def base_url "https://#{self.bucket_name}.s3.amazonaws.com/" end |
#bucket ⇒ Object
107 108 109 |
# File 'lib/digital_opera/services/s3.rb', line 107 def bucket ::AWS::S3.new.buckets[self.bucket_name] end |
#bucket_name ⇒ Object
111 112 113 |
# File 'lib/digital_opera/services/s3.rb', line 111 def bucket_name self.class.bucket_name end |
#delete ⇒ Object
66 67 68 |
# File 'lib/digital_opera/services/s3.rb', line 66 def delete s3_object.delete end |
#document ⇒ Object
21 22 23 |
# File 'lib/digital_opera/services/s3.rb', line 21 def document @document end |
#document_name ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/digital_opera/services/s3.rb', line 37 def document_name if key.present? key.downcase.split('/').last else nil end end |
#expiration(time = 10.hours) ⇒ Object
62 63 64 |
# File 'lib/digital_opera/services/s3.rb', line 62 def expiration(time=10.hours) (time).from_now.to_time.iso8601 end |
#get_key(url) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/digital_opera/services/s3.rb', line 123 def get_key(url) if url.start_with? self.base_url url.split(self.base_url).last else if url.start_with? '/' url[1..(url.length-1)] else url end end end |
#key ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/digital_opera/services/s3.rb', line 29 def key @key ||= if document.s3_key.present? self.get_key document.s3_key else nil end end |
#policy ⇒ Object
70 71 72 |
# File 'lib/digital_opera/services/s3.rb', line 70 def policy Base64.encode64(self.policy_data.to_json).gsub("\n", "") end |
#policy_data ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/digital_opera/services/s3.rb', line 74 def policy_data { expiration: self.expiration, conditions: [ ["starts-with", "$utf8", ""], ["starts-with", "$key", ""], { bucket: self.bucket_name }, { acl: @options[:acl] } ] } end |
#private_url ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/digital_opera/services/s3.rb', line 53 def private_url if self.s3_object.present? disposition = "attachment; filename=#{document_name.parameterize}" self.s3_object.url_for(:read, :expires => self.expiration(10.minutes), :response_content_disposition => disposition).to_s else nil end end |
#public_url ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/digital_opera/services/s3.rb', line 45 def public_url if self.s3_object.present? self.s3_object.public_url.to_s else nil end end |
#s3_object ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/digital_opera/services/s3.rb', line 86 def s3_object @s3_object ||= if self.key.present? self.bucket.objects[self.key] else nil end end |
#secret_access_key ⇒ Object
119 120 121 |
# File 'lib/digital_opera/services/s3.rb', line 119 def secret_access_key self.class.secret_access_key end |
#signature ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/digital_opera/services/s3.rb', line 94 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), self.secret_access_key, policy ) ).gsub("\n", "") end |
#upload_key ⇒ Object
103 104 105 |
# File 'lib/digital_opera/services/s3.rb', line 103 def upload_key 'documents/${filename}' end |