You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

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/tree/node.rb,
lib/aws/s3/cors_rule.rb,
lib/aws/s3/cipher_io.rb,
lib/aws/s3/s3_object.rb,
lib/aws/s3/client/xml.rb,
lib/aws/s3/presign_v4.rb,
lib/aws/s3/acl_object.rb,
lib/aws/s3/acl_options.rb,
lib/aws/s3/tree/parent.rb,
lib/aws/s3/data_options.rb,
lib/aws/s3/uploaded_part.rb,
lib/aws/s3/presigned_post.rb,
lib/aws/s3/tree/leaf_node.rb,
lib/aws/s3/object_version.rb,
lib/aws/s3/object_metadata.rb,
lib/aws/s3/tree/branch_node.rb,
lib/aws/s3/encryption_utils.rb,
lib/aws/s3/region_detection.rb,
lib/aws/s3/multipart_upload.rb,
lib/aws/s3/object_collection.rb,
lib/aws/s3/bucket_collection.rb,
lib/aws/s3/access_control_list.rb,
lib/aws/s3/prefixed_collection.rb,
lib/aws/s3/bucket_region_cache.rb,
lib/aws/s3/paginated_collection.rb,
lib/aws/s3/cors_rule_collection.rb,
lib/aws/s3/bucket_tag_collection.rb,
lib/aws/s3/website_configuration.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

Before you can upload files to S3, you need to create a bucket.

s3 = AWS::S3.new
bucket = s3.buckets.create('my-bucket')

If a bucket already exists, you can get a reference to the bucket.

bucket = s3.buckets['my-bucket'] # no request made

You can also enumerate all buckets in your account.

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

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

Objects

Buckets contain objects. Each object in a bucket has a unique key.

Getting an Object

If the object already exists, you can get a reference to the object.

# makes no request, returns an AWS::S3::S3Object
obj = bucket.objects['key']

Reading and Writing an Object

The example above returns an S3Object. You call S3Object#write and S3Object#read to upload to and download from S3 respectively.

# streaming upload a file to S3
obj.write(Pathname.new('/path/to/file.txt'))

# streaming download from S3 to a file on disk
File.open('file.txt', 'wb') do |file|
  obj.read do |chunk|
     file.write(chunk)
  end
end

Enumerating Objects

You can enumerate objects in your buckets.

# enumerate ALL objects in the bucket (even if the bucket contains
# more than 1k objects)
bucket.objects.each do |obj|
  puts obj.key
end

# enumerate at most 20 objects with the given prefix
bucket.objects.with_prefix('photos/').each(:limit => 20) do |photo|
  puts photo.key
end

See ObjectCollection and S3Object for more information on working with objects.

Defined Under Namespace

Modules: ACLObject, Errors, PrefixedCollection Classes: AccessControlList, Bucket, BucketCollection, BucketLifecycleConfiguration, BucketRegionCache, BucketTagCollection, BucketVersionCollection, CORSRule, CORSRuleCollection, Client, MultipartUpload, MultipartUploadCollection, ObjectCollection, ObjectMetadata, ObjectUploadCollection, ObjectVersion, ObjectVersionCollection, Policy, PresignV4, PresignedPost, S3Object, Tree, UploadedPart, UploadedPartCollection, WebsiteConfiguration

Constant Summary

BUCKET_REGIONS =
BucketRegionCache.new

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.



155
156
157
# File 'lib/aws/s3.rb', line 155

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