carrierwave-google-storage 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3fe891849a61908e41b35c935710bc4a569a5734
4
+ data.tar.gz: 4bb3cbf64e11b7d0332a31c704866119942469ea
5
+ SHA512:
6
+ metadata.gz: 09e09763d724d810621724e3f7dd3acfa2b257449232a0196598f041e79bceb6bb9cf74a278e8af0bc743168ab99f253f17cf173b6689e04b0d7b785d70c8a90
7
+ data.tar.gz: 106287b9e00fa42557e2af14da8b22b12c4221810790f1c42ceeefdc168c921dd7ed312f83bebf327e5faf1964c0fd12917cd45dd3e075154d21b27f6442a75d
data/.env.sample ADDED
@@ -0,0 +1,2 @@
1
+ GCLOUD_PROJECT=
2
+ GCLOUD_BUCKET=
@@ -0,0 +1,12 @@
1
+ {
2
+ "type": "service_account",
3
+ "project_id": "",
4
+ "private_key_id": "",
5
+ "private_key": "",
6
+ "client_email": "",
7
+ "client_id": "",
8
+ "auth_uri": "",
9
+ "token_uri": "",
10
+ "auth_provider_x509_cert_url": "",
11
+ "client_x509_cert_url": ""
12
+ }
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ *.gem
2
+ .env
3
+ /bin
4
+ /vendor
5
+ /uploads
6
+ /.bundle/
7
+ /.yardoc
8
+ /Gemfile.lock
9
+ /_yardoc/
10
+ /coverage/
11
+ /doc/
12
+ /pkg/
13
+ /spec/reports/
14
+ /tmp/
15
+ /.gcp-keyfile.json
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.0
5
+ before_install: gem install bundler -v 1.12.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-google-storage.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jasdeep Singh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Carrierwave Google Storage
2
+
3
+ [![Build Status](https://travis-ci.org/metaware/carrierwave-google-storage.svg?branch=master)](https://travis-ci.org/metaware/carrierwave-google-storage)
4
+ [![Code Climate](https://codeclimate.com/github/metaware/carrierwave-google-storage/badges/gpa.svg)](https://codeclimate.com/github/metaware/carrierwave-google-storage)
5
+
6
+ Use the official `gcloud` gem by Google for Google Cloud Storage, instead of Fog.
7
+
8
+ - No need to activate Interoperable Access on your project.
9
+ - Rely on Google's preferred authentication mechanism. ie: Service Accounts.
10
+
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'carrierwave-google-storage'
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```
23
+ CarrierWave.configure do |config|
24
+ config.storage = :gcloud
25
+ config.gcloud_bucket = 'your-bucket-name'
26
+ config.gcloud_bucket_is_public = true
27
+ config.gcloud_authenticated_url_expiration = 600
28
+
29
+ config.gcloud_attributes = {
30
+ expires: 600
31
+ }
32
+
33
+ config.gcloud_credentials = {
34
+ gcloud_project: 'gcp-project-name',
35
+ gcloud_keyfile: 'path-to-gcp-keyfile.json'
36
+ }
37
+ end
38
+ ```
39
+
40
+ ## How to get the Keyfile?
41
+
42
+ To generate a new keyfile, perform the following steps:
43
+
44
+ - Go to [Cloud Console > API Manager > Credentials](https://console.cloud.google.com/apis/credentials)
45
+ - Click Create Credentials > Service Account Key
46
+ - Service Account > New Service Account
47
+ - Give any name for "Service Account Name"
48
+ - Set Key type to JSON
49
+ - Click "Create"
50
+
51
+ Here's a quick GIF for those who are visual like myself:
52
+
53
+ ![](http://g.recordit.co/VjsK6CAUha.gif)
54
+
55
+ ## Contributing
56
+
57
+ ### Environment Variables and such
58
+
59
+ You should have access to a Google Cloud Platform account, as you'll need the keyfile and a project ID in order to contribute to the development of this gem. You'll need the the following files:
60
+
61
+ - `.gcp-keyfile.json` (sample file provided: `.gcp-keyfile.sample.json`)
62
+ - `.env` (sample file provided: `.env.sample`)
63
+
64
+ If you have any questions, please feel free to open up issues and/or PR's. Contributions are welcomed.
65
+
66
+ In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
67
+
68
+ You get extra attention, if your PR includes specs/tests.
69
+
70
+ - Fork or clone the project.
71
+ - Create your feature branch ($ git checkout -b my-new-feature)
72
+ - Install the dependencies by doing: $ bundle install in the project directory.
73
+ - Add your bug fixes or new feature code.
74
+ - New features should include new specs/tests.
75
+ - Bug fixes should ideally include exposing specs/tests.
76
+ - Commit your changes ($ git commit -am 'Add some feature')
77
+ - Push to the branch ($ git push origin my-new-feature)
78
+ - Open your Pull Request!
79
+
80
+ ## License
81
+
82
+ Copyright (c) 2016 [Jasdeep Singh](http://jasdeep.ca) ([Metaware Labs Inc](http://metawarelabs.com/))
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -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 'carrierwave/google/storage/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carrierwave-google-storage"
8
+ spec.version = Carrierwave::Google::Storage::VERSION
9
+ spec.authors = ["Jasdeep Singh"]
10
+ spec.email = ["narang.jasdeep@gmail.com"]
11
+
12
+ spec.summary = %q{Use gcloud for Google Cloud Storage support in CarrierWave.}
13
+ spec.description = %q{A slimmer alternative to using Fog for Google Cloud Storage support in CarrierWave. Heavily inspired from carrierwave-aws}
14
+ spec.homepage = "https://github.com/metaware/carrierwave-google-cloud"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'carrierwave', '~> 0.7'
23
+ spec.add_dependency 'gcloud', '~> 0.8.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.12"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry", "~> 0.10.3"
29
+ spec.add_development_dependency "uri-query_params", "~> 0.7.1"
30
+ end
@@ -0,0 +1,23 @@
1
+ require 'gcloud'
2
+ require 'carrierwave'
3
+ require 'carrierwave/google/storage/version'
4
+ require 'carrierwave/storage/gcloud'
5
+ require 'carrierwave/storage/gcloud_file'
6
+ require 'carrierwave/storage/gcloud_options'
7
+ require 'carrierwave/support/uri_filename'
8
+
9
+ class CarrierWave::Uploader::Base
10
+
11
+ ConfigurationError = Class.new(StandardError)
12
+
13
+ add_config :gcloud_attributes
14
+ add_config :gcloud_bucket
15
+ add_config :gcloud_bucket_is_public
16
+ add_config :gcloud_credentials
17
+ add_config :gcloud_authenticated_url_expiration
18
+
19
+ configure do |config|
20
+ config.storage_engines[:gcloud] = 'CarrierWave::Storage::Gcloud'
21
+ end
22
+
23
+ end
@@ -0,0 +1,7 @@
1
+ module Carrierwave
2
+ module Google
3
+ module Storage
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+
2
+ module CarrierWave
3
+ module Storage
4
+ class Gcloud < Abstract
5
+
6
+ def self.connection_cache
7
+ @connection_cache ||= {}
8
+ end
9
+
10
+ def self.clear_connection_cache!
11
+ @connection_cache = {}
12
+ end
13
+
14
+ def store!(file)
15
+ CarrierWave::Storage::GcloudFile.new(uploader, connection, uploader.store_path).tap do |gcloud_file|
16
+ gcloud_file.store(file)
17
+ end
18
+ end
19
+
20
+ def retrieve!(identifier)
21
+ CarrierWave::Storage::GcloudFile.new(uploader, connection, uploader.store_path(identifier)).retrieve
22
+ end
23
+
24
+ def connection
25
+ @connection ||= begin
26
+ cert_path = Gem.loaded_specs['google-api-client'].full_gem_path+'/lib/cacerts.pem'
27
+ ENV['SSL_CERT_FILE'] = cert_path
28
+ self.class.connection_cache[credentials] ||= ::Gcloud.new(credentials[:gcloud_project] || ENV["GCLOUD_PROJECT"], credentials[:gcloud_keyfile] || ENV["GCLOUD_KEYFILE"]).storage
29
+ end
30
+ end
31
+
32
+ def credentials
33
+ uploader.gcloud_credentials
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,102 @@
1
+ module CarrierWave
2
+ module Storage
3
+ class GcloudFile
4
+
5
+ attr_writer :file
6
+ attr_accessor :uploader, :connection, :path,
7
+ :gcloud_options, :file_exists
8
+
9
+ delegate :content_type, :size, to: :file
10
+
11
+ def initialize(uploader, connection, path)
12
+ @uploader = uploader
13
+ @connection = connection
14
+ @path = path
15
+ end
16
+
17
+ def file
18
+ by_verifying_existence { @file ||= bucket.file(path) }
19
+ end
20
+ alias_method :to_file, :file
21
+
22
+ def retrieve
23
+ by_verifying_existence { @file ||= bucket.file(path) }
24
+ self
25
+ end
26
+
27
+ def by_verifying_existence(&block)
28
+ begin
29
+ self.file_exists = true
30
+ yield
31
+ rescue Exception => exception
32
+ self.file_exists = false if (exception.class == ::Gcloud::Storage::ApiError) && (exception.message == "Not Found")
33
+ raise exception
34
+ end
35
+ end
36
+
37
+ def attributes
38
+ {
39
+ content_type: file.content_type,
40
+ size: file.size,
41
+ updated_at: file.updated_at.to_s,
42
+ etag: file.etag
43
+ }
44
+ end
45
+
46
+ def delete
47
+ deleted = file.delete
48
+ self.file_exists = false if deleted
49
+ deleted
50
+ end
51
+
52
+ def exists?
53
+ self.file_exists
54
+ end
55
+
56
+ def extension
57
+ elements = path.split('.')
58
+ elements.last if elements.size > 1
59
+ end
60
+
61
+ def filename(options = {})
62
+ CarrierWave::Support::UriFilename.filename(file.name)
63
+ end
64
+
65
+ def read
66
+ (file.download file.name, verify: :all).read
67
+ end
68
+
69
+ def store(new_file)
70
+ bucket.create_file new_file.path, "uploads/" + new_file.filename
71
+ self
72
+ end
73
+
74
+ def copy_to(new_path)
75
+ file.copy(new_path)
76
+ end
77
+
78
+ def authenticated_url(options = {})
79
+ file.signed_url
80
+ end
81
+
82
+ def public_url
83
+ if uploader.asset_host
84
+ "#{uploader.asset_host}/#{path}"
85
+ else
86
+ file.public_url.to_s
87
+ end
88
+ end
89
+
90
+ def url(options = {})
91
+ uploader.gcloud_bucket_is_public ? public_url : authenticated_url
92
+ end
93
+
94
+ private
95
+
96
+ def bucket
97
+ bucket ||= connection.bucket(uploader.gcloud_bucket)
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,34 @@
1
+ module CarrierWave
2
+ module Storage
3
+ class GcloudOptions
4
+ attr_reader :uploader
5
+
6
+ def initialize(uploader)
7
+ @uploader = uploader
8
+ end
9
+
10
+ def read_options
11
+ gcloud_read_options
12
+ end
13
+
14
+ def expiration_options(options = {})
15
+ uploader_expiration = uploader.gcloud_authenticated_url_expiration
16
+ { expires_in: uploader_expiration }.merge(options)
17
+ end
18
+
19
+ private
20
+
21
+ def gcloud_attributes
22
+ uploader.gcloud_attributes || {}
23
+ end
24
+
25
+ def gcloud_read_options
26
+ uploader.gcloud_read_options || {}
27
+ end
28
+
29
+ def gcloud_write_options
30
+ uploader.gcloud_write_options || {}
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ module CarrierWave
2
+ module Support
3
+ module UriFilename
4
+ def self.filename(url)
5
+ path = url.split('?').first
6
+
7
+ URI.decode(path).gsub(/.*\/(.*?$)/, '\1')
8
+ end
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-google-storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jasdeep Singh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: carrierwave
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gcloud
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.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.8.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.3
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.3
97
+ - !ruby/object:Gem::Dependency
98
+ name: uri-query_params
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.7.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.7.1
111
+ description: A slimmer alternative to using Fog for Google Cloud Storage support in
112
+ CarrierWave. Heavily inspired from carrierwave-aws
113
+ email:
114
+ - narang.jasdeep@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".env.sample"
120
+ - ".gcp-keyfile.sample.json"
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".travis.yml"
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - carrierwave-google-storage.gemspec
131
+ - lib/carrierwave-google-storage.rb
132
+ - lib/carrierwave/google/storage/version.rb
133
+ - lib/carrierwave/storage/gcloud.rb
134
+ - lib/carrierwave/storage/gcloud_file.rb
135
+ - lib/carrierwave/storage/gcloud_options.rb
136
+ - lib/carrierwave/support/uri_filename.rb
137
+ homepage: https://github.com/metaware/carrierwave-google-cloud
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.6
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Use gcloud for Google Cloud Storage support in CarrierWave.
161
+ test_files: []