s3link 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/s3link.rb +135 -0
- metadata +65 -0
data/lib/s3link.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'aws/s3'
|
3
|
+
|
4
|
+
module S3Link
|
5
|
+
class CmdLineArgsError < RuntimeError; end
|
6
|
+
|
7
|
+
class Main
|
8
|
+
def initialize
|
9
|
+
@options = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def run_from_cmd_line
|
13
|
+
begin
|
14
|
+
parse_cmd_line
|
15
|
+
rescue CmdLineArgsError
|
16
|
+
usage
|
17
|
+
end
|
18
|
+
|
19
|
+
puts "Establishing Connection" unless @options[:silent]
|
20
|
+
establish_connection
|
21
|
+
|
22
|
+
puts "Uploading File... please wait" unless @options[:silent]
|
23
|
+
upload_file
|
24
|
+
|
25
|
+
url = generate_url
|
26
|
+
print "URL: " unless @options[:silent]
|
27
|
+
puts "#{url}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse_cmd_line
|
31
|
+
usage if $ARGV.include?("--help") || $ARGV.include?("--help") || $ARGV.length == 0
|
32
|
+
|
33
|
+
@options[:silent] == ! $ARGV.include?("--silent")
|
34
|
+
|
35
|
+
if $ARGV.include?("--access-key")
|
36
|
+
@options[:access_key] = next_arg_after("--secret-key")
|
37
|
+
end
|
38
|
+
|
39
|
+
if $ARGV.include?("--secret-key")
|
40
|
+
@options[:access_key] = next_arg_after("--secret-key")
|
41
|
+
end
|
42
|
+
|
43
|
+
if $ARGV.include?("--bucket")
|
44
|
+
@options[:bucket] = next_arg_after("--bucket")
|
45
|
+
end
|
46
|
+
|
47
|
+
if $ARGV.include?("--expires-in")
|
48
|
+
@options[:expires_in] = next_arg_after("--expires-in")
|
49
|
+
end
|
50
|
+
|
51
|
+
if $ARGV.include?("--never-expire")
|
52
|
+
@options[:never_expire] = true
|
53
|
+
end
|
54
|
+
|
55
|
+
# Last arg has to be filename
|
56
|
+
@options[:filename] = $ARGV[-1]
|
57
|
+
end
|
58
|
+
|
59
|
+
def next_arg_after(key)
|
60
|
+
index = $ARGV.index(key)
|
61
|
+
raise CmdLineArgsError if $ARGV.length <= index
|
62
|
+
$ARGV[index + 1]
|
63
|
+
end
|
64
|
+
|
65
|
+
def establish_connection
|
66
|
+
@aws = AWS::S3::Base.establish_connection!(:access_key_id => access_key, :secret_access_key => secret_key)
|
67
|
+
end
|
68
|
+
|
69
|
+
def access_key
|
70
|
+
@options[:access_key] || ENV["AMAZON_ACCESS_KEY_ID"] || false
|
71
|
+
end
|
72
|
+
|
73
|
+
def secret_key
|
74
|
+
@options[:secret_key] || ENV["AMAZON_SECRET_ACCESS_KEY"] || false
|
75
|
+
end
|
76
|
+
|
77
|
+
def upload_file
|
78
|
+
AWS::S3::S3Object.store(@options[:filename], File.open(@options[:filename]), bucket_name)
|
79
|
+
end
|
80
|
+
|
81
|
+
def bucket_name
|
82
|
+
@options[:bucket] || ENV["S3LINK_BUCKET_NAME"]
|
83
|
+
end
|
84
|
+
|
85
|
+
def generate_url
|
86
|
+
AWS::S3::S3Object.url_for(@options[:filename], bucket_name, expires_hash )
|
87
|
+
end
|
88
|
+
|
89
|
+
def expires_hash
|
90
|
+
if @options[:never_expire]
|
91
|
+
doomsday = Time.mktime(2038, 1, 18).to_i
|
92
|
+
{:expires => doomsday}
|
93
|
+
elsif @options[:expires_in]
|
94
|
+
# Convert from hours to seconds
|
95
|
+
{:expires_in => @options[:expires_in] * 60 * 60}
|
96
|
+
else
|
97
|
+
# 24 hours default
|
98
|
+
{:expires_in => 24 * 60 * 60}
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def usage(msg=nil)
|
103
|
+
puts msg if msg
|
104
|
+
puts <<-EOF
|
105
|
+
Uploads a file up to Amazon's S3 Service, and provides a time limited URL to access.
|
106
|
+
|
107
|
+
Several options may be set via environment variables for ease of use.
|
108
|
+
|
109
|
+
Options:
|
110
|
+
--access-key <key> [ENV: AMAZON_ACCESS_KEY_ID] -- Amazon provided access key
|
111
|
+
--secret-key <key [ENV: AMAZON_SECRET_ACCESS_KEY] -- Amazon provided secret key
|
112
|
+
--bucket <name> [ENV: S3LINK_BUCKET_NAME] -- Bucket to store the uploaded file
|
113
|
+
--expires-in <hours> [default: 24] -- How long the URL is valid for.
|
114
|
+
--never-expire -- Never expire the URL. If both
|
115
|
+
expires commands are set,
|
116
|
+
--expires-in will win.
|
117
|
+
--silent -- Quiet mode outputs ONLY the url
|
118
|
+
|
119
|
+
NOTE: This tool DOES NOT ever remove files from S3. Manually using the web
|
120
|
+
interface, or the s3cmd tool to clean out your bucket is advised.
|
121
|
+
|
122
|
+
WARNING: This tool will just silently overwrite a file that is already
|
123
|
+
present. This is good to replace files with new versions, but bad if it
|
124
|
+
destroys something. Don't destroy anything please.
|
125
|
+
|
126
|
+
EOF
|
127
|
+
exit(1)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
if __FILE__ == $0
|
133
|
+
S3Link::Main.new.run_from_cmd_line
|
134
|
+
end
|
135
|
+
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: s3link
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Chris Schneider
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-01 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Quick upload to S3, with time limited share URL
|
22
|
+
email: chris@christopher-schneider.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/s3link.rb
|
31
|
+
homepage: http://rubygems.org/gems/s3link
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.15
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Quick upload to S3, with time limited share URL
|
64
|
+
test_files: []
|
65
|
+
|