Ruby AWS::SDK Examples (aws-sdk gem ~>2)

Settings

You can setup the connection on global way:

  1. Aws.config.update(
  2. endpoint: 'https://objects.dreamhost.com.',
  3. access_key_id: 'my-access-key',
  4. secret_access_key: 'my-secret-key',
  5. force_path_style: true,
  6. region: 'us-east-1'
  7. )

and instantiate a client object:

  1. s3_client = Aws::S3::Client.new

Listing Owned Buckets

This gets a list of buckets that you own. This also prints out the bucket name and creation date of each bucket.

  1. s3_client.list_buckets.buckets.each do |bucket|
  2. puts "#{bucket.name}\t#{bucket.creation_date}"
  3. end

The output will look something like this:

  1. mahbuckat1 2011-04-21T18:05:39.000Z
  2. mahbuckat2 2011-04-21T18:05:48.000Z
  3. mahbuckat3 2011-04-21T18:07:18.000Z

Creating a Bucket

This creates a new bucket called my-new-bucket

  1. s3_client.create_bucket(bucket: 'my-new-bucket')

If you want a private bucket:

acl option accepts: # private, public-read, public-read-write, authenticated-read

  1. s3_client.create_bucket(bucket: 'my-new-bucket', acl: 'private')

Listing a Bucket’s Content

This gets a list of hashes with the contents of each object This also prints out each object’s name, the file size, and last modified date.

  1. s3_client.get_objects(bucket: 'my-new-bucket').contents.each do |object|
  2. puts "#{object.key}\t#{object.size}\t#{object.last-modified}"
  3. end

The output will look something like this if the bucket has some files:

  1. myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
  2. myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

Deleting a Bucket

Note

The Bucket must be empty! Otherwise it won’t work!

  1. s3_client.delete_bucket(bucket: 'my-new-bucket')

Forced Delete for Non-empty Buckets

First, you need to clear the bucket:

  1. Aws::S3::Bucket.new('my-new-bucket', client: s3_client).clear!

after, you can destroy the bucket

  1. s3_client.delete_bucket(bucket: 'my-new-bucket')

Creating an Object

This creates a file hello.txt with the string "Hello World!"

  1. s3_client.put_object(
  2. key: 'hello.txt',
  3. body: 'Hello World!',
  4. bucket: 'my-new-bucket',
  5. content_type: 'text/plain'
  6. )

Change an Object’s ACL

This makes the object hello.txt to be publicly readable, and secret_plans.txt to be private.

  1. s3_client.put_object_acl(bucket: 'my-new-bucket', key: 'hello.txt', acl: 'public-read')
  2. s3_client.put_object_acl(bucket: 'my-new-bucket', key: 'private.txt', acl: 'private')

Download an Object (to a file)

This downloads the object poetry.pdf and saves it in /home/larry/documents/

  1. s3_client.get_object(bucket: 'my-new-bucket', key: 'poetry.pdf', response_target: '/home/larry/documents/poetry.pdf')

Delete an Object

This deletes the object goodbye.txt

  1. s3_client.delete_object(key: 'goodbye.txt', bucket: 'my-new-bucket')

Generate Object Download URLs (signed and unsigned)

This generates an unsigned download URL for hello.txt. This works because we made hello.txt public by setting the ACL above. This then generates a signed download URL for secret_plans.txt that will work for 1 hour. Signed download URLs will work for the time period even if the object is private (when the time period is up, the URL will stop working).

  1. puts Aws::S3::Object.new(
  2. key: 'hello.txt',
  3. bucket_name: 'my-new-bucket',
  4. client: s3_client
  5. ).public_url
  6. puts Aws::S3::Object.new(
  7. key: 'secret_plans.txt',
  8. bucket_name: 'hermes_ceph_gem',
  9. client: s3_client
  10. ).presigned_url(:get, expires_in: 60 * 60)

The output of this will look something like:

  1. http://objects.dreamhost.com/my-bucket-name/hello.txt
  2. http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX

Ruby AWS::S3 Examples (aws-s3 gem)

Creating a Connection

This creates a connection so that you can interact with the server.

  1. AWS::S3::Base.establish_connection!(
  2. :server => 'objects.dreamhost.com',
  3. :use_ssl => true,
  4. :access_key_id => 'my-access-key',
  5. :secret_access_key => 'my-secret-key'
  6. )

Listing Owned Buckets

This gets a list of AWS::S3::Bucket objects that you own. This also prints out the bucket name and creation date of each bucket.

  1. AWS::S3::Service.buckets.each do |bucket|
  2. puts "#{bucket.name}\t#{bucket.creation_date}"
  3. end

The output will look something like this:

  1. mahbuckat1 2011-04-21T18:05:39.000Z
  2. mahbuckat2 2011-04-21T18:05:48.000Z
  3. mahbuckat3 2011-04-21T18:07:18.000Z

Creating a Bucket

This creates a new bucket called my-new-bucket

  1. AWS::S3::Bucket.create('my-new-bucket')

Listing a Bucket’s Content

This gets a list of hashes with the contents of each object This also prints out each object’s name, the file size, and last modified date.

  1. new_bucket = AWS::S3::Bucket.find('my-new-bucket')
  2. new_bucket.each do |object|
  3. puts "#{object.key}\t#{object.about['content-length']}\t#{object.about['last-modified']}"
  4. end

The output will look something like this if the bucket has some files:

  1. myphoto1.jpg 251262 2011-08-08T21:35:48.000Z
  2. myphoto2.jpg 262518 2011-08-08T21:38:01.000Z

Deleting a Bucket

Note

The Bucket must be empty! Otherwise it won’t work!

  1. AWS::S3::Bucket.delete('my-new-bucket')

Forced Delete for Non-empty Buckets

  1. AWS::S3::Bucket.delete('my-new-bucket', :force => true)

Creating an Object

This creates a file hello.txt with the string "Hello World!"

  1. AWS::S3::S3Object.store(
  2. 'hello.txt',
  3. 'Hello World!',
  4. 'my-new-bucket',
  5. :content_type => 'text/plain'
  6. )

Change an Object’s ACL

This makes the object hello.txt to be publicly readable, and secret_plans.txt to be private.

  1. policy = AWS::S3::S3Object.acl('hello.txt', 'my-new-bucket')
  2. policy.grants = [ AWS::S3::ACL::Grant.grant(:public_read) ]
  3. AWS::S3::S3Object.acl('hello.txt', 'my-new-bucket', policy)
  4. policy = AWS::S3::S3Object.acl('secret_plans.txt', 'my-new-bucket')
  5. policy.grants = []
  6. AWS::S3::S3Object.acl('secret_plans.txt', 'my-new-bucket', policy)

Download an Object (to a file)

This downloads the object poetry.pdf and saves it in /home/larry/documents/

  1. open('/home/larry/documents/poetry.pdf', 'w') do |file|
  2. AWS::S3::S3Object.stream('poetry.pdf', 'my-new-bucket') do |chunk|
  3. file.write(chunk)
  4. end
  5. end

Delete an Object

This deletes the object goodbye.txt

  1. AWS::S3::S3Object.delete('goodbye.txt', 'my-new-bucket')

Generate Object Download URLs (signed and unsigned)

This generates an unsigned download URL for hello.txt. This works because we made hello.txt public by setting the ACL above. This then generates a signed download URL for secret_plans.txt that will work for 1 hour. Signed download URLs will work for the time period even if the object is private (when the time period is up, the URL will stop working).

  1. puts AWS::S3::S3Object.url_for(
  2. 'hello.txt',
  3. 'my-new-bucket',
  4. :authenticated => false
  5. )
  6. puts AWS::S3::S3Object.url_for(
  7. 'secret_plans.txt',
  8. 'my-new-bucket',
  9. :expires_in => 60 * 60
  10. )

The output of this will look something like:

  1. http://objects.dreamhost.com/my-bucket-name/hello.txt
  2. http://objects.dreamhost.com/my-bucket-name/secret_plans.txt?Signature=XXXXXXXXXXXXXXXXXXXXXXXXXXX&Expires=1316027075&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXX