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

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

upload_to_s3 - Ruby S3 upload client

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  }

Ruby Client for Amazon Alexa Site Thumbnail (AST) Service

It's scrappy, but it does the job.

   1  
   2  require 'cgi'
   3  require 'openssl'
   4  require 'base64'
   5  require 'open-uri'
   6  
   7  access_id = 'YOUR_ACCESS_ID'
   8  secret_id = 'YOUR_SECRET_ID'
   9  
  10  source_url = ARGV.first
  11  
  12  timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")
  13  sig = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::Digest.new('SHA1'), secret_id, 'Thumbnail' + timestamp)).strip
  14  
  15  url = "http://ast.amazonaws.com/Xino?Action=Thumbnail&AWSAccessKeyId=" + access_id
  16  url << "&Signature=" + CGI.escape(sig)
  17  url << "&Timestamp=" + CGI.escape(timestamp)
  18  url << "&Url=" +  source_url
  19  
  20  begin
  21    doc = open(url).read
  22  rescue
  23    puts "Could not access AWS"
  24    exit
  25  end
  26  
  27  m = doc.match(/\<aws:thumbnail[^\>]+exists=\"true\"\>(.+?)\<\//i)
  28  
  29  if m && m[1]
  30    thumb_url = m[1]
  31    thumb_url.gsub!(/\&amp;/, '&')
  32    File.open("#{source_url}.jpg", "w") { |f| f.write open(thumb_url).read }
  33    puts "Saved to #{source_url}.jpg"
  34  elsif m && m.match(/exists=\"false\"/)
  35    puts "No thumbnail for #{source_url}"
  36  else
  37    puts "Error"
  38  end

S3 upload client for Ruby

Uses Marcel Molina's AWS::S3 gem.. gem install aws-s3

   1  
   2  #!/usr/bin/env ruby
   3  
   4  require 'rubygems'
   5  require 'aws/s3'
   6  
   7  local_file = ARGV[0]
   8  bucket = ARGV[1]
   9  mime_type = ARGV[2] || "application/octet-stream"
  10  
  11  AWS::S3::Base.establish_connection!(
  12    :access_key_id     => 'REPLACE_ME',
  13    :secret_access_key => 'REPLACE_ME'
  14  )
  15  
  16  base_name = File.basename(local_file)
  17  
  18  puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"
  19  
  20  AWS::S3::S3Object.store(
  21    base_name,
  22    File.open(local_file),
  23    bucket,
  24    :content_type => mime_type
  25  )
  26  
  27  puts "Uploaded!"
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS