class HealthCheck::S3HealthCheck

Public Class Methods

check() click to toggle source
# File lib/health_check/s3_health_check.rb, line 6
def check
  unless defined?(::Aws)
    raise "Wrong configuration. Missing 'aws-sdk' or 'aws-sdk-s3' gem"
  end
  return create_error 's3', 'Could not connect to aws' if aws_s3_client.nil?
  HealthCheck.buckets.each do |bucket_name, permissions|
    if permissions.nil? # backward compatible
      permissions = [:R, :W, :D]
    end
    permissions.each do |permision|
      begin
      send(permision, bucket_name)
    rescue Exception => e
      raise "bucket:#{bucket_name}, permission:#{permision} - #{e.message}"
    end
    end
  end
  ''
rescue Exception => e
  create_error 's3', e.message
end

Private Class Methods

D(bucket) click to toggle source
# File lib/health_check/s3_health_check.rb, line 54
def D(bucket)
  aws_s3_client.delete_object(bucket: bucket,
                              key: "healthcheck_#{::Rails.application.class.parent_name}")
end
R(bucket) click to toggle source
# File lib/health_check/s3_health_check.rb, line 44
def R(bucket)
  aws_s3_client.list_objects(bucket: bucket)
end
W(bucket) click to toggle source
# File lib/health_check/s3_health_check.rb, line 48
def W(bucket)
  aws_s3_client.put_object(bucket: bucket,
                           key: "healthcheck_#{::Rails.application.class.parent_name}",
                           body: Time.new.to_s)
end
aws_s3_client() click to toggle source
# File lib/health_check/s3_health_check.rb, line 40
def aws_s3_client
  @aws_s3_client ||= configure_client
end
configure_client() click to toggle source

We already assume you are using Rails. Let's also assume you have an initializer created for your Aws config. We will set the region here so you can use an instance profile and simply set the region in your environment.

# File lib/health_check/s3_health_check.rb, line 33
def configure_client
  ::Aws.config[:s3] = { force_path_style: true }
  ::Aws.config[:region] ||= ENV['AWS_REGION'] || ENV['DEFAULT_AWS_REGION']

  ::Aws::S3::Client.new
end