Class: S3Link::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/s3link.rb

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



11
12
13
# File 'lib/s3link.rb', line 11

def initialize
  @options = {}
end

Instance Method Details

#access_keyObject



37
38
39
# File 'lib/s3link.rb', line 37

def access_key
  @options[:access_key] || ENV["AMAZON_ACCESS_KEY_ID"] || false
end

#bucket_nameObject



49
50
51
# File 'lib/s3link.rb', line 49

def bucket_name
  @options[:bucket] || ENV["S3LINK_BUCKET_NAME"]
end

#establish_connectionObject



33
34
35
# File 'lib/s3link.rb', line 33

def establish_connection
  @aws = AWS::S3::Base.establish_connection!(:access_key_id => access_key, :secret_access_key => secret_key)
end

#expires_hashObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/s3link.rb', line 57

def expires_hash
  if @options[:never_expire]
    doomsday = Time.mktime(2038, 1, 18).to_i
    {:expires => doomsday}
  elsif @options[:expires_in]
    # Convert from hours to seconds
    {:expires_in => @options[:expires_in] * 60 * 60}
  else
    # 24 hours default
    {:expires_in => 24 * 60 * 60}
  end
end

#generate_urlObject



53
54
55
# File 'lib/s3link.rb', line 53

def generate_url
  AWS::S3::S3Object.url_for(@options[:filename], bucket_name, expires_hash )
end

#run_from_cmd_lineObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/s3link.rb', line 15

def run_from_cmd_line
  @options = begin
    S3Link::CommandLineParser.new($ARGV).parse
  rescue CmdLineArgsError
    Usage.new
  end

  puts "Establishing Connection" unless @options[:silent]
  establish_connection

  puts "Uploading File... please wait" unless @options[:silent]
  upload_file

  url = generate_url 
  print "URL: " unless @options[:silent]
  puts "#{url}"
end

#secret_keyObject



41
42
43
# File 'lib/s3link.rb', line 41

def secret_key
  @options[:secret_key] || ENV["AMAZON_SECRET_ACCESS_KEY"] || false
end

#upload_fileObject



45
46
47
# File 'lib/s3link.rb', line 45

def upload_file
  AWS::S3::S3Object.store(@options[:filename], File.open(@options[:filename]), bucket_name)
end