class Attachs::Storages::S3

Public Instance Methods

copy(current_path, new_path) click to toggle source
# File lib/attachs/storages/s3.rb, line 38
def copy(current_path, new_path)
  Rails.logger.info "Copying: #{current_path} => #{new_path}"
  bucket.object(current_path).copy_to bucket.object(new_path), acl: 'public-read'
end
delete(path) click to toggle source
# File lib/attachs/storages/s3.rb, line 48
def delete(path)
  Rails.logger.info "Deleting: #{path}"
  bucket.object(path).delete
end
exists?(path) click to toggle source
# File lib/attachs/storages/s3.rb, line 53
def exists?(path)
  bucket.object(path).exists?
end
find_each() { |key| ... } click to toggle source
# File lib/attachs/storages/s3.rb, line 57
def find_each
  bucket.objects.each do |object|
    yield object.key
  end
end
get(path) click to toggle source
# File lib/attachs/storages/s3.rb, line 27
def get(path)
  file = build_tempfile(path)
  file.binmode
  bucket.object(path).get do |chunk|
    file.write chunk
  end
  file.rewind
  file.close
  file
end
move(current_path, new_path) click to toggle source
# File lib/attachs/storages/s3.rb, line 43
def move(current_path, new_path)
  Rails.logger.info "Moving: #{current_path} => #{new_path}"
  bucket.object(current_path).move_to bucket.object(new_path)
end
process(file, paths, options) click to toggle source
# File lib/attachs/storages/s3.rb, line 14
def process(file, paths, options)
  if processable?(file.path)
    processor = build_processor(file.path)
    paths.each do |style, path|
      tmp = build_tempfile(path)
      processor.process tmp.path, options[style]
      upload tmp.path, path
    end
  else
    upload file.path, paths[:original]
  end
end
url(path) click to toggle source
# File lib/attachs/storages/s3.rb, line 5
def url(path)
  base_url = Attachs.configuration.base_url
  if base_url.present?
    Pathname.new(base_url).join(path).to_s
  else
    bucket.object(path).public_url
  end
end

Private Instance Methods

bucket() click to toggle source
# File lib/attachs/storages/s3.rb, line 65
def bucket
  @bucket ||= begin
    require 'aws-sdk'
    credentials = Aws::Credentials.new(
      Rails.application.secrets.aws_access_key_id,
      Rails.application.secrets.aws_secret_access_key
    )
    Aws.config.update(
      region: Attachs.configuration.region,
      credentials: credentials
    )
    Aws::S3::Resource.new.bucket Attachs.configuration.bucket
  end
end
build_tempfile(path) click to toggle source
# File lib/attachs/storages/s3.rb, line 80
def build_tempfile(path)
  extension = File.extname(path)
  Tempfile.new ['s3', extension]
end
upload(local_path, remote_path) click to toggle source
# File lib/attachs/storages/s3.rb, line 85
def upload(local_path, remote_path)
  Rails.logger.info "Uploading: #{local_path} => #{remote_path}"
  bucket.object(remote_path).upload_file(
    local_path,
    acl: 'public-read',
    content_type: detect_content_type(remote_path),
    cache_control: 'max-age=315360000, public',
    expires: Time.parse('31 Dec 2037 23:55:55 GMT').httpdate
  )
end