Class: Shutterbug::Storage::S3Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/shutterbug/storage/s3_storage.rb

Constant Summary collapse

PUT_URL_EXP_TIME =

seconds

300

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ S3Storage

Returns a new instance of S3Storage.



63
64
65
66
67
# File 'lib/shutterbug/storage/s3_storage.rb', line 63

def initialize(filename)
  @filename = filename
  @stream_file = S3Storage.write(filename)
  @url = @stream_file.public_url
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/shutterbug/storage/s3_storage.rb', line 6

def filename
  @filename
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/shutterbug/storage/s3_storage.rb', line 7

def url
  @url
end

Class Method Details

.connect!Object



11
12
13
14
15
16
17
# File 'lib/shutterbug/storage/s3_storage.rb', line 11

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

.connectionObject



19
20
21
# File 'lib/shutterbug/storage/s3_storage.rb', line 19

def self.connection
  @connection ||= self.connect!
end

.create_binObject



23
24
25
26
27
# File 'lib/shutterbug/storage/s3_storage.rb', line 23

def self.create_bin
  self.connection.directories.create(
    :key    => Configuration.instance.s3_bin,
    :public => true)
end

.fs_path_exists?(filename) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/shutterbug/storage/s3_storage.rb', line 43

def self.fs_path_exists?(filename)
  File.exists?(filename)
end

.get_url(filename) ⇒ Object



47
48
49
50
51
# File 'lib/shutterbug/storage/s3_storage.rb', line 47

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

.put_url(filename) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/shutterbug/storage/s3_storage.rb', line 53

def self.put_url(filename)
  expiry = (Time.now + PUT_URL_EXP_TIME).to_i
  headers = {}
  query = {
    'x-amz-acl' => 'public-read'
  }
  options = { path_style: true, query: query }
  self.connection.put_object_url(Configuration.instance.s3_bin, filename, expiry, headers, options)
end

.s3_binObject



29
30
31
# File 'lib/shutterbug/storage/s3_storage.rb', line 29

def self.s3_bin
  @s3_bin ||= self.create_bin
end

.write(filename) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/shutterbug/storage/s3_storage.rb', line 33

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