lib/tori/backend/s3.rb in tori-0.4.1 vs lib/tori/backend/s3.rb in tori-0.5.0
- old
+ new
@@ -6,10 +6,16 @@
class S3
DEFAULT_CONTENT_TYPE = 'text/plain'.freeze
attr_accessor :bucket
attr_reader :client
+ class << self
+ def type_for(path)
+ (MIME::Types.type_for(path).first || DEFAULT_CONTENT_TYPE).to_s
+ end
+ end
+
# Must be set bucket name.
# And it use aws-sdk-core >= 2.0
# ENV takes precedence over credentials file and instance profile
#
# example:
@@ -45,20 +51,24 @@
end
end
def write(filename, resource, opts = nil)
opts ||= {}
+ if from_path = opts.delete(:from_path)
+ opts[:content_type] = self.class.type_for(from_path)
+ end
+
case resource
when String
put_object({
key: filename,
body: resource,
content_type: DEFAULT_CONTENT_TYPE,
}.merge(opts))
when File, Pathname
path = resource.to_path
- content_type = MIME::Types.type_for(path).first || DEFAULT_CONTENT_TYPE
+ content_type = self.class.type_for(path)
::File.open(path) { |f|
put_object({
key: filename,
body: f,
content_type: content_type.to_s,
@@ -89,13 +99,15 @@
def read(filename)
body(filename).read
end
+ def get(filename)
+ get_object(key: filename)
+ end
+
def body(filename)
- get_object(
- key: filename
- )[:body]
+ get(filename)[:body]
end
def put(filename, body, opts={})
put_object key: filename, body: body, **opts
end