Class: Shutterbug::Storage::S3Storage
- Inherits:
-
Object
- Object
- Shutterbug::Storage::S3Storage
- Defined in:
- lib/shutterbug/storage/s3_storage.rb
Constant Summary collapse
- PUT_URL_EXP_TIME =
seconds
300
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .connect! ⇒ Object
- .connection ⇒ Object
- .create_bin ⇒ Object
- .fs_path_exists?(filename) ⇒ Boolean
- .get_url(filename) ⇒ Object
- .lookup_bin ⇒ Object
- .put_url(filename) ⇒ Object
- .s3_bin ⇒ Object
- .write(filename) ⇒ Object
Instance Method Summary collapse
-
#initialize(filename) ⇒ S3Storage
constructor
A new instance of S3Storage.
- #unlink(filename) ⇒ Object
Constructor Details
#initialize(filename) ⇒ S3Storage
Returns a new instance of S3Storage.
74 75 76 77 78 79 80 81 82 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 74 def initialize(filename) @filename = filename begin @stream_file = S3Storage.write(filename) @url = @stream_file.public_url ensure unlink(filename) end end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
7 8 9 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 7 def filename @filename end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 8 def url @url end |
Class Method Details
.connect! ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 12 def self.connect! Fog::Storage.new({ :provider => 'AWS', :aws_access_key_id => Configuration.instance.s3_key, :aws_secret_access_key => Configuration.instance.s3_secret }) end |
.connection ⇒ Object
20 21 22 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 20 def self.connection @connection ||= self.connect! end |
.create_bin ⇒ Object
24 25 26 27 28 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 24 def self.create_bin self.connection.directories.create( :key => Configuration.instance.s3_bin, :public => true) end |
.fs_path_exists?(filename) ⇒ Boolean
48 49 50 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 48 def self.fs_path_exists?(filename) File.exists?(filename) end |
.get_url(filename) ⇒ Object
52 53 54 55 56 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 52 def self.get_url(filename) # Manual URL construction, no proper method implemented in FOG. # But should be available soon, see: https://github.com/fog/fog/issues/3263 "https://#{Configuration.instance.s3_bin}.s3.amazonaws.com/#{filename}" end |
.lookup_bin ⇒ Object
30 31 32 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 30 def self.lookup_bin self.connection.directories.get(Configuration.instance.s3_bin) || self.create_bin end |
.put_url(filename) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 58 def self.put_url(filename) expiry = (Time.now + PUT_URL_EXP_TIME).to_i headers = {} query = { 'x-amz-acl' => 'public-read' } = { path_style: true, query: query } self.connection.put_object_url(Configuration.instance.s3_bin, filename, expiry, headers, ) end |
.s3_bin ⇒ Object
34 35 36 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 34 def self.s3_bin @s3_bin ||= self.lookup_bin end |
.write(filename) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 38 def self.write(filename) full_path = Configuration.instance.fs_path_for(filename) if self.fs_path_exists? full_path self.s3_bin.files.create( :key => filename, :body => File.open(full_path), :public => true) end end |
Instance Method Details
#unlink(filename) ⇒ Object
68 69 70 71 72 |
# File 'lib/shutterbug/storage/s3_storage.rb', line 68 def unlink(filename) if File.exists? filename FileUtils.rm(filename) end end |