Class: MiniPaperclip::Storage::S3

Inherits:
Base
  • Object
show all
Defined in:
lib/mini_paperclip/storage/s3.rb

Instance Attribute Summary

Attributes inherited from Base

#attachment, #config, #interpolator

Instance Method Summary collapse

Methods inherited from Base

#initialize, #interpolate, #path_for, #url_for_read

Constructor Details

This class inherits a constructor from MiniPaperclip::Storage::Base

Instance Method Details

#do_delete_filesObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mini_paperclip/storage/s3.rb', line 43

def do_delete_files
  return if @deletes.empty?
  debug("deleting by S3 to bucket:#{@config.s3_bucket_name},objects:#{@deletes}")
  Aws::S3::Client.new.delete_objects(
    bucket: @config.s3_bucket_name,
    delete: {
      objects: @deletes,
      quiet: true,
    }
  )
end

#exists?(style) ⇒ Boolean Also known as: exist?

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/mini_paperclip/storage/s3.rb', line 28

def exists?(style)
  Aws::S3::Client.new.head_object(
    bucket: @config.s3_bucket_name,
    key: s3_object_key(style),
  )
  true
rescue Aws::S3::Errors::NotFound
  false
end

#hostObject



23
24
25
26
# File 'lib/mini_paperclip/storage/s3.rb', line 23

def host
  # AWS CloudFront origin should be attached bucket name
  @config.s3_host_alias || "#{@config.s3_bucket_name}.#{@config.url_host}"
end

#open(style) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mini_paperclip/storage/s3.rb', line 55

def open(style)
  Tempfile.new(['MiniPaperclip::Storage::S3']).tap do |response_target|
    response_target.binmode
    Aws::S3::Client.new.get_object(
      bucket: @config.s3_bucket_name,
      key: s3_object_key(style),
      response_target: response_target,
    )
    yield response_target if block_given?
  end
end

#push_delete_file(style) ⇒ Object



39
40
41
# File 'lib/mini_paperclip/storage/s3.rb', line 39

def push_delete_file(style)
  @deletes.push({ key: s3_object_key(style) })
end

#s3_object_key(style) ⇒ Object



19
20
21
# File 'lib/mini_paperclip/storage/s3.rb', line 19

def s3_object_key(style)
  interpolate(@config.url_path, style)
end

#write(style, file) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mini_paperclip/storage/s3.rb', line 6

def write(style, file)
  debug("writing by S3 to bucket:#{@config.s3_bucket_name},key:#{s3_object_key(style)}")
  Aws::S3::Client.new.put_object(
    acl: @config.s3_acl,
    cache_control: @config.s3_cache_control,
    content_type: @attachment.content_type,
    body: file.tap(&:rewind),
    bucket: @config.s3_bucket_name,
    key: s3_object_key(style),
  )
  @deletes.delete({ key: s3_object_key(style) }) # cancel deletion if overwrite
end