class Storage::S3Store
Constants
- CONTENT_TYPES
- HALF_AN_HOUR
Public Class Methods
new(path_prefix, options)
click to toggle source
# File lib/storage/s3_store.rb, line 119 def initialize(path_prefix, options) @path_prefix = path_prefix @url_expires = options[:url_expires] || HALF_AN_HOUR @bucket_name = options[:bucket_name] @namespace = options[:namespace] end
Public Instance Methods
absolute_path(*relative_paths)
click to toggle source
todo: this should be interface that retrive a lazy file object
# File lib/storage/s3_store.rb, line 180 def absolute_path(*relative_paths) File.join("s3:#{bucket_name}://", *relative_paths) end
clear()
click to toggle source
# File lib/storage/s3_store.rb, line 188 def clear if s3_path.nil? || s3_path.empty? bucket.clear else bucket.objects(prefix: s3_path).batch_delete! end end
content_type(path)
click to toggle source
# File lib/storage/s3_store.rb, line 139 def content_type(path) object(path).get.content_type end
copy(path, to_local_path)
click to toggle source
# File lib/storage/s3_store.rb, line 143 def copy(path, to_local_path) obj_content = read(path) raise "File(#{path}) does not exist in the bucket #{bucket.name}" if obj_content.nil? File.open(to_local_path, 'w') do |f| f.write(obj_content) end end
delete(path)
click to toggle source
# File lib/storage/s3_store.rb, line 184 def delete(path) bucket.objects(prefix: s3_path(path)).batch_delete! end
exists?(path)
click to toggle source
# File lib/storage/s3_store.rb, line 162 def exists?(path) !object(path).nil? end
objects(path)
click to toggle source
# File lib/storage/s3_store.rb, line 196 def objects(path) bucket.objects(prefix: s3_path(path)) end
public_url(path, opts={})
click to toggle source
# File lib/storage/s3_store.rb, line 175 def public_url(path, opts={}) object(path).public_url(opts).to_s end
read(path)
click to toggle source
# File lib/storage/s3_store.rb, line 156 def read(path) object = object(path) return object.get.body.read unless object.nil? object end
upload(path, local_file, options={})
click to toggle source
# File lib/storage/s3_store.rb, line 126 def upload(path, local_file, options={}) local_file_name = File.basename(local_file) object = bucket.object(s3_path(path, local_file_name)) object.upload_file(Pathname.new(local_file), { :content_type => derive_content_type(local_file_name) }.merge(options)) end
upload_dir(path, local_dir)
click to toggle source
# File lib/storage/s3_store.rb, line 132 def upload_dir(path, local_dir) bucket.objects(prefix: s3_path(path)).batch_delete! Dir[File.join(local_dir, "*")].each do |f| upload(path, f) end end
url_for(path, opts = {})
click to toggle source
# File lib/storage/s3_store.rb, line 166 def url_for(path, opts = {}) url_opts = { :expires_in => opts.delete(:expires_in) || @url_expires, :response_content_type => derive_content_type(path) }.merge(opts) _object = object(path) _object.nil? ? '' : _object.presigned_url(:get, url_opts) end
write_to_file(path, content, options={})
click to toggle source
# File lib/storage/s3_store.rb, line 151 def write_to_file(path, content, options={}) object = bucket.object(s3_path(path)) object.put({body: content}.merge(options)) end
Private Instance Methods
bucket()
click to toggle source
# File lib/storage/s3_store.rb, line 222 def bucket @bucket ||= s3.bucket(bucket_name) end
bucket_name()
click to toggle source
# File lib/storage/s3_store.rb, line 226 def bucket_name @bucket_name.is_a?(String) ? @bucket_name : @bucket_name[@path_prefix] end
derive_content_type(file_name)
click to toggle source
# File lib/storage/s3_store.rb, line 201 def derive_content_type(file_name) file_extension = file_name.split(".").last CONTENT_TYPES[file_extension] end
namespace()
click to toggle source
# File lib/storage/s3_store.rb, line 214 def namespace Proc === @namespace ? @namespace.call : @namespace end
object(path)
click to toggle source
# File lib/storage/s3_store.rb, line 210 def object(path) bucket.objects.find { |object| object.key == s3_path(path) } end
s3()
click to toggle source
# File lib/storage/s3_store.rb, line 206 def s3 Aws::S3::Resource.new end
s3_path(*paths)
click to toggle source
# File lib/storage/s3_store.rb, line 218 def s3_path(*paths) File.join(*([namespace, @path_prefix, *paths].compact)) end