Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Upload an Object Using the AWS SDK for Ruby

The following tasks guide you through using the Ruby classes to upload a file. The API provides provides a #write method that can take options that you can use to specify how to upload your data.

Uploading Objects

1

Create an instance of the AWS::S3 class by providing your AWS credentials.

2

Use the AWS::S3::S3Object#write method which takes a data parameter and options hash which allow you to upload data from a file, or a stream.


The following Ruby code sample demonstrates the preceding tasks and uses the options hash :file to specify the path to the file to upload.

s3 = AWS::S3.new

# Upload a file.
key = File.basename(file_name)
s3.buckets[bucket_name].objects[key].write(:file => file_name)

Example

The following Ruby script example uploads a file to an Amazon S3 bucket. For instructions about how to create and test a working sample, see Using the AWS SDK for Ruby.

#!/usr/bin/env ruby

require 'rubygems'
require 'aws-sdk'

AWS.config(
  :access_key_id => '*** Provide your access key ***', 
  :secret_access_key => '*** Provide your secret key ***'
)

bucket_name = '*** Provide bucket name ***'
file_name = '*** Provide file name ****'

# Get an instance of the S3 interface.
s3 = AWS::S3.new

# Upload a file.
key = File.basename(file_name)
s3.buckets[bucket_name].objects[key].write(:file => file_name)
puts "Uploading file #{file_name} to bucket #{bucket_name}."