Class: Attachie::S3Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/attachie/s3_driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s3_client) ⇒ S3Driver

Returns a new instance of S3Driver.



57
58
59
60
# File 'lib/attachie/s3_driver.rb', line 57

def initialize(s3_client)
  self.s3_client = s3_client
  self.s3_resource = Aws::S3::Resource.new(client: s3_client)
end

Instance Attribute Details

#s3_clientObject

Returns the value of attribute s3_client.



55
56
57
# File 'lib/attachie/s3_driver.rb', line 55

def s3_client
  @s3_client
end

#s3_resourceObject

Returns the value of attribute s3_resource.



55
56
57
# File 'lib/attachie/s3_driver.rb', line 55

def s3_resource
  @s3_resource
end

Instance Method Details

#delete(name, bucket) ⇒ Object



127
128
129
130
131
# File 'lib/attachie/s3_driver.rb', line 127

def delete(name, bucket)
  s3_resource.bucket(bucket).object(name).delete

  true
end

#download(name, bucket, path) ⇒ Object



121
122
123
124
125
# File 'lib/attachie/s3_driver.rb', line 121

def download(name, bucket, path)
  s3_resource.bucket(bucket).object(name).download_file(path)
rescue Aws::S3::Errors::NotFound => e
  raise ItemNotFound, e.message
end

#exists?(name, bucket) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/attachie/s3_driver.rb', line 133

def exists?(name, bucket)
  s3_resource.bucket(bucket).object(name).exists?
end

#info(name, bucket) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/attachie/s3_driver.rb', line 79

def info(name, bucket)
  object = s3_resource.bucket(bucket).object(name)

  {
    content_length: object.content_length,
    last_modified: object.last_modified,
    content_type: object.content_type
  }
rescue Aws::S3::Errors::NotFound => e
  raise ItemNotFound, e.message
end

#list(bucket, prefix: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/attachie/s3_driver.rb', line 68

def list(bucket, prefix: nil)
  return enum_for(:list, bucket, prefix: prefix) unless block_given?

  options = {}
  options[:prefix] = prefix if prefix

  s3_resource.bucket(bucket).objects(options).each do |object|
    yield object.key
  end
end

#presigned_post(name, bucket, options = {}) ⇒ Object



62
63
64
65
66
# File 'lib/attachie/s3_driver.rb', line 62

def presigned_post(name, bucket, options = {})
  res = s3_resource.bucket(bucket).object(name).presigned_post(options)

  return { fields: res.fields, headers: {}, method: "post", url: res.url }
end

#store(name, data_or_io, bucket, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/attachie/s3_driver.rb', line 91

def store(name, data_or_io, bucket, options = {})
  opts = options.dup

  mime_type = MIME::Types.of(name).first

  opts[:content_type] ||= mime_type.content_type if mime_type
  opts[:content_type] ||= "application/octet-stream"

  opts[:body] = data_or_io

  s3_resource.bucket(bucket).object(name).put(opts)
end

#store_multipart(name, bucket, options = {}, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/attachie/s3_driver.rb', line 104

def store_multipart(name, bucket, options = {}, &block)
  opts = options.dup

  mime_type = MIME::Types.of(name).first

  opts[:content_type] ||= mime_type.content_type if mime_type
  opts[:content_type] ||= "application/octet-stream"

  S3MultipartUpload.new(s3_client, name, bucket, opts, &block)
end

#temp_url(name, bucket, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/attachie/s3_driver.rb', line 137

def temp_url(name, bucket, options = {})
  opts = options.dup
  opts[:expires_in] = opts.delete(:expires_in).to_i if opts.key?(:expires_in)

  method = opts.delete(:method) || :get

  s3_resource.bucket(bucket).object(name).presigned_url(method, opts)
end

#value(name, bucket) ⇒ Object



115
116
117
118
119
# File 'lib/attachie/s3_driver.rb', line 115

def value(name, bucket)
  s3_resource.bucket(bucket).object(name).get.body.read.force_encoding(Encoding::BINARY)
rescue Aws::S3::Errors::NotFound => e
  raise ItemNotFound, e.message
end