module S3Index
Namespaces
module
Constants
- VERSION
Public Instance Methods
default_client()
click to toggle source
# File lib/s3_index.rb, line 85 def default_client @s3_client ||= new_client end
default_client=(client)
click to toggle source
# File lib/s3_index.rb, line 99 def default_client=(client) @s3_client = client end
download!(s3: default_client, src: nil, s3_url: nil, index: nil, dst: nil)
click to toggle source
Download file from s3 bucket back on Index
model. src and s3_url are quick options to find an Index
model to download.
# File lib/s3_index.rb, line 58 def download!(s3: default_client, src: nil, s3_url: nil, index: nil, dst: nil) src || s3_url || index || raise(ArgumentError, 'src:, s3_url:, or index: must be provided!') index ||= begin finder = src ? { origin_url: src } : { s3_url: s3_url } Index.find_by(finder) || raise(ActiveRecord::RecordNotFound, "S3Index::Index not found by #{finder}") end obj = s3.bucket(index.s3_bucket).object(index.origin_url) obj.get(response_target: dst || index.origin_url) end
env()
click to toggle source
# File lib/s3_index.rb, line 13 def env @env ||= ActiveSupport::StringInquirer.new(ENV['S3_INDEX_ENV']) end
index_attributes_for(path)
click to toggle source
# File lib/s3_index.rb, line 76 def index_attributes_for(path) { md5: Digest::MD5.file(path).to_s, file_name: File.basename(path), content_type: MIME::Types.type_for(path).first.try(:content_type), size: File.size(path) } end
index_for_src(src)
click to toggle source
# File lib/s3_index.rb, line 72 def index_for_src(src) Index.where(origin_url: src).first_or_initialize end
logger()
click to toggle source
# File lib/s3_index.rb, line 23 def logger @logger ||= ActiveRecord::Base.logger end
logger=(logger)
click to toggle source
# File lib/s3_index.rb, line 27 def logger=(logger) @logger = logger end
method_missing(meth, *args, &block)
click to toggle source
Let the model handle everything else
Calls superclass method
# File lib/s3_index.rb, line 104 def method_missing(meth, *args, &block) super unless Index.respond_to?(meth) Index.public_send(meth, *args, &block) end
new_client()
click to toggle source
# File lib/s3_index.rb, line 89 def new_client credentials = Aws::SharedCredentials.new(profile_name: 'default') client = Aws::S3::Client.new( credentials: credentials, http_wire_trace: $DEBUG, logger: logger ) Aws::S3::Resource.new(client: client) end
root()
click to toggle source
Use like `Rails.root` @return [Pathname]
# File lib/s3_index.rb, line 19 def root @root ||= Pathname(File.expand_path('../..', __FILE__)) end
upload!(s3: default_client, bucket:, src:, dst: src)
click to toggle source
Upload file to s3 bucket and register it to the Index
model.
# File lib/s3_index.rb, line 32 def upload!(s3: default_client, bucket:, src:, dst: src) obj = s3.bucket(bucket).object(dst) index = index_for_src(src) index_attrs = index_attributes_for(src) # nothing to do, file is registered and the same as before return index if index.md5 == index_attrs[:md5] # try to get the data to s3 first, then save in index. # order is important, we don't want to make the row until # S3 gets the file. obj.upload_file( src, content_type: index_attrs[:content_type], content_length: index_attrs[:size] ) index.s3_url = obj.public_url index.s3_bucket = obj.bucket_name index.update!(index_attrs) index end