class S3::Service

Attributes

access_key_id[R]
host[R]
proxy[R]
secret_access_key[R]
use_ssl[R]
use_vhost[R]

Public Class Methods

new(options) click to toggle source

Creates new service.

Options

  • :access_key_id - Access key id (REQUIRED)

  • :secret_access_key - Secret access key (REQUIRED)

  • :use_ssl - Use https or http protocol (false by default)

  • :use_vhost - Use bucket.s3.amazonaws.com or s3.amazonaws.com/bucket (true by default)

  • :debug - Display debug information on the STDOUT (false by default)

  • :timeout - Timeout to use by the Net::HTTP object (60 by default)

# File lib/s3/service.rb, line 27
def initialize(options)
  # The keys for these required options might exist in the options hash, but
  # they might be set to something like `nil`. If this is the case, we want
  # to fail early.
  fail ArgumentError, 'Missing :access_key_id.' unless options[:access_key_id]
  fail ArgumentError, 'Missing :secret_access_key.' unless options[:secret_access_key]

  @access_key_id = options.fetch(:access_key_id)
  @secret_access_key = options.fetch(:secret_access_key)
  @host = options.fetch(:host)
  @use_ssl = options.fetch(:use_ssl, false)
  @use_vhost = options.fetch(:use_vhost, true)
  @timeout = options.fetch(:timeout, 60)
  @debug = options.fetch(:debug, false)

  fail ArgumentError, 'Missing proxy settings. Must specify at least :host.' if options[:proxy] && !options[:proxy][:host]
  @proxy = options.fetch(:proxy, nil)
end

Public Instance Methods

==(other) click to toggle source

Compares service to other, by access_key_id and secret_access_key

# File lib/s3/service.rb, line 10
def ==(other)
  access_key_id == other.access_key_id && secret_access_key == other.secret_access_key
end
auth_sign() click to toggle source

Returns the signature for POST operations done externally via javascript

# File lib/s3/service.rb, line 60
def auth_sign
  service_request(:post, :use_authsign => true)
end
bucket(name) click to toggle source

Returns the bucket with the given name. Does not check whether the bucket exists. But also does not issue any HTTP requests, so it's much faster than buckets.find

# File lib/s3/service.rb, line 55
def bucket(name)
  Bucket.send(:new, self, name)
end
buckets() click to toggle source

Returns all buckets in the service and caches the result (see reload)

# File lib/s3/service.rb, line 48
def buckets
  Proxy.new(-> { list_all_my_buckets }, owner: self, extend: BucketsExtension)
end
port() click to toggle source

Returns 443 or 80, depends on :use_ssl value from initializer

# File lib/s3/service.rb, line 72
def port
  use_ssl ? 443 : 80
end
protocol() click to toggle source

Returns “http://” or “https://”, depends on :use_ssl value from initializer

# File lib/s3/service.rb, line 66
def protocol
  use_ssl ? 'https://' : 'http://'
end

Private Instance Methods

connection() click to toggle source
# File lib/s3/service.rb, line 92
def connection
  return @connection if defined?(@connection)
  @connection = Connection.new(access_key_id: @access_key_id,
                               secret_access_key: @secret_access_key,
                               host: @host,
                               use_ssl: @use_ssl,
                               timeout: @timeout,
                               debug: @debug,
                               proxy: @proxy)
end
list_all_my_buckets() click to toggle source
# File lib/s3/service.rb, line 82
def list_all_my_buckets
  response = service_request(:get)
  names = parse_list_all_my_buckets_result(response.body)
  names.map { |name| Bucket.send(:new, self, name) }
end
service_request(method, options = {}) click to toggle source
# File lib/s3/service.rb, line 88
def service_request(method, options = {})
  connection.request(method, options.merge(path: "/#{options[:path]}"))
end