Class: S3DataPacker::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_data_packer/bucket.rb

Direct Known Subclasses

Sources::S3Bucket, Targets::S3Bucket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Bucket

Returns a new instance of Bucket.



5
6
7
8
9
10
# 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

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



3
4
5
# File 'lib/s3_data_packer/bucket.rb', line 3

def bucket_name
  @bucket_name
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/s3_data_packer/bucket.rb', line 3

def path
  @path
end

Instance Method Details

#credentialsObject



12
13
14
# File 'lib/s3_data_packer/bucket.rb', line 12

def credentials
  @credentials ||= S3DataPacker.config.default_s3_credentials
end

#download(key) ⇒ Object



34
35
36
37
38
39
# 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(&block) ⇒ Object



24
25
26
27
28
# 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) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/s3_data_packer/bucket.rb', line 30

def exist?(key)
  request! { object(key).exists? }
end

#loggerObject



20
21
22
# File 'lib/s3_data_packer/bucket.rb', line 20

def logger
  @logger ||= S3DataPacker.logger
end

#regionObject



16
17
18
# File 'lib/s3_data_packer/bucket.rb', line 16

def region
  @region ||= S3DataPacker.config.s3_region
end

#upload(file, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
# 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)
   = opts
  [:content_type] ||= file_mime_type(file)
  [:content_disposition] ||= 'attachement'
  request! { object(key).upload_file(file, ) }
  logger.info "Uploaded #{file} to s3://#{bucket_name}/#{key}"
end