Class: S3::AWSAuthConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/s3-ruby.rb

Overview

uses Net::HTTP to interface with S3. note that this interface should only be used for smaller objects, as it does not stream the data. if you were to download a 1gb file, it would require 1gb of memory. also, this class creates a new http connection each time. it would be greatly improved with some connection pooling.

Instance Method Summary collapse

Constructor Details

#initialize(aws_access_key_id, aws_secret_access_key, is_secure = true, server = DEFAULT_HOST, port = ) ⇒ AWSAuthConnection

Returns a new instance of AWSAuthConnection.



131
132
133
134
135
136
137
# File 'lib/s3-ruby.rb', line 131

def initialize(aws_access_key_id, aws_secret_access_key, is_secure=true,
               server=DEFAULT_HOST, port=PORTS_BY_SECURITY[is_secure])
  @aws_access_key_id = aws_access_key_id
  @aws_secret_access_key = aws_secret_access_key
  @http = Net::HTTP.new(server, port)
  @http.use_ssl = is_secure
end

Instance Method Details

#create_bucket(bucket, headers = {}) ⇒ Object



139
140
141
# File 'lib/s3-ruby.rb', line 139

def create_bucket(bucket, headers={})
  return Response.new(make_request('PUT', bucket, headers))
end

#delete(bucket, key, headers = {}) ⇒ Object



169
170
171
# File 'lib/s3-ruby.rb', line 169

def delete(bucket, key, headers={})
  return Response.new(make_request('DELETE', "#{bucket}/#{CGI::escape key}", headers))
end

#delete_bucket(bucket, headers = {}) ⇒ Object



153
154
155
# File 'lib/s3-ruby.rb', line 153

def delete_bucket(bucket, headers={})
  return Response.new(make_request('DELETE', bucket, headers))
end

#get(bucket, key, headers = {}) ⇒ Object



165
166
167
# File 'lib/s3-ruby.rb', line 165

def get(bucket, key, headers={})
  return GetResponse.new(make_request('GET', "#{bucket}/#{CGI::escape key}", headers))
end

#get_acl(bucket, key, headers = {}) ⇒ Object

returns an xml document representing the access control list. this could be parsed into an object.



219
220
221
# File 'lib/s3-ruby.rb', line 219

def get_acl(bucket, key, headers={})
  return GetResponse.new(make_request('GET', "#{bucket}/#{CGI::escape key}?acl", headers))
end

#get_bucket_acl(bucket, headers = {}) ⇒ Object



213
214
215
# File 'lib/s3-ruby.rb', line 213

def get_bucket_acl(bucket, headers={})
  return get_acl(bucket, '', headers)
end

#get_bucket_logging(bucket, headers = {}) ⇒ Object



205
206
207
# File 'lib/s3-ruby.rb', line 205

def get_bucket_logging(bucket, headers={})
  return GetResponse.new(make_request('GET', "#{bucket}?logging", headers))
end

#head(bucket, key, headers = {}) ⇒ Object



173
174
175
# File 'lib/s3-ruby.rb', line 173

def head(bucket, key, headers={})
  return GetResponse.new(make_request('HEAD',"#{bucket}/#{CGI::escape key}", headers))
end

#list_all_my_buckets(headers = {}) ⇒ Object



235
236
237
# File 'lib/s3-ruby.rb', line 235

def list_all_my_buckets(headers={})
  return ListAllMyBucketsResponse.new(make_request('GET', '', headers))
end

#list_bucket(bucket, options = {}, headers = {}) ⇒ Object

takes options :prefix, :marker, :max_keys, and :delimiter



144
145
146
147
148
149
150
151
# File 'lib/s3-ruby.rb', line 144

def list_bucket(bucket, options={}, headers={})
  path = bucket
  if options.size > 0
      path += '?' + options.map { |k, v| "#{k}=#{CGI::escape v.to_s}" }.join('&')
  end

  return ListBucketResponse.new(make_request('GET', path, headers))
end

#list_bucket_contents(bucket) ⇒ Object

a convenience method that returns an array with the list of all the keys in a bucket



193
194
195
# File 'lib/s3-ruby.rb', line 193

def list_bucket_contents(bucket)
  list_bucket(bucket).entries.map { |entry| entry.key }
end

#list_bucketsObject

a convenience method that returns an array with the list of all your bucket names



188
189
190
# File 'lib/s3-ruby.rb', line 188

def list_buckets
  list_all_my_buckets.entries.map { |entry| entry.name }
end

#make_private(bucket, object, headers = {}) ⇒ Object



201
202
203
# File 'lib/s3-ruby.rb', line 201

def make_private(bucket,object,headers={})
  put_acl(bucket,object,'', { 'a-amz-acl' => 'private' }.merge(headers) )
end

#make_public(bucket, object, headers = {}) ⇒ Object



197
198
199
# File 'lib/s3-ruby.rb', line 197

def make_public(bucket,object,headers={})
  put_acl(bucket,object,'', { 'a-amz-acl' => 'public-read' }.merge(headers) )
end

#put(bucket, key, object, headers = {}) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/s3-ruby.rb', line 157

def put(bucket, key, object, headers={})
  object = S3Object.new(object) if not object.instance_of? S3Object

  return Response.new(
    make_request('PUT', "#{bucket}/#{CGI::escape key}", headers, object.data, object.)
  )
end

#put_acl(bucket, key, acl_xml_doc, headers = {}) ⇒ Object

sets the access control policy for the given resource. acl_xml_doc must be a string in the acl xml format.



229
230
231
232
233
# File 'lib/s3-ruby.rb', line 229

def put_acl(bucket, key, acl_xml_doc, headers={})
  return Response.new(
    make_request('PUT', "#{bucket}/#{CGI::escape key}?acl", headers, acl_xml_doc, {})
  )
end

#put_bucket_acl(bucket, acl_xml_doc, headers = {}) ⇒ Object



223
224
225
# File 'lib/s3-ruby.rb', line 223

def put_bucket_acl(bucket, acl_xml_doc, headers={})
  return put_acl(bucket, '', acl_xml_doc, headers)
end

#put_bucket_logging(bucket, logging_xml_doc, headers = {}) ⇒ Object



209
210
211
# File 'lib/s3-ruby.rb', line 209

def put_bucket_logging(bucket, logging_xml_doc, headers={})
  return Response.new(make_request('PUT', "#{bucket}?logging", headers, logging_xml_doc))
end

#put_public(bucket, key, object, headers) ⇒ Object

a convenience method to put an object to S3 publically readbale



178
179
180
181
182
183
184
185
# File 'lib/s3-ruby.rb', line 178

def put_public(bucket,key,object,headers)
  put(
    bucket,
    key,
    object,
    { 'x-amz-acl' => 'public-read'}.merge(headers)
  )
end