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

#!/bin/env ruby

require 'rubygems'
require 'main'
require 'aws/s3'
include AWS::S3

Main {
  argument('source_filename') {
    cast :string
    description 'source filename to copy to S3'
  }

  argument('bucket_name') {
    cast :string
    description 'bucket to place the file in on S3'
  }

  option('access_key_id') {
    argument :optional
    description 'specify the access_key_id manually'
    default 'put your access key here if you want'
  }

  option('secret_access_key') {
    argument :optional
    description 'specify the secret key manually'
    default 'put your secret key here if you want'
  }

  def run
    bucket_name = params['bucket_name'].value
    source_filename = params['source_filename'].value

    Base.establish_connection!(
      :access_key_id     => params['access_key_id'].value,
      :secret_access_key => params['secret_access_key'].value
    )

    begin
      Bucket.find(bucket_name)
    rescue
      puts "Need to make bucket #{bucket_name}.."
      Bucket.create(bucket_name)

      # Confirm its existence..
      Bucket.find(bucket_name)
    end

    puts "Got bucket.."
    puts "Uploading #{File.basename(source_filename)}.."
    S3Object.store(File.basename(source_filename), open(source_filename), bucket_name)
    puts "Stored!"

    exit_success!
  end
}

Ruby Client for Amazon Alexa Site Thumbnail (AST) Service

It's scrappy, but it does the job.

require 'cgi'
require 'openssl'
require 'base64'
require 'open-uri'

access_id = 'YOUR_ACCESS_ID'
secret_id = 'YOUR_SECRET_ID'

source_url = ARGV.first

timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")
sig = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::Digest.new('SHA1'), secret_id, 'Thumbnail' + timestamp)).strip

url = "http://ast.amazonaws.com/Xino?Action=Thumbnail&AWSAccessKeyId=" + access_id
url << "&Signature=" + CGI.escape(sig)
url << "&Timestamp=" + CGI.escape(timestamp)
url << "&Url=" +  source_url

begin
  doc = open(url).read
rescue
  puts "Could not access AWS"
  exit
end

m = doc.match(/\<aws:thumbnail[^\>]+exists=\"true\"\>(.+?)\<\//i)

if m && m[1]
  thumb_url = m[1]
  thumb_url.gsub!(/\&amp;/, '&')
  File.open("#{source_url}.jpg", "w") { |f| f.write open(thumb_url).read }
  puts "Saved to #{source_url}.jpg"
elsif m && m.match(/exists=\"false\"/)
  puts "No thumbnail for #{source_url}"
else
  puts "Error"
end

S3 upload client for Ruby

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

#!/usr/bin/env ruby

require 'rubygems'
require 'aws/s3'

local_file = ARGV[0]
bucket = ARGV[1]
mime_type = ARGV[2] || "application/octet-stream"

AWS::S3::Base.establish_connection!(
  :access_key_id     => 'REPLACE_ME',
  :secret_access_key => 'REPLACE_ME'
)

base_name = File.basename(local_file)

puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"

AWS::S3::S3Object.store(
  base_name,
  File.open(local_file),
  bucket,
  :content_type => mime_type
)

puts "Uploaded!"
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS