Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

upload_to_s3 - Ruby S3 upload client (See related posts)

Prerequisites:
gem install aws-s3
gem install main

   1  #!/bin/env ruby
   2  
   3  require 'rubygems'
   4  require 'main'
   5  require 'aws/s3'
   6  include AWS::S3
   7  
   8  Main {
   9    argument('source_filename') {
  10      cast :string
  11      description 'source filename to copy to S3'
  12    }
  13  
  14    argument('bucket_name') {
  15      cast :string
  16      description 'bucket to place the file in on S3'
  17    }
  18  
  19    option('access_key_id') {
  20      argument :optional
  21      description 'specify the access_key_id manually'
  22      default 'put your access key here if you want'
  23    }
  24  
  25    option('secret_access_key') {
  26      argument :optional
  27      description 'specify the secret key manually'
  28      default 'put your secret key here if you want'
  29    }
  30  
  31    def run
  32      bucket_name = params['bucket_name'].value
  33      source_filename = params['source_filename'].value
  34  
  35      Base.establish_connection!(
  36        :access_key_id     => params['access_key_id'].value,
  37        :secret_access_key => params['secret_access_key'].value
  38      )
  39  
  40      begin
  41        Bucket.find(bucket_name)
  42      rescue
  43        puts "Need to make bucket #{bucket_name}.."
  44        Bucket.create(bucket_name)
  45  
  46        # Confirm its existence..
  47        Bucket.find(bucket_name)
  48      end
  49  
  50      puts "Got bucket.."
  51      puts "Uploading #{File.basename(source_filename)}.."
  52      S3Object.store(File.basename(source_filename), open(source_filename), bucket_name)
  53      puts "Stored!"
  54  
  55      exit_success!
  56    end
  57  }

You need to create an account or log in to post comments to this site.


Click here to browse all 5545 code snippets

Related Posts