carrierwave-swift-swauth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2013 DropMyEmail Pte Ltd
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,34 @@
1
+ # Carrierwave Swift Swauth
2
+
3
+ This gem is a [Carrierwave](https://github.com/jnicklas/carrierwave/) store for [OpenStack Swift](swift.openstack.org). This library uses [swauth](https://github.com/gholt/swauth) for authentication.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install carrierwave-swift-swauth
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ You should specify the following variables in your environment. We don't like storing passwords in the plain.
14
+
15
+ ```
16
+ export SWIFT_USER="user"
17
+ export SWIFT_PASSWORD="some-secret-password"
18
+ export SWIFT_URL="https://your-swift-server.com"
19
+ export SWIFT_TENANT="tenant-name"
20
+ export SWIFT_CONTAINER="container-name"
21
+ ```
22
+
23
+ Set the storage to be __swift_swauth__. The usual Paperclip configuration should be applicable.
24
+
25
+ ``` ruby
26
+ class User < ActiveRecord::Base
27
+ has_attached_file :avatar,
28
+ :storage => :swift_swauth
29
+ end
30
+ ```
31
+
32
+ ## License
33
+
34
+ Apache 2.0. See LICENSE for details.
@@ -0,0 +1,3 @@
1
+ require 'openstack'
2
+ require 'carrierwave/storage/swift_swauth'
3
+ require 'carrierwave/swift/swauth_client'
@@ -0,0 +1,52 @@
1
+ require 'carrierwave'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'swift', 'swauth_client'))
3
+
4
+ module CarrierWave
5
+ module Storage
6
+ class SwiftSwauth < Abstract
7
+
8
+ # Public: Stores the file on Swift
9
+ #
10
+ # Examples
11
+ #
12
+ # swift_swauth.store! <File>
13
+ # => nil
14
+ #
15
+ # Returns nothing
16
+ def store!(file)
17
+ write_opts = { content_type: file.content_type }
18
+ client.create_object(identifier, write_opts, file)
19
+ end
20
+
21
+ # Public: Returns the object given the identifier
22
+ #
23
+ # Examples
24
+ #
25
+ # swift_swauth.retrieve! 'foo'
26
+ # => <OpenStack::Swift::Container::Object>
27
+ #
28
+ # Returns an instance of OpenStack::Swift::COntainer::Object
29
+ def retrieve!(file_identifier)
30
+ client.object(file_identifier)
31
+ end
32
+
33
+ protected
34
+
35
+ # Private: Returns the swift client for performing the operations
36
+ #
37
+ # Examples
38
+ #
39
+ # swift_swauth.client
40
+ # => <Carrierwave::Swift::SwauthClient>
41
+ #
42
+ # Returns an instance of SwauthClient
43
+ def client
44
+ if @client.nil?
45
+ @client = CarrierWave::Swift::SwauthClient.new
46
+ end
47
+ @client
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,115 @@
1
+ # Public: Carrierwave storage adaptor for Openstack Swift
2
+ # Swift authentication is assumed to be on Swauth
3
+ module CarrierWave
4
+ module Swift
5
+ class SwauthClient
6
+ SERVICE_TYPE = 'object-store'
7
+
8
+ # Public: Constructor. Assumes all the needed variables are
9
+ # found in the environment
10
+ def initialize
11
+ @swift_opts = {
12
+ user: ENV['SWIFT_USER'] || '',
13
+ password: ENV['SWIFT_PASSWORD'] || '',
14
+ url: ENV['SWIFT_URL'] || '',
15
+ tenant: ENV['SWIFT_TENANT'] || '',
16
+ container: ENV['SWIFT_CONTAINER'] || ''
17
+ }
18
+ end
19
+
20
+ # Public: Checks if the object exists in the container
21
+ #
22
+ # path - The String which represents the object
23
+ #
24
+ # Examples
25
+ #
26
+ # swauth_client.object_exists 'foo'
27
+ # => true
28
+ #
29
+ # Returns a Boolean
30
+ def object_exists?(path)
31
+ container.object_exists? path
32
+ end
33
+
34
+ # Public: Creates the object given the path, options and data
35
+ #
36
+ # path - The String which represents the object
37
+ # opts - The Hash which contains the options for the object
38
+ # data - The File or String which represents the data
39
+ #
40
+ # Examples
41
+ #
42
+ # swauth_client.create_object 'foo', {}, File.open('foo.pdf')
43
+ # => nil
44
+ #
45
+ # Returns nothing
46
+ def create_object(path, opts={}, data)
47
+ container.create_object path, opts, data
48
+ end
49
+
50
+ # Public: Deletes the object given the path
51
+ #
52
+ # path - The String which represents the object
53
+ #
54
+ # Examples
55
+ #
56
+ # swauth_client.delete_object 'foo'
57
+ # => nil
58
+ #
59
+ # Returns nothing
60
+ def delete_object(path)
61
+ container.delete_object path
62
+ end
63
+
64
+ # Public: Returns the object given the path
65
+ #
66
+ # path - The String which represents the object
67
+ #
68
+ # Examples
69
+ #
70
+ # swauth_client.object 'foo'
71
+ # => <OpenStack::Swift::Container::Object>
72
+ #
73
+ # Returns an instance of OpenStack::Swift::Container::Object
74
+ def object(path)
75
+ container.object path
76
+ end
77
+
78
+ protected
79
+
80
+ # Private: Returns the container object for the connection
81
+ #
82
+ # Examples
83
+ #
84
+ # swauth_client.container
85
+ # => <OpenStack::Swift::Container>
86
+ #
87
+ # Returns an instance of OpenStack::Swift::Container
88
+ def container
89
+ if @connection.nil?
90
+ @connection = OpenStack::Connection.create(connection_opts)
91
+ end
92
+ @connection.container(@swift_opts[:container])
93
+ end
94
+
95
+ # Private: Returns the connection options
96
+ #
97
+ # Examples
98
+ #
99
+ # swauth_client.connection_opts
100
+ # => {...}
101
+ #
102
+ # Returns a Hash of options
103
+ def connection_opts
104
+ {
105
+ username: @swift_opts[:user],
106
+ api_key: @swift_opts[:password],
107
+ auth_url: @swift_opts[:url],
108
+ authtenant_name: @swift_opts[:tenant],
109
+ auth_method: @swift_opts[:password],
110
+ service_type: SERVICE_TYPE
111
+ }
112
+ end
113
+ end # class
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-swift-swauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Wong Liang Zan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: carrierwave
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda
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
+ description: A store for carrierwave that uses openstack swift. Authenication is assumed
63
+ to be swauth.
64
+ email:
65
+ - zan@liangzan.net
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/carrierwave/storage/swift_swauth.rb
71
+ - lib/carrierwave/swift/swauth_client.rb
72
+ - lib/carrierwave-swift-swauth.rb
73
+ - LICENSE
74
+ - README.md
75
+ homepage: https://github.com/DropMyEmail/carrierwave-swift-swauth
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: 1.3.6
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.24
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Carrierwave store for openstack swift with swauth authentication
99
+ test_files: []