Class: RedshiftConnector::S3Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/redshift_connector/s3_bucket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region: nil, bucket:, prefix: nil, access_key_id: nil, secret_access_key: nil, iam_role: nil) ⇒ S3Bucket

Returns a new instance of S3Bucket.



24
25
26
27
28
29
30
31
# File 'lib/redshift_connector/s3_bucket.rb', line 24

def initialize(region: nil, bucket:, prefix: nil, access_key_id: nil, secret_access_key: nil, iam_role: nil)
  @region = region
  @name = bucket
  @prefix = prefix
  @access_key_id = access_key_id
  @secret_access_key = secret_access_key
  @iam_role = iam_role
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/redshift_connector/s3_bucket.rb', line 33

def name
  @name
end

#prefixObject (readonly)

Returns the value of attribute prefix.



34
35
36
# File 'lib/redshift_connector/s3_bucket.rb', line 34

def prefix
  @prefix
end

Class Method Details

.add(name, default: false, **params) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/redshift_connector/s3_bucket.rb', line 8

def S3Bucket.add(name, default: false, **params)
  instance = new(**params)
  @buckets[name.to_s] = instance
  if !@default or default
    @default = instance
  end
end

.defaultObject



16
17
18
# File 'lib/redshift_connector/s3_bucket.rb', line 16

def S3Bucket.default
  @default or raise ArgumentError, "no default S3 bucket configured"
end

.get(name) ⇒ Object



20
21
22
# File 'lib/redshift_connector/s3_bucket.rb', line 20

def S3Bucket.get(name)
  @buckets[name.to_s] or raise ArgumentError, "no such S3 bucket configured: #{name.inspect}"
end

Instance Method Details

#bucketObject



47
48
49
50
51
52
# File 'lib/redshift_connector/s3_bucket.rb', line 47

def bucket
  @bucket ||= begin
    resource = Aws::S3::Resource.new(client: client)
    resource.bucket(@name)
  end
end

#clientObject



40
41
42
43
44
45
# File 'lib/redshift_connector/s3_bucket.rb', line 40

def client
  @client ||= begin
    args = { region: @region, access_key_id: @access_key_id, secret_access_key: @secret_access_key }.reject {|k, v| v.nil? }
    Aws::S3::Client.new(**args)
  end
end

#credential_stringObject



66
67
68
69
70
71
72
73
74
# File 'lib/redshift_connector/s3_bucket.rb', line 66

def credential_string
  if @iam_role
    "aws_iam_role=#{@iam_role}"
  elsif @access_key_id
    "aws_access_key_id=#{@access_key_id};aws_secret_access_key=#{@secret_access_key}"
  else
    raise ArgumentError, "no credential given for Redshift S3 access"
  end
end

#delete_objects(keys) ⇒ Object



62
63
64
# File 'lib/redshift_connector/s3_bucket.rb', line 62

def delete_objects(keys)
  bucket.delete_objects(delete: {objects: keys.map {|k| {key: k} }})
end

#object(key) ⇒ Object



54
55
56
# File 'lib/redshift_connector/s3_bucket.rb', line 54

def object(key)
  bucket.object(key)
end

#objects(prefix:) ⇒ Object



58
59
60
# File 'lib/redshift_connector/s3_bucket.rb', line 58

def objects(prefix:)
  bucket.objects(prefix: prefix)
end

#urlObject



36
37
38
# File 'lib/redshift_connector/s3_bucket.rb', line 36

def url
  "s3://#{@bucket.name}/#{@prefix}/"
end