Class: Heirloom::AWS::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/heirloom/aws/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ S3

Returns a new instance of S3.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/heirloom/aws/s3.rb', line 7

def initialize(args)
  @config = args[:config]
  @region = args[:region]
  @logger = @config.logger

  fog_args = { :region   => @region, :provider => 'AWS' }

  if @config.use_iam_profile
    fog_args[:use_iam_profile] = true
  else
    fog_args[:aws_access_key_id]     = @config.access_key
    fog_args[:aws_secret_access_key] = @config.secret_key
  end

  @s3 = Fog::Storage.new fog_args
end

Instance Method Details

#bucket_empty?(bucket) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/heirloom/aws/s3.rb', line 38

def bucket_empty?(bucket)
  get_bucket_object_versions(bucket)["Versions"].count == 0
end

#bucket_exists?(bucket) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/heirloom/aws/s3.rb', line 32

def bucket_exists?(bucket)
  get_bucket(bucket) != nil
rescue Excon::Errors::Forbidden
  false
end

#bucket_exists_in_another_region?(bucket) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/heirloom/aws/s3.rb', line 42

def bucket_exists_in_another_region?(bucket)
  if bucket_exists? bucket
    get_bucket(bucket).location != @region
  else
    false
  end
rescue Excon::Errors::Forbidden
  false
end

#bucket_name_available?(bucket) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
# File 'lib/heirloom/aws/s3.rb', line 60

def bucket_name_available?(bucket)
  @logger.info "Checking for #{bucket} availability in #{@region}."

  if bucket_owned_by_another_account?(bucket) ||
     bucket_exists_in_another_region?(bucket)
     false
  else
    true
  end
end

#bucket_owned_by_another_account?(bucket) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/heirloom/aws/s3.rb', line 52

def bucket_owned_by_another_account?(bucket)
  get_bucket bucket
  false
rescue Excon::Errors::Forbidden
  @logger.warn "#{bucket} owned by another account."
  true
end

#delete_bucket(bucket) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/heirloom/aws/s3.rb', line 71

def delete_bucket(bucket)
  if bucket_empty? bucket
    @s3.delete_bucket bucket
  else
    @logger.warn "#{bucket} not empty, not destroying."
    false
  end
rescue Excon::Errors::NotFound
  @logger.info "#{bucket} already destroyed."
  true
end

#delete_object(bucket_name, object_name, options = {}) ⇒ Object



24
25
26
# File 'lib/heirloom/aws/s3.rb', line 24

def delete_object(bucket_name, object_name, options = {})
  @s3.delete_object(bucket_name, object_name, options)
end

#get_bucket(bucket) ⇒ Object



28
29
30
# File 'lib/heirloom/aws/s3.rb', line 28

def get_bucket(bucket)
  @s3.directories.get bucket
end

#get_bucket_object_versions(bucket) ⇒ Object



91
92
93
# File 'lib/heirloom/aws/s3.rb', line 91

def get_bucket_object_versions(bucket)
  @s3.get_bucket_object_versions(bucket).body
end

#get_object(bucket_name, object_name) ⇒ Object



83
84
85
# File 'lib/heirloom/aws/s3.rb', line 83

def get_object(bucket_name, object_name)
  @s3.get_object(bucket_name, object_name).body
end

#get_object_acl(args) ⇒ Object



87
88
89
# File 'lib/heirloom/aws/s3.rb', line 87

def get_object_acl(args)
  @s3.get_object_acl(args[:bucket], args[:object_name]).body
end

#put_bucket(bucket_name, region) ⇒ Object



99
100
101
102
103
104
# File 'lib/heirloom/aws/s3.rb', line 99

def put_bucket(bucket_name, region)
  region = nil if region == 'us-east-1'
  options = { 'LocationConstraint' => region,
              'x-amz-acl'          => 'private' }
  @s3.put_bucket bucket_name, options
end

#put_object_acl(bucket, key, grants) ⇒ Object



95
96
97
# File 'lib/heirloom/aws/s3.rb', line 95

def put_object_acl(bucket, key, grants)
  @s3.put_object_acl(bucket, key, grants)
end