easy_s3 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +32 -0
- data/README.md +67 -0
- data/easy_s3.gemspec +3 -3
- data/lib/easy_s3.rb +29 -21
- data/lib/easy_s3/version.rb +1 -1
- data/spec/easy_s3_spec.rb +114 -0
- metadata +10 -25
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
easy_s3 (0.0.1)
|
5
|
+
aws-s3
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
aws-s3 (0.6.2)
|
11
|
+
builder
|
12
|
+
mime-types
|
13
|
+
xml-simple
|
14
|
+
builder (2.1.2)
|
15
|
+
diff-lcs (1.1.2)
|
16
|
+
mime-types (1.16)
|
17
|
+
rspec (2.4.0)
|
18
|
+
rspec-core (~> 2.4.0)
|
19
|
+
rspec-expectations (~> 2.4.0)
|
20
|
+
rspec-mocks (~> 2.4.0)
|
21
|
+
rspec-core (2.4.0)
|
22
|
+
rspec-expectations (2.4.0)
|
23
|
+
diff-lcs (~> 1.1.2)
|
24
|
+
rspec-mocks (2.4.0)
|
25
|
+
xml-simple (1.0.12)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
easy_s3!
|
32
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
Easier Than S3?
|
2
|
+
===============
|
3
|
+
The [Ruby S3 Gem](https://github.com/marcel/aws-s3) is already easy, what's this all about? Its true, the standard gem is great, but I usually use it the same way every time and only do a few things with it. Like, I almost always use the same bucket for everything an app does. And I never use more than one S3 account in one app. So, I was always writing the same few bits of code to support my use case and I decided to cut a gem to make this easier.
|
4
|
+
|
5
|
+
Install
|
6
|
+
-------
|
7
|
+
Just use the `gem` command:
|
8
|
+
|
9
|
+
gem install easy_s3
|
10
|
+
|
11
|
+
If you don't have `aws-s3`, rubygems will install it for you.
|
12
|
+
|
13
|
+
Setup
|
14
|
+
-----
|
15
|
+
`EasyS3` must be instantiated before you can use it and there are a couple ways you can do this. The most straightforward way is to simply pass it three string parameters:
|
16
|
+
|
17
|
+
easy_s3 = EasyS3.new('bucket', 'key', 'secret')
|
18
|
+
|
19
|
+
This will create a new instance of `EasyS3` that you can then use later (see Methods and Usage below).
|
20
|
+
|
21
|
+
Another approach is to set ENV variables for the gem to use. If you set `ENV['EASY_S3_KEY']` and `ENV['EASY_S3_SECRET']`, then you can leave those parameters off, like this:
|
22
|
+
|
23
|
+
easy_s3 = EasyS3.new('bucket')
|
24
|
+
|
25
|
+
But make sure you name your ENV variables as above so the gem can find them.
|
26
|
+
|
27
|
+
I like the second approach best because it keeps sensitive things out of your repo. In development, I'll have a handful of apps using this gem and they all use the same S3 account, so setting these variables once in my .profile file works great. But when I do have a project that uses a different S3 account, then I'll use the first approach, but still use EVN variables, just ones that are specific to the app I'm working with, like this:
|
28
|
+
|
29
|
+
easy_s3 = EasyS3.new('bucket', ENV['MYAPP_EASY_S3_KEY'], ENV['MYAPP_EASY_S3_SECRET'])
|
30
|
+
|
31
|
+
Whichever way you go about setting up your instance, the main thing you're setting is the bucket and that's my main assumption here: that you'll do all your work in one bucket. If that's not how you use S3, this gem ain't for you. If that is how you use S3, then I think you'll like convenience of not having to specify this over and over again.
|
32
|
+
|
33
|
+
Methods
|
34
|
+
-------
|
35
|
+
Once you've got your `EasyS3` instance, you can call five main methods on it:
|
36
|
+
|
37
|
+
easy_s3.write(path, data) => writes the data its given to the file at path
|
38
|
+
easy_s3.file(path) => gets the AWS::S3::S3Object found at path
|
39
|
+
easy_s3.exists?(path) => check if a file exists at path and returns a boolean
|
40
|
+
easy_s3.append(path, data) => appends data to the end of the file found at path (uses write)
|
41
|
+
easy_s3.read(path) => returns a string of the value of the file found at path (uses file)
|
42
|
+
|
43
|
+
Remember: these are all being called in the context of your `EasyS3` instance, so the S3 account and bucket are coming from your setup above.
|
44
|
+
|
45
|
+
Usage
|
46
|
+
-----
|
47
|
+
I wrote this to support five use cases.
|
48
|
+
|
49
|
+
--Create a file and write some data to it:
|
50
|
+
|
51
|
+
easy_s3.write('path/to/file.txt', 'blah, blah, blah')
|
52
|
+
|
53
|
+
--Append a message to a log file:
|
54
|
+
|
55
|
+
easy_s3.append('path/to/log/file.txt', 'ack, the program asploded!')
|
56
|
+
|
57
|
+
--See if a file exists
|
58
|
+
|
59
|
+
puts 'our file is there!' if easy_s3.exists?('path/to/file.txt')
|
60
|
+
|
61
|
+
--Get the value of a file
|
62
|
+
|
63
|
+
data = easy_s3.read('path/to/file.txt')
|
64
|
+
|
65
|
+
--Get the actual `S3Object` (and then do something with it)
|
66
|
+
|
67
|
+
file = easy_s3.file('path/to/file.txt')
|
data/easy_s3.gemspec
CHANGED
@@ -7,9 +7,9 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Jon Allured"]
|
9
9
|
s.email = ["jon.allured@me.com"]
|
10
|
-
s.homepage = "
|
11
|
-
s.summary = "
|
12
|
-
s.description = "
|
10
|
+
s.homepage = "https://github.com/jonallured/easy_s3"
|
11
|
+
s.summary = "simple wrapper for working with S3"
|
12
|
+
s.description = "wraps the S3 Ruby Gem with some simple methods for those common tasks that should be easier"
|
13
13
|
|
14
14
|
s.required_rubygems_version = ">= 1.3.6"
|
15
15
|
|
data/lib/easy_s3.rb
CHANGED
@@ -1,29 +1,37 @@
|
|
1
1
|
require 'aws/s3'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
class EasyS3
|
4
|
+
|
5
|
+
attr_accessor :bucket
|
6
|
+
|
7
|
+
def initialize(bucket, key = ENV['EASY_S3_KEY'], secret = ENV['EASY_S3_SECRET'])
|
8
|
+
@bucket = bucket
|
9
|
+
EasyS3.connect_to_s3(key, secret)
|
10
|
+
end
|
11
|
+
|
12
|
+
def write(path, data)
|
13
|
+
AWS::S3::S3Object.store(path, data, @bucket, :access => :public_read)
|
14
|
+
end
|
15
|
+
|
16
|
+
def file(path)
|
17
|
+
AWS::S3::S3Object.find(path, @bucket)
|
12
18
|
end
|
13
|
-
|
14
|
-
|
15
|
-
|
19
|
+
|
20
|
+
def exists?(path)
|
21
|
+
AWS::S3::S3Object.exists?(path, @bucket)
|
16
22
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
|
24
|
+
def read(path)
|
25
|
+
file(path).value
|
20
26
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
27
|
+
|
28
|
+
def append(path, data)
|
29
|
+
write(path, read(path) + data)
|
24
30
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
|
32
|
+
def self.connect_to_s3(key, secret)
|
33
|
+
raise StandardError, "Bad key or secret" unless key and secret
|
34
|
+
AWS::S3::Base.establish_connection!(:access_key_id => key, :secret_access_key => secret)
|
28
35
|
end
|
36
|
+
|
29
37
|
end
|
data/lib/easy_s3/version.rb
CHANGED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rspec'
|
3
|
+
require "#{File.dirname __FILE__}/../lib/easy_s3"
|
4
|
+
|
5
|
+
describe EasyS3 do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
AWS::S3::Base.stub!(:establish_connection!)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#new' do
|
12
|
+
|
13
|
+
it 'should return the first argument as the bucket attribute' do
|
14
|
+
easy_s3 = EasyS3.new('bucket')
|
15
|
+
easy_s3.bucket.should == 'bucket'
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'if passed S3 credentials' do
|
19
|
+
it 'should send connect_to_s3 the key and secret' do
|
20
|
+
EasyS3.should_receive(:connect_to_s3).with('key', 'secret')
|
21
|
+
EasyS3.new('bucket', 'key', 'secret')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'if easy_s3 ENV variables exist' do
|
26
|
+
before(:each) do
|
27
|
+
ENV['EASY_S3_KEY'] = 'env_key'
|
28
|
+
ENV['EASY_S3_SECRET'] = 'env_secret'
|
29
|
+
end
|
30
|
+
|
31
|
+
after(:each) do
|
32
|
+
ENV['EASY_S3_KEY'] = nil
|
33
|
+
ENV['EASY_S3_SECRET'] = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should send connect_to_s3 ENV['EASY_S3_KEY'] and ENV['EASY_S3_SECRET']" do
|
37
|
+
EasyS3.should_receive(:connect_to_s3).with('env_key', 'env_secret')
|
38
|
+
EasyS3.new('bucket')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'if config/easy_s3.yml exists' do
|
43
|
+
it 'should create an S3 connection using the credentials from the file'
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '.connect_to_s3' do
|
49
|
+
|
50
|
+
it 'should create a connection to s3 when passed a key and secret' do
|
51
|
+
AWS::S3::Base.should_receive(:establish_connection!).with(:access_key_id => 'key', :secret_access_key => 'secret')
|
52
|
+
EasyS3.connect_to_s3('key', 'secret')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should throw an error when either key or secret is nil' do
|
56
|
+
lambda do
|
57
|
+
EasyS3.connect_to_s3(nil, nil)
|
58
|
+
end.should raise_error(StandardError, "Bad key or secret")
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'instance methods' do
|
64
|
+
|
65
|
+
before(:each) do
|
66
|
+
EasyS3.stub!(:connect_to_s3)
|
67
|
+
AWS::S3::S3Object.stub!(:exists?).and_return(true)
|
68
|
+
|
69
|
+
@mock_s3_object = mock('S3Object')
|
70
|
+
@mock_s3_object.stub!(:value).and_return('some data')
|
71
|
+
AWS::S3::S3Object.stub!(:find).and_return(@mock_s3_object)
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '#write' do
|
75
|
+
it 'should send data to the path at S3' do
|
76
|
+
AWS::S3::S3Object.should_receive(:store).with('/path/to/file.txt', 'some message', 'bucket', :access => :public_read)
|
77
|
+
easy_s3 = EasyS3.new('bucket')
|
78
|
+
easy_s3.write('/path/to/file.txt', 'some message')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#file' do
|
83
|
+
it 'should return an S3Object when a file is found' do
|
84
|
+
easy_s3 = EasyS3.new('bucket')
|
85
|
+
file = easy_s3.file('/path/to/file.txt')
|
86
|
+
file.should == @mock_s3_object
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#read' do
|
91
|
+
it 'should return the value of the file found' do
|
92
|
+
easy_s3 = EasyS3.new('bucket')
|
93
|
+
data = easy_s3.read('/path/to/file.txt')
|
94
|
+
data.should == 'some data'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe '#append' do
|
99
|
+
it 'should add its data to the data of the file at path' do
|
100
|
+
AWS::S3::S3Object.should_receive(:store).with('/path/to/file.txt', 'some data some message', 'bucket', :access => :public_read)
|
101
|
+
easy_s3 = EasyS3.new('bucket')
|
102
|
+
easy_s3.append('/path/to/file.txt', ' some message')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe '#exists?' do
|
107
|
+
it 'should return true when the file is found' do
|
108
|
+
easy_s3 = EasyS3.new('bucket')
|
109
|
+
easy_s3.exists?('/path/to/file.txt').should be_true
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_s3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 0.0.1
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Jon Allured
|
@@ -15,8 +10,7 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
13
|
+
date: 2011-04-29 00:00:00 Z
|
20
14
|
dependencies:
|
21
15
|
- !ruby/object:Gem::Dependency
|
22
16
|
name: aws-s3
|
@@ -26,13 +20,10 @@ dependencies:
|
|
26
20
|
requirements:
|
27
21
|
- - ">="
|
28
22
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 3
|
30
|
-
segments:
|
31
|
-
- 0
|
32
23
|
version: "0"
|
33
24
|
type: :runtime
|
34
25
|
version_requirements: *id001
|
35
|
-
description:
|
26
|
+
description: wraps the S3 Ruby Gem with some simple methods for those common tasks that should be easier
|
36
27
|
email:
|
37
28
|
- jon.allured@me.com
|
38
29
|
executables: []
|
@@ -44,12 +35,14 @@ extra_rdoc_files: []
|
|
44
35
|
files:
|
45
36
|
- .gitignore
|
46
37
|
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- README.md
|
47
40
|
- Rakefile
|
48
41
|
- easy_s3.gemspec
|
49
42
|
- lib/easy_s3.rb
|
50
43
|
- lib/easy_s3/version.rb
|
51
|
-
|
52
|
-
homepage:
|
44
|
+
- spec/easy_s3_spec.rb
|
45
|
+
homepage: https://github.com/jonallured/easy_s3
|
53
46
|
licenses: []
|
54
47
|
|
55
48
|
post_install_message:
|
@@ -62,27 +55,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
55
|
requirements:
|
63
56
|
- - ">="
|
64
57
|
- !ruby/object:Gem::Version
|
65
|
-
hash: 3
|
66
|
-
segments:
|
67
|
-
- 0
|
68
58
|
version: "0"
|
69
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
60
|
none: false
|
71
61
|
requirements:
|
72
62
|
- - ">="
|
73
63
|
- !ruby/object:Gem::Version
|
74
|
-
hash: 23
|
75
|
-
segments:
|
76
|
-
- 1
|
77
|
-
- 3
|
78
|
-
- 6
|
79
64
|
version: 1.3.6
|
80
65
|
requirements: []
|
81
66
|
|
82
67
|
rubyforge_project:
|
83
|
-
rubygems_version: 1.
|
68
|
+
rubygems_version: 1.7.2
|
84
69
|
signing_key:
|
85
70
|
specification_version: 3
|
86
|
-
summary:
|
71
|
+
summary: simple wrapper for working with S3
|
87
72
|
test_files: []
|
88
73
|
|