easy-s3 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +10 -0
- data/Gemfile +3 -1
- data/README.md +7 -9
- data/lib/easy-s3/version.rb +1 -1
- data/lib/easy-s3.rb +21 -7
- data/spec/easy-s3_spec.rb +22 -8
- data/spec/spec_helper.rb +3 -0
- metadata +6 -4
data/.gitignore
CHANGED
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
language: ruby
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
Easy use Amazon S3
|
4
4
|
|
5
|
+
[](https://travis-ci.org/mgrachev/easy-s3)
|
6
|
+
[](https://coveralls.io/r/mgrachev/easy-s3?branch=master)
|
7
|
+
[](https://gemnasium.com/mgrachev/easy-s3)
|
8
|
+
|
5
9
|
## Installation
|
6
10
|
|
7
11
|
Add this line to your application's Gemfile:
|
@@ -18,21 +22,15 @@ Or install it yourself as:
|
|
18
22
|
|
19
23
|
## Usage
|
20
24
|
|
21
|
-
First, set the credentials for Fog:
|
22
|
-
|
23
|
-
Fog.credentials = {
|
24
|
-
aws_access_key_id: 'XXX',
|
25
|
-
aws_secret_access_key: 'XXXX'
|
26
|
-
}
|
27
|
-
|
28
25
|
Create instance of EasyS3:
|
29
26
|
|
30
|
-
s3 = EasyS3.new('my-bucket')
|
27
|
+
s3 = EasyS3.new('my-bucket', access_key_id: 'XXX', secret_access_key: 'XXXX')
|
31
28
|
|
32
29
|
Create private or public file:
|
33
30
|
|
34
31
|
file_url = s3.create_file(path_to_file) # private
|
35
|
-
file_url = s3.create_file(path_to_file, true) # public
|
32
|
+
file_url = s3.create_file(path_to_file, public: true) # public
|
33
|
+
file_url = s3.create_file(path_to_file, digest: true) # with digest
|
36
34
|
|
37
35
|
Delete file by url:
|
38
36
|
|
data/lib/easy-s3/version.rb
CHANGED
data/lib/easy-s3.rb
CHANGED
@@ -8,13 +8,20 @@ class EasyS3
|
|
8
8
|
|
9
9
|
attr_reader :fog, :dir
|
10
10
|
|
11
|
-
def initialize(dir_name)
|
11
|
+
def initialize(dir_name, options={})
|
12
|
+
options[:access_key_id] ||= Fog.credentials[:aws_access_key_id]
|
13
|
+
options[:secret_access_key] ||= Fog.credentials[:aws_secret_access_key]
|
14
|
+
|
15
|
+
Fog.credentials = {
|
16
|
+
aws_access_key_id: options[:access_key_id],
|
17
|
+
aws_secret_access_key: options[:secret_access_key]
|
18
|
+
}
|
19
|
+
|
12
20
|
begin
|
13
|
-
#Fog.credentials = { aws_access_key_id: 'XXX', aws_secret_access_key: 'XXXX' }
|
14
21
|
@fog = Fog::Storage.new(provider: 'AWS')
|
15
22
|
@dir = @fog.directories.get(dir_name)
|
16
23
|
rescue ArgumentError
|
17
|
-
raise MissingOptions, '
|
24
|
+
raise MissingOptions, 'access_key_id or secret_access_key'
|
18
25
|
rescue Excon::Errors::MovedPermanently, 'Expected(200) <=> Actual(301 Moved Permanently)'
|
19
26
|
raise DirectoryDoesNotExist, dir_name
|
20
27
|
end
|
@@ -24,20 +31,27 @@ class EasyS3
|
|
24
31
|
end
|
25
32
|
|
26
33
|
# Create private or public file into directory
|
27
|
-
def create_file(file_path,
|
34
|
+
def create_file(file_path, options={})
|
35
|
+
options[:digest] ||= false
|
36
|
+
options[:public] ||= false
|
37
|
+
|
28
38
|
begin
|
29
39
|
file = File.open(file_path)
|
30
40
|
rescue
|
31
|
-
raise
|
41
|
+
raise FileNotFound, file_path
|
32
42
|
end
|
33
43
|
|
34
|
-
filename = "#{File.basename(file_path)}
|
44
|
+
filename = "#{File.basename(file_path)}"
|
45
|
+
filename += "_#{Digest::SHA1.hexdigest(File.basename(file_path))}" if options[:digest]
|
35
46
|
|
36
47
|
file = @dir.files.create(
|
37
48
|
key: filename,
|
38
49
|
body: file,
|
39
|
-
public: public
|
50
|
+
public: options[:public]
|
40
51
|
)
|
52
|
+
|
53
|
+
return file.public_url if options[:public] && file.public_url
|
54
|
+
|
41
55
|
file.url((Time.now + 3600).to_i) # 1 hour
|
42
56
|
end
|
43
57
|
|
data/spec/easy-s3_spec.rb
CHANGED
@@ -22,12 +22,15 @@ describe EasyS3 do
|
|
22
22
|
expect{ EasyS3.new }.to raise_error(ArgumentError, 'wrong number of arguments (0 for 1)')
|
23
23
|
end
|
24
24
|
|
25
|
-
it 'should
|
26
|
-
fog_credentials = Fog.credentials
|
25
|
+
it 'should accept options hash as the second argument' do
|
27
26
|
Fog.credentials = {}
|
27
|
+
EasyS3.new(bucket_name, access_key_id: 'XXX', secret_access_key: 'XXXX').should be_an_instance_of EasyS3
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
it 'should raise exception if missing options access_key_id or secret_access_key' do
|
31
|
+
fog_credentials = Fog.credentials
|
32
|
+
Fog.credentials = {}
|
33
|
+
expect{ s3 }.to raise_error(EasyS3::MissingOptions, 'access_key_id or secret_access_key')
|
31
34
|
Fog.credentials = fog_credentials
|
32
35
|
end
|
33
36
|
|
@@ -35,7 +38,7 @@ describe EasyS3 do
|
|
35
38
|
expect{ EasyS3.new(bucket_not_exist_name) }.to raise_error(EasyS3::DirectoryDoesNotExist, bucket_not_exist_name)
|
36
39
|
end
|
37
40
|
|
38
|
-
it 'should return true and instance of
|
41
|
+
it 'should return true and instance of EasyS3' do
|
39
42
|
s3.should be_an_instance_of EasyS3
|
40
43
|
end
|
41
44
|
end
|
@@ -45,17 +48,28 @@ describe EasyS3 do
|
|
45
48
|
expect{ s3.create_file(file_not_exist_path) }.to raise_error(EasyS3::FileNotFound, file_not_exist_path)
|
46
49
|
end
|
47
50
|
|
48
|
-
it 'should
|
51
|
+
it 'should create file with digest' do
|
52
|
+
url = s3.create_file(file_path, digest: true, public: true)
|
53
|
+
url.should == "https://#{bucket_name}.s3.amazonaws.com/#{File.basename(file_path)}_#{Digest::SHA1.hexdigest(File.basename(file_path))}"
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should create a public file' do
|
57
|
+
url = s3.create_file(file_path, public: true)
|
58
|
+
url.should be_an_instance_of String
|
59
|
+
url.should_not match /#{bucket_name}.+amazonaws.com.+AWSAccessKeyId=#{Fog.credentials[:aws_access_key_id]}/
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should create a private file' do
|
49
63
|
url = s3.create_file(file_path)
|
50
64
|
url.should be_an_instance_of String
|
51
|
-
url.should match /#{bucket_name}.+amazonaws.com/
|
65
|
+
url.should match /#{bucket_name}.+amazonaws.com.+AWSAccessKeyId=#{Fog.credentials[:aws_access_key_id]}/
|
52
66
|
end
|
53
67
|
end
|
54
68
|
|
55
69
|
context '#get_file_name_by_url' do
|
56
70
|
it 'should return name of file by url' do
|
57
71
|
url = s3.create_file(file_path)
|
58
|
-
s3.send(:get_filename_by_url, url).should eq "#{File.basename(file_path)}
|
72
|
+
s3.send(:get_filename_by_url, url).should eq "#{File.basename(file_path)}"
|
59
73
|
end
|
60
74
|
end
|
61
75
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy-s3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-10-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: fog
|
@@ -84,6 +84,8 @@ extra_rdoc_files: []
|
|
84
84
|
files:
|
85
85
|
- .gitignore
|
86
86
|
- .rspec
|
87
|
+
- .travis.yml
|
88
|
+
- CHANGELOG.md
|
87
89
|
- Gemfile
|
88
90
|
- LICENSE.txt
|
89
91
|
- README.md
|
@@ -109,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
111
|
version: '0'
|
110
112
|
segments:
|
111
113
|
- 0
|
112
|
-
hash:
|
114
|
+
hash: 2331300128522359285
|
113
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
116
|
none: false
|
115
117
|
requirements:
|
@@ -118,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
120
|
version: '0'
|
119
121
|
segments:
|
120
122
|
- 0
|
121
|
-
hash:
|
123
|
+
hash: 2331300128522359285
|
122
124
|
requirements: []
|
123
125
|
rubyforge_project:
|
124
126
|
rubygems_version: 1.8.23
|