class S3restful::S3::Item

Constants

REQUIRED_FIELDS
VALID_HEADERS

Attributes

aws_id[RW]
bucket[RW]
options[RW]

Public Class Methods

new(bucket, aws_id, options = {}) click to toggle source
# File lib/s3restful/s3/item.rb, line 14
def initialize(bucket, aws_id, options = {})
  @options = {
    :timeout => 10,
    :server => 's3.amazonaws.com',
    :protocol => 'https',
    :aws_access_key_id => nil,
    :aws_secret_access_key => nil,
    :retry_count => 4,
    :permissions => 'private',
    :ssl => S3restful::S3.ssl_options
  }.update(symbolize_keys(options))
  assert_valid_keys(options, :timeout, :server, :port, :protocol, :aws_access_key_id, :aws_secret_access_key, :retry_count, :permissions, :ssl)
  @aws_id = aws_id.to_s
  @bucket = bucket.to_s

  validate
end

Public Instance Methods

aget(request_options = {}, &blk) click to toggle source
# File lib/s3restful/s3/item.rb, line 46
def aget(request_options = {}, &blk)
  headers = needs_to_sign? ? aws.sign("GET", path) : {}
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers)
  S3restful::S3::Request.new(:aget, url, {:ssl => options[:ssl]}.update(request_options)).execute
end
delete(request_options = {}, &blk) click to toggle source
# File lib/s3restful/s3/item.rb, line 67
def delete(request_options = {}, &blk)
  headers = needs_to_sign? ? aws.sign("DELETE", path, {'url' => path}) : {}
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers)
  S3restful::S3::Request.new(:delete, url, {:ssl => options[:ssl]}.update(request_options)).execute
end
get(request_options = {}, &blk) click to toggle source
# File lib/s3restful/s3/item.rb, line 39
def get(request_options = {}, &blk)
  headers = needs_to_sign? ? aws.sign("GET", path) : {}
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers)
  S3restful::S3::Request.new(:get, url, {:ssl => options[:ssl]}.update(request_options)).execute
end
head(request_options = {}, &blk) click to toggle source
# File lib/s3restful/s3/item.rb, line 32
def head(request_options = {}, &blk)
  headers = needs_to_sign? ? aws.sign("HEAD", path) : {}
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers)
  S3restful::S3::Request.new(:head, url, {:ssl => options[:ssl]}.update(request_options)).execute
end
path(with_bucket=true) click to toggle source
# File lib/s3restful/s3/item.rb, line 84
def path(with_bucket=true)
  with_bucket ? "/#{bucket}/#{CGI::escape(aws_id)}" : "/#{CGI::escape(aws_id)}"
end
put(data, request_options = {}, &blk) click to toggle source
# File lib/s3restful/s3/item.rb, line 53
def put(data, request_options = {}, &blk)
  headers = construct_aws_headers('PUT', request_options.delete(:headers) || {})
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers, :data => data)
  S3restful::S3::Request.new(:put, url, {:ssl => options[:ssl]}.update(request_options)).execute
end
server() click to toggle source
# File lib/s3restful/s3/item.rb, line 79
def server
  return options[:server] if options[:port]
  dns_bucket? ? "#{bucket}.#{options[:server]}" : options[:server]
end
store(file_path, request_options = {}, &blk) click to toggle source
# File lib/s3restful/s3/item.rb, line 60
def store(file_path, request_options = {}, &blk)
  headers = construct_aws_headers('PUT', request_options.delete(:headers) || {})
  request_options[:on_success] = blk if blk
  request_options.update(:headers => headers, :file => file_path)
  S3restful::S3::Request.new(:put, url, {:ssl => options[:ssl]}.update(request_options)).execute
end
url() click to toggle source
# File lib/s3restful/s3/item.rb, line 74
def url
  uri = options[:port] ? path : path(!dns_bucket?)
  URI::Generic.new(options[:protocol], nil, server, port, nil, uri, nil, nil, nil).to_s
end

Protected Instance Methods

aws() click to toggle source
# File lib/s3restful/s3/item.rb, line 118
def aws
  @aws ||= S3restful::AWS.new(options[:aws_access_key_id], options[:aws_secret_access_key])
end
construct_aws_headers(http_method, headers = {}) click to toggle source
# File lib/s3restful/s3/item.rb, line 122
def construct_aws_headers(http_method, headers = {})
  unless headers.keys.all?{|header| VALID_HEADERS.include?(header) || header.to_s.match(/\Ax-amz-/) }
    raise ArgumentError, "invalid headers. All headers must either one of #{VALID_HEADERS} or start with 'x-amz-'"
  end

  permissions = options[:permissions] != 'private' ? {'x-amz-acl' => options[:permissions] } : {}
  headers.update(permissions)
  headers.update({'url' => path})

  headers = needs_to_sign? ? aws.sign(http_method, path, headers) : headers
end
dns_bucket?() click to toggle source
# File lib/s3restful/s3/item.rb, line 94
def dns_bucket?
  # http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?BucketRestrictions.html
  return false unless (3..63) === bucket.size
  bucket.split('.').each do |component|
    return false unless component[/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/]
  end
  true
end
needs_to_sign?() click to toggle source
# File lib/s3restful/s3/item.rb, line 90
def needs_to_sign?
  present?(options[:aws_access_key_id])
end
port() click to toggle source
# File lib/s3restful/s3/item.rb, line 103
def port
  options[:port] || (options[:protocol].to_s == 'https' ? 443 : 80)
end
validate() click to toggle source
# File lib/s3restful/s3/item.rb, line 107
def validate
  raise ArgumentError, "need a bucket name" unless present?(bucket)
  raise ArgumentError, "need a AWS Key" unless present?(aws_id)

  REQUIRED_FIELDS.each do |field|
    raise ArgumentError, "need field #{field}" unless present?(options[field])
  end

  raise ArgumentError, "unknown protocoll #{options[:protocol]}" unless ['http', 'https'].include?(options[:protocol])
end