module Omnibus::S3Helpers::InstanceMethods

Private Instance Methods

bucket() click to toggle source

The bucket where the objects live.

@return [Aws::S3::Bucket]

# File lib/omnibus/s3_helpers.rb, line 107
def bucket
  @s3_bucket ||= begin
                   bucket = client.bucket(s3_configuration[:bucket_name])
                   unless bucket.exists?
                     bucket_config = if s3_configuration[:region] == "us-east-1"
                                       nil
                                     else
                                       {
                                         location_constraint: s3_configuration[:region],
                                       }
                                     end
                     bucket.create(create_bucket_configuration: bucket_config)
                   end
                   bucket
                 end
end
client() click to toggle source

The client to connect to S3 with.

@return [Aws::S3::Resource]

# File lib/omnibus/s3_helpers.rb, line 55
def client
  Aws.config.update(region: s3_configuration[:region])

  @s3_client ||= Aws::S3::Resource.new(resource_params)
end
get_credentials() click to toggle source

Create credentials object based on AWS IAM role arn, credential profile or access key parameters for use by the client object.

@return [Aws::SharedCredentials, Aws::Credentials]

# File lib/omnibus/s3_helpers.rb, line 89
def get_credentials
  if s3_configuration[:iam_role_arn]
    Aws::AssumeRoleCredentials.new(role_arn: s3_configuration[:iam_role_arn], role_session_name: "omnibus-assume-role-s3-access")
  elsif s3_configuration[:profile]
    Aws::SharedCredentials.new(profile_name: s3_configuration[:profile])
  elsif s3_configuration[:access_key_id] && s3_configuration[:secret_access_key]
    Aws::Credentials.new(s3_configuration[:access_key_id], s3_configuration[:secret_access_key])
  else
    # No credentials specified, only public data will be retrievable
    Aws::Credentials.new(nil, nil)
  end
end
resource_params() click to toggle source

S3 Resource Parameters

@return [Hash]

# File lib/omnibus/s3_helpers.rb, line 66
def resource_params
  params = {
    use_accelerate_endpoint: s3_configuration[:use_accelerate_endpoint],
    force_path_style: s3_configuration[:force_path_style],
    credentials: get_credentials,
  }

  if s3_configuration[:use_accelerate_endpoint]
    params[:endpoint] = "https://s3-accelerate.amazonaws.com"
  end

  if s3_configuration[:endpoint]
    params[:endpoint] = s3_configuration[:endpoint]
  end

  params
end
s3_configuration() click to toggle source

Returns the configuration for S3. You must provide keys :region,

@example

{
  region:                   'us-east-1',
  bucket_name:              Config.s3_bucket,
  endpoint:                 Config.s3_endpoint,
  use_accelerate_endpoint:  Config.s3_accelerate
  force_path_style:         Config.s3_force_path_style
}

@return [Hash<String, String>]

# File lib/omnibus/s3_helpers.rb, line 46
def s3_configuration
  raise "You must override s3_configuration"
end
store_object(key, content, content_md5, acl) click to toggle source

Store an object at the specified key

@param [String] key @param [File, String] content @param [String] content_md5 @param [String] acl

@return [Aws::S3::Object]

# File lib/omnibus/s3_helpers.rb, line 134
def store_object(key, content, content_md5, acl)
  bucket.put_object({
    key: key,
    body: content,
    content_md5: to_base64_digest(content_md5),
    acl: acl,
  })
end
to_base64_digest(content_md5) click to toggle source

Convert a hex digest into a base64 hex digest

For example: to_base64_digest(‘c3b5247592ce694f7097873aa07d66fe’) => ‘w7UkdZLOaU9wl4c6oH1m/g==’

@param [String] content_md5

@return [String]

# File lib/omnibus/s3_helpers.rb, line 153
def to_base64_digest(content_md5)
  if content_md5
    md5_digest = content_md5.unpack("a2" * 16).collect { |i| i.hex.chr }.join
    Base64.encode64(md5_digest).strip
  end
end