Class: S3::Service

Inherits:
Object
  • Object
show all
Includes:
Proxies, Parser
Defined in:
lib/s3/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#parse_acl, #parse_copy_object_result, #parse_error, #parse_is_truncated, #parse_list_all_my_buckets_result, #parse_list_bucket_result, #parse_location_constraint, #rexml_document

Constructor Details

#initialize(options) ⇒ Service

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)

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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.
  raise ArgumentError, "Missing :access_key_id." if !options[:access_key_id]
  raise ArgumentError, "Missing :secret_access_key." if !options[:secret_access_key]

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

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

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



6
7
8
# File 'lib/s3/service.rb', line 6

def access_key_id
  @access_key_id
end

#proxyObject (readonly)

Returns the value of attribute proxy.



6
7
8
# File 'lib/s3/service.rb', line 6

def proxy
  @proxy
end

#secret_access_keyObject (readonly)

Returns the value of attribute secret_access_key.



6
7
8
# File 'lib/s3/service.rb', line 6

def secret_access_key
  @secret_access_key
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



6
7
8
# File 'lib/s3/service.rb', line 6

def use_ssl
  @use_ssl
end

#use_vhostObject (readonly)

Returns the value of attribute use_vhost.



6
7
8
# File 'lib/s3/service.rb', line 6

def use_vhost
  @use_vhost
end

Instance Method Details

#==(other) ⇒ Object

Compares service to other, by access_key_id and secret_access_key



10
11
12
# File 'lib/s3/service.rb', line 10

def ==(other)
  self.access_key_id == other.access_key_id and self.secret_access_key == other.secret_access_key
end

#bucket(name) ⇒ Object

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



54
55
56
# File 'lib/s3/service.rb', line 54

def bucket(name)
  Bucket.send(:new, self, name)
end

#bucketsObject

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



47
48
49
# File 'lib/s3/service.rb', line 47

def buckets
  Proxy.new(lambda { list_all_my_buckets }, :owner => self, :extend => BucketsExtension)
end

#inspectObject

:nodoc:



70
71
72
# File 'lib/s3/service.rb', line 70

def inspect #:nodoc:
  "#<#{self.class}:#@access_key_id>"
end

#portObject

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



66
67
68
# File 'lib/s3/service.rb', line 66

def port
  S3.port || (use_ssl ? 443 : 80)
end

#protocolObject

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



60
61
62
# File 'lib/s3/service.rb', line 60

def protocol
  use_ssl ? "https://" : "http://"
end