class S3DataPacker::Bucket
Attributes
bucket_name[R]
path[R]
Public Class Methods
new(opts = {})
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 5 def initialize opts = {} @bucket_name = opts[:bucket_name] @credentials = opts[:credentials] @region = opts[:region] @path = opts[:path] end
Public Instance Methods
credentials()
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 12 def credentials @credentials ||= S3DataPacker.config.default_s3_credentials end
download(key)
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 34 def download(key) data = request! { object(key).get } logger.warn "missing key #{key}" unless data return nil unless data data.body.read end
each_key() { |key| ... }
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 24 def each_key &block bucket.objects(prefix: path).each do |item| yield item.key end end
exist?(key)
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 30 def exist?(key) request! { object(key).exists? } end
logger()
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 20 def logger @logger ||= S3DataPacker.logger end
region()
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 16 def region @region ||= S3DataPacker.config.s3_region end
upload(file, opts={})
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 41 def upload(file, opts={}) raise ArgumentError, 'File does not exist' unless File.exist?(file) key = "#{path}/#{File.basename(file)}" raise ArgumentError, "File #{File.basename(file)} already exists in target location" if exist?(key) metadata = opts metadata[:content_type] ||= file_mime_type(file) metadata[:content_disposition] ||= 'attachement' request! { object(key).upload_file(file, metadata) } logger.info "Uploaded #{file} to s3://#{bucket_name}/#{key}" end
Private Instance Methods
bucket()
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 79 def bucket @bucket ||= resource.bucket(bucket_name) end
file_mime_type(file)
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 66 def file_mime_type(file) begin MIME::Types.type_for(file).first.content_type rescue StandardError logger.error "Could not guess MIME type of #{file}" return nil end end
object(key)
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 75 def object(key) bucket.object(key) end
request!() { || ... }
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 54 def request! &block begin yield rescue Aws::S3::Errors::InternalError logger.warn "Aws::S3::Errors::InternalError, retrying in 1 second" sleep(1) retry rescue Aws::S3::Errors::NoSuchKey return nil end end
resource()
click to toggle source
# File lib/s3_data_packer/bucket.rb, line 83 def resource @resource ||= ::Aws::S3::Resource.new(region: region, credentials: credentials) end