Class: AWS::S3

Inherits:
Object
  • Object
show all
Includes:
Core::ServiceInterface
Defined in:
lib/aws/s3.rb,
lib/aws/s3/tree.rb,
lib/aws/s3/bucket.rb,
lib/aws/s3/client.rb,
lib/aws/s3/errors.rb,
lib/aws/s3/policy.rb,
lib/aws/s3/request.rb,
lib/aws/s3/s3_object.rb,
lib/aws/s3/tree/node.rb,
lib/aws/s3/acl_object.rb,
lib/aws/s3/client/xml.rb,
lib/aws/s3/tree/parent.rb,
lib/aws/s3/data_options.rb,
lib/aws/s3/uploaded_part.rb,
lib/aws/s3/object_version.rb,
lib/aws/s3/tree/leaf_node.rb,
lib/aws/s3/object_metadata.rb,
lib/aws/s3/multipart_upload.rb,
lib/aws/s3/tree/branch_node.rb,
lib/aws/s3/bucket_collection.rb,
lib/aws/s3/object_collection.rb,
lib/aws/s3/access_control_list.rb,
lib/aws/s3/prefixed_collection.rb,
lib/aws/s3/paginated_collection.rb,
lib/aws/s3/tree/child_collection.rb,
lib/aws/s3/object_upload_collection.rb,
lib/aws/s3/uploaded_part_collection.rb,
lib/aws/s3/bucket_version_collection.rb,
lib/aws/s3/object_version_collection.rb,
lib/aws/s3/multipart_upload_collection.rb,
lib/aws/s3/bucket_lifecycle_configuration.rb,
lib/aws/s3/prefix_and_delimiter_collection.rb

Overview

Provides an expressive, object-oriented interface to Amazon S3.

To use Amazon S3 you must first sign up here.

For more information about Amazon S3, see:

Credentials

You can setup default credentials for all AWS services via AWS.config:

AWS.config(
  :access_key_id => 'YOUR_ACCESS_KEY_ID',
  :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')

Or you can set them directly on the S3 interface:

s3 = AWS::S3.new(
  :access_key_id => 'YOUR_ACCESS_KEY_ID',
  :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')

Buckets Keys and Objects

S3 stores objects in buckets.

To create a bucket:

bucket = s3.buckets.create('mybucket')

To get a bucket:

bucket = s3.buckets[:mybucket]
bucket = s3.buckets['mybucket']

Listing buckets:

s3.buckets.each do |bucket|
  puts bucket.name
end

See Bucket and BucketCollection for more information on working with S3 buckets.

Listing Objects

Enumerating objects in a bucket:

bucket.objects.each do |object|
  puts object.key #=> no data is fetched from s3, just a list of keys
end

You can limit the list of objects with a prefix, or explore the objects in a bucket as a tree. See ObjectCollection for more information.

Reading and Writing to S3

Each object in a bucket has a unique key.

photo = s3.buckets['mybucket'].objects['photo.jpg']

Writing to an S3Object:

photo.write(File.read('/some/photo.jpg'))

Reading from an S3Object:

File.open("/some/path/on/disk.jpg", "w") do |f|
  f.write(photo.read)
end

See S3Object for more information on reading and writing to S3.

Defined Under Namespace

Modules: ACLObject, DataOptions, Errors, PaginatedCollection, PrefixAndDelimiterCollection, PrefixedCollection Classes: AccessControlList, Bucket, BucketCollection, BucketLifecycleConfiguration, BucketVersionCollection, Client, MultipartUpload, MultipartUploadCollection, ObjectCollection, ObjectMetadata, ObjectUploadCollection, ObjectVersion, ObjectVersionCollection, Policy, Request, S3Object, Tree, UploadedPart, UploadedPartCollection

Instance Method Summary collapse

Methods included from Core::ServiceInterface

included, #initialize, #inspect

Instance Method Details

#bucketsBucketCollection

Returns a collection that represents all buckets in the account.

Returns:

  • (BucketCollection)

    Returns a collection that represents all buckets in the account.


130
131
132
# File 'lib/aws/s3.rb', line 130

def buckets
  BucketCollection.new(:config => @config)
end