Class: RightScale::CloudApi::AWS::S3::Manager

Inherits:
CloudApi::Manager
  • Object
show all
Defined in:
lib/cloud/aws/s3/manager.rb

Overview

Amazon Simple Storage Service (S3) compatible manager (thread safe).

Examples:

require "right_aws_api"

s3 = RightScale::CloudApi::AWS::S3::Manager.new(key, secret, 'https://s3.amazonaws.com')
# -- Using HTTP verb methods --

# List all buckets
s3.get

# List bucket objects
s3.get('devs-us-east')

# Get bucket ACL
s3.get('devs-us-east', :params => {'acl' => nil} )

# Put bucket ACL
s3.put('devs-us-east',
       :params  => {'acl' => nil},
       :body    => access_control_policy_xml,
       :headers => { 'content-type' => 'application/xml' }
)

# Get bucket Versions
s3.get('devs-us-east', :params => {'version' => nil} )

# Get object
s3.get('devs-us-east/boot1.jpg')
# Do not parse response if Amazon reports back XML or JSON content-type
s3.get('devs-us-east/boot1.xml', :options => { :raw_response => true})

# Get object, force set content-type
s3.get('kd-kd-kd-1/boot1.jpg', :params => { 'response-content-type' => 'image/jpeg'})

# Put object
# Do not forget to provide a proper 'content-type' header because the default
# one is set to 'application/octet-stream'
s3.put('devs-us-east/boot1.jpg',
        :body    => 'This is my object DATA. WooHoo!!!',
        :headers => {'content-type' => 'text/plain'})
# A simple example of a multi-thread file download:
threads    = []
file_size  = 3257230
chunks     = 3
chunk_size = file_size / chunks
chunks.times do |i|
  from_byte = i * chunk_size
  to_byte   = from_byte + chunk_size - 1
  to_byte  += file_size % chunks if i + 1 == chunks
  threads << Thread::new {
    Thread.current[:my_file] = s3.get('devs-us-east/xxx/boot.jpg', {:headers => {'Range' => "bytes=#{from_byte}-#{to_byte}"}})
  }
end
file_body = ''
threads.each do |thread|
  thread.join
  file_body << thread[:my_file]
end
file_body.size #=> 3257230
# Download into IO object
File.open('/tmp/boot.jpg','w') do |file|
  s3.get('devs-us-east/kd/boot.jpg') do |chunk|
    file.write(chunk)
  end
end
# -- Using helper methods --

# List all buckets
s3.ListAllMyBuckets

# List bucket objects
s3.ListObjects('Bucket' => 'devs-us-east')

# Get bucket ACL
s3.GetBucketAcl('Bucket' => 'devs-us-east')

# Get bucket Versions
s3.GetBucketVersions('Bucket' => 'devs-us-east')

# Get object
s3.GetObject('Bucket' => 'devs-us-east', 'Object' => 'boot1.jpg')

# Get object, force set content-type
s3.GetObject('Bucket' => 'devs-us-east', 'Object' => 'boot1.jpg',
             :params => { 'response-content-type' => 'image/jpeg'})

# Put object
# P.S. 'content-type' is 'application/octet-stream' by default
s3.PutObject('Bucket' => 'devs-us-east',
             'Object' => 'boot1.jpg',
             :body    => file_content,
             :headers => {'content-type' => 'image/jpeg'})

# List all buckets
s3.ListAllMyBuckets #=>
  {"ListAllMyBucketsResult"=>
    {"Buckets"=>
      {"Bucket"=>
        [{"Name"=>"CI_right_test",
          "CreationDate"=>"2011-05-25T20:46:28.000Z"},
         {"Name"=>"CR_right_test",
          "CreationDate"=>"2011-06-08T20:46:32.000Z"},
         {"Name"=>"DarrylTest", 
          "CreationDate"=>"2011-06-03T03:43:08.000Z"},
         {"Name"=>"RightScalePeter", 
          "CreationDate"=>"2008-10-28T03:59:20.000Z"}]},
     "Owner"=>
      {"DisplayName"=>"fghsfg",
       "ID"=>"16144ab2929314cc309ffe736daa2b264357476c7fea6efb2c3347ac3ab2792a"},
     "@xmlns"=>"http://s3.amazonaws.com/doc/2006-03-01/"}}

# List bucket objects
s3.ListObjects('Bucket' => 'devs-us-east', 'max-keys' => 3, 'prefix' => 'kd') #=>
  {"ListBucketResult"=>
    {"MaxKeys"=>"3",
     "IsTruncated"=>"false",
     "Name"=>"devs-us-east",
     "Marker"=>nil,
     "Contents"=>
      {"LastModified"=>"2010-08-26T12:23:30.000Z",
       "StorageClass"=>"STANDARD",
       "ETag"=>"\"3c9a2717e34efedb6d6ac007b2acb8df\"",
       "Owner"=>
        {"DisplayName"=>"thve",
         "ID"=>
          "16144ab2929314cc309ffe736daa2b264357476c7fea6efb2c3347ac3ab2792a"},
       "Size"=>"3257230",
       "Key"=>"kd/boot.jpg"},
     "@xmlns"=>"http://s3.amazonaws.com/doc/2006-03-01/",
     "Prefix"=>"kd"}}        
# Get
s3.GetBucketCors('Bucket' => 'my-bucket' ) #=>
  {"CORSConfiguration"=>
      {"@xmlns"=>"http://s3.amazonaws.com/doc/2006-03-01/",
      "CORSRule"=>
        [{"AllowedOrigin"=>"http://www.example.com",
          "AllowedMethod"=>["PUT", "POST"],
          "MaxAgeSeconds"=>"2000",
          "ExposeHeader"=>"x-amz-server-side-encryption"},
        {"AllowedOrigin"=>"*", "AllowedMethod"=>"GET", "MaxAgeSeconds"=>"2001"}]}}
# Put
cors_rules = [
  {'AllowedOrigin' => 'http://www.example.com',
   'AllowedMethod' => ['PUT', 'POST'],
   'MaxAgeSeconds' => 3000,
   'ExposeHeader'  => 'x-amz-server-side-encryption' },
  {'AllowedOrigin' => '*',
   'AllowedMethod' => 'GET',
   'MaxAgeSeconds' => 3000 } ]
s3.PutBucketCors('Bucket' => 'kd-ver-test', 'CORSRule' => cors_rules ) #=> ''

# .. or
body = "<CORSConfiguration><CORSRule><AllowedOrigin>http://www.example.com</AllowedOrigin>"+
       "<AllowedMethod>PUT</AllowedMethod><AllowedMethod>POST</AllowedMethod>"+
       "<MaxAgeSeconds>3000</MaxAgeSeconds>..</CORSConfiguration>"
s3.PutBucketCors('Bucket' => 'my-bucket', :body => body ) #=> ''
# Delete
s3.DeleteBucketCors('Bucket' => 'my-bucket' ) #=> ''
# Bucket Tagging
# Get
s3.GetBucketTagging('Bucket' => 'my-bucket' ) #=>
  {"Tagging"=> {
     "TagSet"=> {
        "Tag"=>[
          {"Key"=>"Project",
            "Value"=>"Project One"},
           {"Key"=>"User",
            "Value"=>"jsmith"}]}}}
# Delete
s3.DeleteBucketTagging('Bucket' => 'my-bucket' ) #=> ''
# Put
tagging_rules = [
  {"Key"=>"Project",
   "Value"=>"Project One"},
  {"Key"=>"User",
   "Value"=>"jsmith"} ]
s3.PutBucketTagging('Bucket' => 'my-bucket', 'TagSet' => tagging_rules ) #=> ''
# Bucket Lifecycle
# Get
s3.GetBucketLifecycle('Bucket' => 'my-bucket' ) #=>
  {"LifecycleConfiguration"=> {
     "Rule"=>[{
       "ID" => "30-day-log-deletion-rule",
       "Prefix" => "logs",
       "Status" => "Enabled",
       "Expiration" => { "Days" => 30 }}]}}
# Delete
s3.DeleteBucketLifecycle('Bucket' => 'my-bucket' ) #=> ''
# Put
lifecycle_rules = [
  { "ID"         => "30-day-log-deletion-rule",
    "Prefix"     => "logs",
    "Status"     => "Enabled",
    "Expiration" => {"Days" => 30}},
  {"ID"          => "delete-documents-rule",
    "Prefix"     => "documents",
    "Status"     => "Enabled",
    "Expiration" => { "Days" => 365 }}]
s3.PutBucketLifecycle('Bucket' => 'my-bucket', 'Rule' => lifecycle_rules ) #=> ''
# Get a link to ListAllMyBuckets action
s3.ListAllMyBuckets(:options=>{:cloud=>{:link=>true}}) #=> 
  {"verb"   => "get",
   "link"   => "https://s3.amazonaws.com/?AWSAccessKeyId=0K4QH...G2&Expires=1426111071&Signature=vLMH...3D",
   "headers"=> {"host"=>["s3.amazonaws.com"]}}

# If you need to get a bunch of links you can use with_options helper method:
opts = {:headers => {'expires' => Time.now + 3600}}
s3.with_options(:cloud=>{:link => true}) do
  pp s3.GetObject({'Bucket'=>'my-bucket', 'Object'=>'kd/kd0.test', 'versionId'=>"00eYZeb291o4"}, opts)
  pp s3.GetObject({'Bucket'=>'my-bucket', 'Object'=>'kd/kd1.test', 'versionId'=>"fafaf1obp1W4"}, opts)
  pp s3.GetObject({'Bucket'=>'my-bucket', 'Object'=>'kd/kd2.test', 'versionId'=>"00asdTebp1W4"}, opts)
  pp s3.GetObject({'Bucket'=>'my-bucket', 'Object'=>'kd/kd3.test', 'versionId'=>"0lkjreobp1W4"}, opts)
end

See Also: