cono_storage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: ce220c34c7d813e49d6e81220da7fdd36d46e792
4
+ data.tar.gz: 5eecd835848acd05941e04c147354af708e5f3ad
5
+ SHA512:
6
+ metadata.gz: 9233924460104eba2df07feaa73de195c0f97179c5768a67691d36634bfc468ec4ab9cc9b3ee2ba53fd16f372adb4477da025df60e3db55313f14bad1b8ac17d
7
+ data.tar.gz: 51c4d68a86faae9b24c2de748e48d38cae1672c71323e60e16a1b2e5e1bc1700c6c5dfb5297fd516e472597c9ba5d6ff9cbaea591c306e260c68c1f0720107ed
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cono_storage.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 kkosuge
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,58 @@
1
+ # ConoStorage
2
+
3
+ [![](https://objectstore-r1nd1001.cnode.jp/v1/b7daff98c2b748dfa6cb8240c8ac7c33/kksg/bg_main-visual.png)](https://objectstore-r1nd1001.cnode.jp/v1/b7daff98c2b748dfa6cb8240c8ac7c33/kksg/bg_main-visual.png)
4
+ https://objectstore-r1nd1001.cnode.jp/v1/b7daff98c2b748dfa6cb8240c8ac7c33/kksg/bg_main-visual.png
5
+ OpenStack Swift Client for ConoHa オブジェクトストレージ
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ require 'cono_storage'
11
+
12
+ client = ConoStorage.new(
13
+ tenant_id: 'b7daff9xxxxxxxxxxxxxxxxx',
14
+ username: 1111111,
15
+ password: '^passw0rd$',
16
+ endpoint: 'https://objectstore-r1nd1001.cnode.jp/v1/b7daff9xxxxxxxxxxxxxxxxx',
17
+ web_mode: true # Web公開モード
18
+ )
19
+
20
+ client.put_container('awsome_gifs')# => ConoStorage::Response
21
+ client.put_object('awsome_gifs','nyan.gif').url #=> "https://objectstore-...cnode.jp/.../awsome_gifs/nyan.gif"
22
+ client.put_object('awsome_gifs', 'wan.gif', hearders: { 'X-Delete-At' => "1170774000" } ) # Custom Headers
23
+ client.get_object('awsome_gifs', 'nyan.gif')
24
+ client.delete_object('awsome_gifs', 'nyan.gif')
25
+ client.delete_container('awsome_gifs').status #=> 204
26
+ ```
27
+
28
+ ## Documentation
29
+
30
+ - [オブジェクトストレージ APIリファレンス - ConoHa](https://www.conoha.jp/guide/guide.php?g=52)
31
+
32
+ ## Requirements
33
+
34
+ Ruby 2.1.0 or later
35
+
36
+ ## Installation
37
+
38
+ Add this line to your application's Gemfile:
39
+
40
+ ```ruby
41
+ gem 'cono_storage'
42
+ ```
43
+
44
+ And then execute:
45
+
46
+ $ bundle
47
+
48
+ Or install it yourself as:
49
+
50
+ $ gem install cono_storage
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/kkosuge/cono_storage/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cono_storage/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cono_storage"
8
+ spec.version = ConoStorage::VERSION
9
+ spec.authors = ["kkosuge"]
10
+ spec.email = ["root@kksg.net"]
11
+ spec.summary = %q{OpenStack Swift Client for ConoHa オブジェクトストレージ}
12
+ spec.description = %q{OpenStack Swift Client for ConoHa オブジェクトストレージ}
13
+ spec.homepage = "https://github.com/kkosuge/cono_storage"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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 "activesupport"
22
+ spec.add_dependency "faraday"
23
+ spec.add_dependency "faraday_middleware"
24
+ spec.add_dependency "mimemagic"
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,19 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+ require "mimemagic"
4
+ require "json"
5
+ require "time"
6
+ require 'active_support/core_ext/hash'
7
+ require "cono_storage/version"
8
+ require "cono_storage/client"
9
+ require "cono_storage/request_methods"
10
+
11
+ module ConoStorage
12
+
13
+ class << self
14
+ # Alias for ConoStorage::Client.new
15
+ def new(params)
16
+ Client.new(params)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,115 @@
1
+ require "cono_storage/response"
2
+ require "cono_storage/request_methods"
3
+
4
+ module ConoStorage
5
+ class Client
6
+
7
+ include RequestMethods
8
+
9
+ DEFAULT_HEADERS = {
10
+ "Accept" => "application/json",
11
+ "User-Agent" => "ConoStorage Ruby Gem v#{ConoStorage::VERSION}",
12
+ "Content-Type" => "application/json"
13
+ }
14
+
15
+ # @param auth_url [String] default: https://ident-r1nd1001.cnode.jp/v2.0
16
+ # @param web_mode [Boolean] true でコンテナやオブジェクトを作成/更新すると公開状態になります。
17
+ # @param tenant_id [String]
18
+ # @param username [String]
19
+ # @param password [String]
20
+ # @param endpoint [String]
21
+ def initialize(auth_url: 'https://ident-r1nd1001.cnode.jp/v2.0',
22
+ web_mode: false,
23
+ tenant_id: , username: , password: , endpoint: )
24
+ @auth_url = auth_url
25
+ @web_mode = web_mode
26
+ @tenant_id = tenant_id
27
+ @username = username
28
+ @password = password
29
+ @endpoint = endpoint.scan(/\A(http.+\/v1)/).first.first
30
+ end
31
+
32
+ %w( get head put delete post ).each do |method|
33
+ class_eval <<-RUBY
34
+ def #{method}(path, params, headers)
35
+ request(:#{method}, path, params, headers)
36
+ end
37
+ RUBY
38
+ end
39
+
40
+ def upload(path, file, params, headers)
41
+ unless file.is_a? File
42
+ file = File.open(file)
43
+ end
44
+
45
+ content_type = MimeMagic.by_magic(file).type
46
+
47
+ io = Faraday::UploadIO.new(file, content_type)
48
+ extra_headers = {
49
+ 'X-Detect-Content-Type' => 'True',
50
+ 'Transfer-Encoding' => 'chunked',
51
+ 'Content-Type' => content_type
52
+ }
53
+ path = path + '?' + params.to_query if params
54
+ request(:put, path, io, extra_headers.merge(headers))
55
+ end
56
+
57
+ def authorize
58
+ if @token.nil? || (Time.parse(@token['expires']) <= Time.now)
59
+ create_token
60
+ else
61
+ @token
62
+ end
63
+ end
64
+
65
+ def create_token
66
+ res = connection.post do |req|
67
+ req.url @auth_url + '/tokens'
68
+ req.body = auth_params.to_json
69
+ end
70
+ json = JSON.parse(res.body)
71
+ @token = json['access']['token']
72
+ end
73
+
74
+ def auth_params
75
+ {
76
+ auth: {
77
+ tenantId: @tenant_id,
78
+ passwordCredentials: {
79
+ username: @username,
80
+ password: @password
81
+ }
82
+ }
83
+ }
84
+ end
85
+
86
+ def default_headers
87
+ headers = DEFAULT_HEADERS.clone
88
+ headers['Content-Type'] = 'application/json'
89
+ if @web_mode
90
+ headers['X-Web-Mode'] = 'True'
91
+ headers['X-Container-Read'] = '.r:*'
92
+ end
93
+ headers
94
+ end
95
+
96
+ def request(request_method, path, params, headers)
97
+ authorize
98
+ Response.new(
99
+ connection.send(
100
+ request_method,
101
+ @endpoint + path,
102
+ params,
103
+ headers.merge('X-Auth-Token' => @token['id'])
104
+ )
105
+ )
106
+ end
107
+
108
+ def connection
109
+ @connection ||= Faraday.new(headers: default_headers) do |connection|
110
+ connection.request :json
111
+ connection.adapter Faraday.default_adapter
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,109 @@
1
+ module ConoStorage
2
+ module RequestMethods
3
+
4
+ # GET(アカウント)
5
+ #
6
+ # 指定したアカウント及びアカウントが保持するコンテナの詳細を取得します。
7
+ #
8
+ def get_account(params: {}, headers: {})
9
+ get("/#{@tenant_id}", params, headers)
10
+ end
11
+
12
+ # POST(アカウント)
13
+ #
14
+ # アカウントのメタデータを作成、更新、削除します。
15
+ #
16
+ def post_account(params: {}, headers: {})
17
+ post("/#{@tenant_id}", params, headers)
18
+ end
19
+
20
+ # HEAD(アカウント)
21
+ #
22
+ # アカウントのメタデータを取得します。
23
+ #
24
+ def head_account(params: {}, headers: {})
25
+ head("/#{@tenant_id}", params, headers)
26
+ end
27
+
28
+ # GET(コンテナ)
29
+ #
30
+ # コンテナの詳細と、コンテナ内のオブジェクトの一覧を取得します。
31
+ #
32
+ def get_container(container, params: {}, headers: {})
33
+ get("/#{@tenant_id}/#{container}", params, headers)
34
+ end
35
+
36
+ # PUT(コンテナ)
37
+ #
38
+ # コンテナの作成、または既存のコンテナの更新をします。
39
+ #
40
+ def put_container(container, params: {}, headers: {})
41
+ put("/#{@tenant_id}/#{container}", params, headers)
42
+ end
43
+
44
+ # DELETE(コンテナ)
45
+ #
46
+ # コンテナを削除します。
47
+ #
48
+ def delete_container(container, params: {}, headers: {})
49
+ delete("/#{@tenant_id}/#{container}", params, headers)
50
+ end
51
+
52
+ # POST(コンテナ)
53
+ #
54
+ # コンテナのメタデータを作成、更新、削除します。
55
+ #
56
+ def post_container(container, params: {}, headers: {})
57
+ post("/#{@tenant_id}/#{container}", params, headers)
58
+ end
59
+
60
+ # HEAD(コンテナ)
61
+ #
62
+ # コンテナのメタデータを取得します。
63
+ #
64
+ def head_container(container, params: {}, headers: {})
65
+ head("/#{@tenant_id}/#{container}", params, headers)
66
+ end
67
+
68
+ # GET(オブジェクト)
69
+ #
70
+ # オブジェクトのダウンロードと、オブジェクトのメタデータの取得をします。
71
+ #
72
+ def get_object(container, object, params: {}, headers: {})
73
+ get("/#{@tenant_id}/#{container}/#{object}", params, headers)
74
+ end
75
+
76
+ # PUT(オブジェクト)
77
+ #
78
+ # メタデータを含むオブジェクトを作成します。
79
+ # オブジェクトが既に存在する場合はオブジェクト、またはメタデータを置き換えます。
80
+ #
81
+ def put_object(container, object, file, params: {}, headers: {})
82
+ upload("/#{@tenant_id}/#{container}/#{object}", file, params, headers)
83
+ end
84
+
85
+ # DELETE(オブジェクト)
86
+ #
87
+ # アップロード済みのオブジェクトを削除します。
88
+ #
89
+ def delete_object(container, object, params: {}, headers: {})
90
+ delete("/#{@tenant_id}/#{container}/#{object}", params, headers)
91
+ end
92
+
93
+ # POST(オブジェクト)
94
+ #
95
+ # オブジェクトにメタデータを付与、またはメタデータを更新します。
96
+ #
97
+ def post_object(container, object, params: {}, headers: {})
98
+ post("/#{@tenant_id}/#{container}/#{object}", params, headers)
99
+ end
100
+
101
+ # HEAD(オブジェクト)
102
+ #
103
+ # オブジェクトのメタデータを取得します。
104
+ #
105
+ def head_object(container, object, params: {}, headers: {})
106
+ head("/#{@tenant_id}/#{container}/#{object}", params, headers)
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,53 @@
1
+ module ConoStorage
2
+ class Response
3
+ attr_accessor :faraday_response
4
+
5
+ def initialize(faraday_response)
6
+ @body = faraday_response.body
7
+ @headers = faraday_response.headers
8
+ @status = faraday_response.status
9
+ @url = faraday_response.env.url.to_s
10
+ @faraday_response = faraday_response
11
+ end
12
+
13
+ # Conostorage::Response#status
14
+ #
15
+ # @return [Fixnum]
16
+ #
17
+ def status
18
+ @status
19
+ end
20
+
21
+ # Conostorage::Response#body
22
+ #
23
+ # @return [Hash]
24
+ # @return [Array]
25
+ # @return [String] JSON以外で返ってきた場合はそのまま出力
26
+ #
27
+ def body
28
+ if @body and @body.size > 0
29
+ JSON.parse(@body)
30
+ else
31
+ @body
32
+ end
33
+ rescue JSON::ParserError
34
+ @body
35
+ end
36
+
37
+ # Conostorage::Response#headers
38
+ #
39
+ # @return [Hash]
40
+ #
41
+ def headers
42
+ @headers
43
+ end
44
+
45
+ # Conostorage::Response#url
46
+ #
47
+ # @return [String] Web ModeにするとアクセスできるオブジェクトのURL
48
+ #
49
+ def url
50
+ @url
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module ConoStorage
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cono_storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kkosuge
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mimemagic
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: OpenStack Swift Client for ConoHa オブジェクトストレージ
98
+ email:
99
+ - root@kksg.net
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - cono_storage.gemspec
110
+ - lib/cono_storage.rb
111
+ - lib/cono_storage/client.rb
112
+ - lib/cono_storage/request_methods.rb
113
+ - lib/cono_storage/response.rb
114
+ - lib/cono_storage/version.rb
115
+ homepage: https://github.com/kkosuge/cono_storage
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.2.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: OpenStack Swift Client for ConoHa オブジェクトストレージ
139
+ test_files: []
140
+ has_rdoc: