Class: Dev::Aws::S3

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

Overview

Class for performing S3 functions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeS3

Returns a new instance of S3.



9
10
11
# File 'lib/firespring_dev_commands/aws/s3.rb', line 9

def initialize
  @client = nil
end

Instance Attribute Details

#clientObject (readonly)

Create/set a new client if none is present Return the client



15
16
17
# File 'lib/firespring_dev_commands/aws/s3.rb', line 15

def client
  @client
end

Instance Method Details

#cf_bucketObject

Finds the specific name of the appropriate “cf-templates-” bucket to use to upload cloudformation templates to



37
38
39
# File 'lib/firespring_dev_commands/aws/s3.rb', line 37

def cf_bucket
  client.list_buckets.buckets.find { |bucket| bucket.name.match(/^cf-templates-.*-#{Dev::Aws::Credentials.new.logged_in_region}/) }
end

#put(bucket:, key:, filename:, acl: 'private') ⇒ Object

Puts the local filename into the given bucket named as the given key Optionally specify an acl. defaults to ‘private’ Returns the URL of the uploaded file



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/firespring_dev_commands/aws/s3.rb', line 22

def put(bucket:, key:, filename:, acl: 'private')
  begin
    File.open(filename, 'rb') do |file|
      client.put_object(bucket:, key:, body: file, acl:)
    end
  rescue => e
    raise "s3 file upload failed: #{e.message}"
  end

  url = "https://#{bucket}.s3.#{Dev::Aws::Credentials.new.logged_in_region}.amazonaws.com/#{key}"
  LOG.info "Uploaded #{url}"
  url
end