easy-s3 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +5 -0
- data/easy-s3.gemspec +26 -0
- data/lib/easy-s3/version.rb +3 -0
- data/lib/easy-s3.rb +61 -0
- data/spec/easy-s3_spec.rb +78 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/support/file.txt +1 -0
- metadata +131 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mikhail Grachev
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# EasyS3
|
2
|
+
|
3
|
+
Easy use Amazon S3
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'easy-s3'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install easy-s3
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
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
|
+
Create instance of EasyS3:
|
29
|
+
|
30
|
+
s3 = EasyS3.new('my-bucket')
|
31
|
+
|
32
|
+
Create private or public file:
|
33
|
+
|
34
|
+
file_url = s3.create_file(path_to_file) # private
|
35
|
+
file_url = s3.create_file(path_to_file, true) # public
|
36
|
+
|
37
|
+
Delete file by url:
|
38
|
+
|
39
|
+
file_url = s3.create_file(path_to_file)
|
40
|
+
s3.delete_file(file_url)
|
41
|
+
|
42
|
+
Get all files in bucket:
|
43
|
+
|
44
|
+
s3.files
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/easy-s3.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'easy-s3/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'easy-s3'
|
8
|
+
spec.version = EasyS3::VERSION
|
9
|
+
spec.authors = ['Mikhail Grachev']
|
10
|
+
spec.email = ['i@mgrachev.com']
|
11
|
+
spec.description = %q{Easy use Amazon S3}
|
12
|
+
spec.summary = %q{Easy use Amazon S3}
|
13
|
+
spec.homepage = 'https://github.com/mgrachev/easy-s3'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'fog', '~> 1.15.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 2.14.1'
|
26
|
+
end
|
data/lib/easy-s3.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'fog'
|
2
|
+
require 'easy-s3/version'
|
3
|
+
|
4
|
+
class EasyS3
|
5
|
+
class MissingOptions < StandardError; end
|
6
|
+
class DirectoryDoesNotExist < StandardError; end
|
7
|
+
class FileNotFound < StandardError; end
|
8
|
+
|
9
|
+
attr_reader :fog, :dir
|
10
|
+
|
11
|
+
def initialize(dir_name)
|
12
|
+
begin
|
13
|
+
#Fog.credentials = { aws_access_key_id: 'XXX', aws_secret_access_key: 'XXXX' }
|
14
|
+
@fog = Fog::Storage.new(provider: 'AWS')
|
15
|
+
@dir = @fog.directories.get(dir_name)
|
16
|
+
rescue ArgumentError
|
17
|
+
raise MissingOptions, 'aws_access_key_id or aws_secret_access_key'
|
18
|
+
rescue Excon::Errors::MovedPermanently, 'Expected(200) <=> Actual(301 Moved Permanently)'
|
19
|
+
raise DirectoryDoesNotExist, dir_name
|
20
|
+
end
|
21
|
+
raise DirectoryDoesNotExist, dir_name if @dir.nil?
|
22
|
+
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
# Create private or public file into directory
|
27
|
+
def create_file(file_path, public = false)
|
28
|
+
begin
|
29
|
+
file = File.open(file_path)
|
30
|
+
rescue
|
31
|
+
raise(FileNotFound, file_path)
|
32
|
+
end
|
33
|
+
|
34
|
+
filename = "#{File.basename(file_path)}_#{Digest::SHA1.hexdigest(File.basename(file_path))}"
|
35
|
+
|
36
|
+
file = @dir.files.create(
|
37
|
+
key: filename,
|
38
|
+
body: file,
|
39
|
+
public: public
|
40
|
+
)
|
41
|
+
file.url((Time.now + 3600).to_i) # 1 hour
|
42
|
+
end
|
43
|
+
|
44
|
+
# Delete file by url
|
45
|
+
def delete_file(url)
|
46
|
+
file_name = get_filename_by_url(url)
|
47
|
+
return false unless @dir.files.head(file_name)
|
48
|
+
@dir.files.get(file_name).destroy
|
49
|
+
end
|
50
|
+
|
51
|
+
# Get all files into directory
|
52
|
+
def files
|
53
|
+
@dir.files.all
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def get_filename_by_url(url)
|
59
|
+
File.basename(URI(url).path)
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'easy-s3'
|
3
|
+
require 'fog'
|
4
|
+
|
5
|
+
|
6
|
+
describe EasyS3 do
|
7
|
+
let(:bucket_name) { 'my-bucket' }
|
8
|
+
let(:bucket_not_exist_name) { 'test' }
|
9
|
+
let(:s3) { EasyS3.new(bucket_name) }
|
10
|
+
let(:file_path) { File.join('spec', 'support', 'file.txt') }
|
11
|
+
let(:file_not_exist_path) { File.join('spec', 'support', 'file_not_exist.txt') }
|
12
|
+
|
13
|
+
before :all do
|
14
|
+
Fog.mock!
|
15
|
+
Fog.credentials = { aws_access_key_id: 'XXX', aws_secret_access_key: 'XXXX' }
|
16
|
+
connection = Fog::Storage.new(provider: 'AWS')
|
17
|
+
connection.directories.create(key: 'my-bucket')
|
18
|
+
end
|
19
|
+
|
20
|
+
context '.initialize' do
|
21
|
+
it 'should raise exception if call method without argument directory name' do
|
22
|
+
expect{ EasyS3.new }.to raise_error(ArgumentError, 'wrong number of arguments (0 for 1)')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should raise exception if missing options aws_access_key_id or aws_secret_access_key' do
|
26
|
+
fog_credentials = Fog.credentials
|
27
|
+
Fog.credentials = {}
|
28
|
+
|
29
|
+
expect{ s3 }.to raise_error(EasyS3::MissingOptions, 'aws_access_key_id or aws_secret_access_key')
|
30
|
+
|
31
|
+
Fog.credentials = fog_credentials
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should raise exception if directory does not exist' do
|
35
|
+
expect{ EasyS3.new(bucket_not_exist_name) }.to raise_error(EasyS3::DirectoryDoesNotExist, bucket_not_exist_name)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should return true and instance of Mercury::S3' do
|
39
|
+
s3.should be_an_instance_of EasyS3
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context '#create_file' do
|
44
|
+
it 'should raise exception if file not found' do
|
45
|
+
expect{ s3.create_file(file_not_exist_path) }.to raise_error(EasyS3::FileNotFound, file_not_exist_path)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should return url of file' do
|
49
|
+
url = s3.create_file(file_path)
|
50
|
+
url.should be_an_instance_of String
|
51
|
+
url.should match /#{bucket_name}.+amazonaws.com/
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context '#get_file_name_by_url' do
|
56
|
+
it 'should return name of file by url' do
|
57
|
+
url = s3.create_file(file_path)
|
58
|
+
s3.send(:get_filename_by_url, url).should eq "#{File.basename(file_path)}_#{Digest::SHA1.hexdigest(File.basename(file_path))}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context '#delete_file' do
|
63
|
+
it 'should return false if file not found on Amazon S3' do
|
64
|
+
s3.delete_file('bad_file').should be_false
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should delete file by namea and return true' do
|
68
|
+
url = s3.create_file(file_path)
|
69
|
+
s3.delete_file(url).should be_true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context '#files' do
|
74
|
+
it 'should return instance of Fog::Storage::AWS::Files' do
|
75
|
+
s3.files.should be_an_instance_of Fog::Storage::AWS::Files
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Test file.txt
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy-s3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mikhail Grachev
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: fog
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.15.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.15.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.14.1
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.14.1
|
78
|
+
description: Easy use Amazon S3
|
79
|
+
email:
|
80
|
+
- i@mgrachev.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- easy-s3.gemspec
|
92
|
+
- lib/easy-s3.rb
|
93
|
+
- lib/easy-s3/version.rb
|
94
|
+
- spec/easy-s3_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
- spec/support/file.txt
|
97
|
+
homepage: https://github.com/mgrachev/easy-s3
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
hash: -4383353905125523505
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
hash: -4383353905125523505
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.8.23
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Easy use Amazon S3
|
128
|
+
test_files:
|
129
|
+
- spec/easy-s3_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
- spec/support/file.txt
|