s3-client 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +174 -0
- data/Rakefile +40 -0
- data/config/settings.yml +11 -0
- data/lib/s3/client.rb +31 -0
- data/lib/s3/client/api.rb +258 -0
- data/lib/s3/client/api/rest_parameter.rb +149 -0
- data/lib/s3/client/api/storage.rb +363 -0
- data/lib/s3/client/exception.rb +20 -0
- data/lib/s3/client/model/bucket.rb +20 -0
- data/lib/s3/client/model/bucket_collection.rb +35 -0
- data/lib/s3/client/model/concerns/buckets_result.rb +21 -0
- data/lib/s3/client/model/concerns/objects_result.rb +35 -0
- data/lib/s3/client/model/object.rb +56 -0
- data/lib/s3/client/model/object_collection.rb +65 -0
- data/lib/s3/client/storage.rb +43 -0
- data/lib/s3/settings.rb +9 -0
- data/lib/s3/version.rb +3 -0
- data/s3-client.gemspec +30 -0
- metadata +176 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
module S3
|
2
|
+
class Client
|
3
|
+
class APIFailure < StandardError
|
4
|
+
attr_accessor :api_code
|
5
|
+
attr_accessor :api_message
|
6
|
+
attr_accessor :api_status
|
7
|
+
attr_accessor :api_request_id
|
8
|
+
attr_accessor :api_resource
|
9
|
+
end
|
10
|
+
|
11
|
+
class ParameterInvalid < StandardError
|
12
|
+
end
|
13
|
+
|
14
|
+
class APIOptionInvalid < StandardError
|
15
|
+
end
|
16
|
+
|
17
|
+
class StatusInvalid < StandardError
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module S3
|
2
|
+
class Bucket < Driver::Model
|
3
|
+
def initialize(api, bucket_name)
|
4
|
+
super(api)
|
5
|
+
@name = bucket_name
|
6
|
+
end
|
7
|
+
|
8
|
+
def name
|
9
|
+
@name
|
10
|
+
end
|
11
|
+
|
12
|
+
def delete
|
13
|
+
@api.delete_bucket(@name)
|
14
|
+
end
|
15
|
+
|
16
|
+
def objects(prefix: nil)
|
17
|
+
S3::ObjectCollection.new(@api, @name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module S3
|
2
|
+
class BucketCollection < Driver::Model
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def create(bucket_name)
|
6
|
+
@api.create_bucket(bucket_name)
|
7
|
+
bucket_named(bucket_name)
|
8
|
+
end
|
9
|
+
|
10
|
+
# @example
|
11
|
+
#
|
12
|
+
# bucket = client.buckets[:mybucket],
|
13
|
+
# bucket = client.buckets['mybucket'],
|
14
|
+
#
|
15
|
+
# @param [String] bucket_name
|
16
|
+
# @return [Bucket]
|
17
|
+
def [] bucket_name
|
18
|
+
bucket_named(bucket_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def each
|
22
|
+
xml_doc = @api.buckets
|
23
|
+
buckets = S3::Concerns::BucketsResult.new(xml_doc).buckets
|
24
|
+
buckets.each do |bucket|
|
25
|
+
yield bucket_named(bucket)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def bucket_named(bucket)
|
32
|
+
S3::Bucket.new(@api, bucket.to_s)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module S3
|
2
|
+
class Concerns
|
3
|
+
class BucketsResult < Driver::Model
|
4
|
+
def initialize(xml_doc)
|
5
|
+
@xml_doc = xml_doc
|
6
|
+
end
|
7
|
+
|
8
|
+
def buckets
|
9
|
+
REXML::XPath.match(@xml_doc, "/ListAllMyBucketsResult/Buckets/Bucket/Name").map { |b| b.text }
|
10
|
+
end
|
11
|
+
|
12
|
+
def owner_id
|
13
|
+
REXML::XPath.match(@xml_doc, "/ListAllMyBucketsResult/Owner/ID").map { |b| b.text }.first
|
14
|
+
end
|
15
|
+
|
16
|
+
def owner_display_name
|
17
|
+
REXML::XPath.match(@xml_doc, "/ListAllMyBucketsResult/Owner/DisplayName").map { |b| b.text }.first
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module S3
|
2
|
+
class Concerns
|
3
|
+
class ObjectsResult < Driver::Model
|
4
|
+
def initialize(xml_doc)
|
5
|
+
@xml_doc = xml_doc
|
6
|
+
end
|
7
|
+
|
8
|
+
def objects
|
9
|
+
REXML::XPath.match(@xml_doc, "/ListBucketResult/Contents/Key").map { |b| b.text }
|
10
|
+
end
|
11
|
+
|
12
|
+
def full_objects
|
13
|
+
REXML::XPath.match(@xml_doc, "/ListBucketResult/Contents").map{|m|
|
14
|
+
XmlSimple.xml_in(m.to_s)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def truncated?
|
19
|
+
REXML::XPath.match(@xml_doc, "/ListBucketResult/IsTruncated").map { |b| b.text }.first == 'true'
|
20
|
+
end
|
21
|
+
|
22
|
+
def marker
|
23
|
+
REXML::XPath.match(@xml_doc, "/ListBucketResult/Marker").map { |b| b.text }.first
|
24
|
+
end
|
25
|
+
|
26
|
+
def next_marker
|
27
|
+
REXML::XPath.match(@xml_doc, "/ListBucketResult/NextMarker").map { |b| b.text }.first
|
28
|
+
end
|
29
|
+
|
30
|
+
def max
|
31
|
+
REXML::XPath.match(@xml_doc, "/ListBucketResult/MaxKeys").map { |b| b.text }.first.to_i
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module S3
|
2
|
+
class Object < Driver::Model
|
3
|
+
def initialize(api, bucket_name, object_name, opts = {})
|
4
|
+
super(api)
|
5
|
+
@bucket = bucket_name
|
6
|
+
@name = object_name
|
7
|
+
@opts = opts
|
8
|
+
end
|
9
|
+
|
10
|
+
def name
|
11
|
+
@name
|
12
|
+
end
|
13
|
+
|
14
|
+
def size
|
15
|
+
@opts["Size"][0].to_i
|
16
|
+
end
|
17
|
+
|
18
|
+
def etag
|
19
|
+
@opts["ETag"][0]
|
20
|
+
end
|
21
|
+
|
22
|
+
def lastmodified
|
23
|
+
Time.parse(@opts["LastModified"][0])
|
24
|
+
end
|
25
|
+
|
26
|
+
def read(range = nil)
|
27
|
+
@api.get_object(@bucket, @name, range)
|
28
|
+
end
|
29
|
+
|
30
|
+
def write(data, options = {})
|
31
|
+
if data.is_a?(String)
|
32
|
+
data = StringIO.new(data)
|
33
|
+
elsif data.is_a?(Pathname)
|
34
|
+
data = File.open(data, 'rb')
|
35
|
+
elsif data.respond_to?(:read) && data.respond_to?(:eof?)
|
36
|
+
end
|
37
|
+
|
38
|
+
unless options[:multipart]
|
39
|
+
@api.create_object(@bucket, @name, options) do
|
40
|
+
data
|
41
|
+
end
|
42
|
+
else
|
43
|
+
@api.create_multipart_object(@bucket, @name, options) do
|
44
|
+
data
|
45
|
+
end
|
46
|
+
end
|
47
|
+
data.close
|
48
|
+
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
52
|
+
def delete
|
53
|
+
@api.delete_object(@bucket, @name)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module S3
|
2
|
+
class ObjectCollection < Driver::Model
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(api, bucket_name)
|
6
|
+
super(api)
|
7
|
+
@bucket = bucket_name
|
8
|
+
end
|
9
|
+
|
10
|
+
def [](object_name)
|
11
|
+
object_named(object_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def where(prefix: nil, delimiter: nil)
|
15
|
+
@prefix = prefix
|
16
|
+
@delimiter = delimiter
|
17
|
+
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def each
|
22
|
+
options = {}
|
23
|
+
if @prefix
|
24
|
+
options = options.merge(prefix: @prefix)
|
25
|
+
end
|
26
|
+
|
27
|
+
if @delimiter
|
28
|
+
options = options.merge(delimiter: @delimiter)
|
29
|
+
end
|
30
|
+
|
31
|
+
marker = nil
|
32
|
+
truncated = false
|
33
|
+
begin
|
34
|
+
if marker.present?
|
35
|
+
options = options.merge(marker: marker)
|
36
|
+
end
|
37
|
+
xml_doc = @api.objects(@bucket, options)
|
38
|
+
objects_result = S3::Concerns::ObjectsResult.new(xml_doc)
|
39
|
+
objects = objects_result.full_objects
|
40
|
+
|
41
|
+
truncated = objects_result.truncated?
|
42
|
+
next_marker = objects_result.next_marker
|
43
|
+
if next_marker.nil?
|
44
|
+
marker = objects.last['Key'][0] if objects.present?
|
45
|
+
else
|
46
|
+
marker = next_marker
|
47
|
+
end
|
48
|
+
|
49
|
+
objects.each do |object|
|
50
|
+
yield object_opts(object)
|
51
|
+
end
|
52
|
+
end while truncated
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def object_named(object_name)
|
58
|
+
S3::Object.new(@api, @bucket, object_name.to_s)
|
59
|
+
end
|
60
|
+
|
61
|
+
def object_opts(object_opts)
|
62
|
+
S3::Object.new(@api, @bucket, object_opts["Key"][0], object_opts)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module S3
|
2
|
+
class Client
|
3
|
+
model Bucket, BucketCollection, :Object, ObjectCollection
|
4
|
+
|
5
|
+
module Storage
|
6
|
+
def buckets
|
7
|
+
S3::BucketCollection.new(@api)
|
8
|
+
end
|
9
|
+
|
10
|
+
def objects(bucket)
|
11
|
+
S3::ObjectCollection.new(@api, bucket)
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_bucket(bucket)
|
15
|
+
@api.create_bucket(bucket)
|
16
|
+
|
17
|
+
S3::Bucket.new(@api, bucket)
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete_bucket(bucket)
|
21
|
+
@api.delete_bucket(bucket)
|
22
|
+
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete_object(bucket, object)
|
27
|
+
@api.delete_object(bucket, object)
|
28
|
+
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# == options ==
|
34
|
+
# * <tt>label</tt> - label
|
35
|
+
# * <tt>jobs</tt> - count of executing pararell
|
36
|
+
def import(db_name, table, file_path, options = {})
|
37
|
+
@api.import(db_name, table, file_path, options)
|
38
|
+
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/s3/settings.rb
ADDED
data/lib/s3/version.rb
ADDED
data/s3-client.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 's3/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "s3-client"
|
8
|
+
spec.version = S3::VERSION
|
9
|
+
spec.authors = ["hiro-su"]
|
10
|
+
spec.email = ["h.sugipon@gmail.com"]
|
11
|
+
spec.description = %q{It is a simple AWS S3 library for Ruby}
|
12
|
+
spec.summary = %q{Is is a simple AWS S3 library for Ruby}
|
13
|
+
spec.homepage = "http://github.com/hiro-su/s3-client"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/).reject {|item| item =~ /^(sample|doc|README|CHANGELOG|.drone.yml|spec|.rspec)/ }
|
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 "httpclient", "~> 2.8.0"
|
22
|
+
spec.add_dependency "settingslogic", "~> 2.0.9"
|
23
|
+
spec.add_dependency "mime-types", "~> 3.1"
|
24
|
+
spec.add_dependency "xml-simple", "~> 1.1.4"
|
25
|
+
spec.add_dependency "driver", "~> 0.0.4"
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.1.0"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: s3-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hiro-su
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httpclient
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.8.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.8.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: settingslogic
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.9
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.9
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mime-types
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: xml-simple
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.4
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.1.4
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: driver
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.0.4
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.0.4
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '10.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '10.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 3.1.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 3.1.0
|
125
|
+
description: It is a simple AWS S3 library for Ruby
|
126
|
+
email:
|
127
|
+
- h.sugipon@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- Gemfile
|
134
|
+
- LICENSE.txt
|
135
|
+
- Rakefile
|
136
|
+
- config/settings.yml
|
137
|
+
- lib/s3/client.rb
|
138
|
+
- lib/s3/client/api.rb
|
139
|
+
- lib/s3/client/api/rest_parameter.rb
|
140
|
+
- lib/s3/client/api/storage.rb
|
141
|
+
- lib/s3/client/exception.rb
|
142
|
+
- lib/s3/client/model/bucket.rb
|
143
|
+
- lib/s3/client/model/bucket_collection.rb
|
144
|
+
- lib/s3/client/model/concerns/buckets_result.rb
|
145
|
+
- lib/s3/client/model/concerns/objects_result.rb
|
146
|
+
- lib/s3/client/model/object.rb
|
147
|
+
- lib/s3/client/model/object_collection.rb
|
148
|
+
- lib/s3/client/storage.rb
|
149
|
+
- lib/s3/settings.rb
|
150
|
+
- lib/s3/version.rb
|
151
|
+
- s3-client.gemspec
|
152
|
+
homepage: http://github.com/hiro-su/s3-client
|
153
|
+
licenses:
|
154
|
+
- MIT
|
155
|
+
metadata: {}
|
156
|
+
post_install_message:
|
157
|
+
rdoc_options: []
|
158
|
+
require_paths:
|
159
|
+
- lib
|
160
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
161
|
+
requirements:
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: '0'
|
165
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
requirements: []
|
171
|
+
rubyforge_project:
|
172
|
+
rubygems_version: 2.6.13
|
173
|
+
signing_key:
|
174
|
+
specification_version: 4
|
175
|
+
summary: Is is a simple AWS S3 library for Ruby
|
176
|
+
test_files: []
|