class Filer::S3

Public Class Methods

new(key, secret, bucket_name) click to toggle source
# File lib/filer/s3.rb, line 7
def initialize(key, secret, bucket_name)
  @key, @secret, @bucket_name = key, secret, bucket_name
end

Public Instance Methods

bucket() click to toggle source
# File lib/filer/s3.rb, line 20
def bucket
  return @bucket if @bucket 
  bucket = s3.buckets[@bucket_name]
  unless bucket.location_constraint == s3.config.region
    @s3 = nil
    @s3 = AWS::S3.new(s3_init_params.
      merge(region: bucket.location_constraint))
    bucket = s3.buckets[@bucket_name]
  end
  @bucket = bucket
end
open_file(key) click to toggle source
# File lib/filer/s3.rb, line 49
def open_file(key)
  o = bucket.objects[key]
  filename = File.basename(key)
  ext = File.extname(key)
  t = Tempfile.new([filename, ext])
  o.read do |ch| t.write(ch) end
  t.close
  `open #{t.path}`
end
put_file(key, path) click to toggle source
# File lib/filer/s3.rb, line 44
def put_file(key, path)
  o = bucket.objects[key]
  o.write(Pathname.new(path), server_side_encryption: :aes256)
end
s3() click to toggle source
# File lib/filer/s3.rb, line 16
def s3
  @s3 ||= AWS::S3.new(s3_init_params)
end
s3_init_params() click to toggle source
# File lib/filer/s3.rb, line 11
def s3_init_params
  { access_key_id: @key, secret_access_key: @secret,
    server_side_encryption: :aes256 }
end
s3_key(source_dir, path) click to toggle source
# File lib/filer/s3.rb, line 32
def s3_key(source_dir, path)
  filename = File.basename(path, '.*')
  ext = File.extname(path)
  keypath = path.gsub(source_dir, "").
          gsub("#{filename}#{ext}", "").
          gsub(/^\//, "")
  y = Time.now.strftime("%Y")
  m = Time.now.strftime("%m")
  d = Time.now.strftime("%d")
  "#{keypath}#{filename}-#{y}-#{m}-#{d}#{ext}"
end