Class: DataDuck::S3Object

Inherits:
Object
  • Object
show all
Defined in:
lib/dataduck/s3_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, contents, aws_key, aws_secret, bucket, region, options = {}) ⇒ S3Object

Returns a new instance of S3Object.



6
7
8
9
10
11
12
13
14
# File 'lib/dataduck/s3_object.rb', line 6

def initialize(path, contents, aws_key, aws_secret, bucket, region, options={})
  @path = path
  @contents = contents
  @options = options
  @aws_key = aws_key
  @aws_secret = aws_secret
  @bucket = bucket
  @region = region
end

Class Method Details

.max_retriesObject



71
72
73
# File 'lib/dataduck/s3_object.rb', line 71

def self.max_retries
  3
end

.regionsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dataduck/s3_object.rb', line 75

def self.regions
  [
      { name: 'US Standard - N. Virginia', region: 'us-east-1' },
      { name: 'US West - N. California', region: 'us-west-1' },
      { name: 'US West - Oregon', region: 'us-west-2' },
      { name: 'EU - Ireland', region: 'eu-west-1' },
      { name: 'EU - Frankfurt', region: 'eu-central-1' },
      { name: 'Asia Pacific - Singapore', region: 'ap-southeast-1' },
      { name: 'Asia Pacific - Sydney', region: 'ap-southeast-2' },
      { name: 'Asia Pacific - Tokyo', region: 'ap-northeast-1' },
      { name: 'South America - Sao Paulo', region: 'sa-east-1' },
  ]
end

Instance Method Details

#delete!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dataduck/s3_object.rb', line 16

def delete!
  Logs.debug("Deleting S3 file #{ self.s3_path }...")

  s3 = Aws::S3::Client.new(
      region: @region,
      access_key_id: @aws_key,
      secret_access_key: @aws_secret,
  )

  s3.delete_object({
      bucket: @bucket,
      key: self.full_path,
  })
end

#full_pathObject



63
64
65
# File 'lib/dataduck/s3_object.rb', line 63

def full_path
  'dataduck/' + @path
end

#s3_pathObject



67
68
69
# File 'lib/dataduck/s3_object.rb', line 67

def s3_path
  "s3://#{ @bucket }/#{ full_path }"
end

#upload!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dataduck/s3_object.rb', line 31

def upload!
  Logs.debug("Uploading S3 file #{ self.s3_path }...")

  s3 = Aws::S3::Client.new(
      region: @region,
      access_key_id: @aws_key,
      secret_access_key: @aws_secret,
  )

  attempts = 0

  while attempts <= S3Object.max_retries
    attempts += 1
    put_hash = @options.merge({
            acl: 'private',
            bucket: @bucket,
            body: @contents,
            key: self.full_path,
            server_side_encryption: 'AES256',
        })
    begin
      response = s3.put_object(put_hash)
    rescue => e
      if attempts == S3Object.max_retries
        throw e
      end
    end
  end

  response
end