open-gcs 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 +7 -0
- data/.github/workflows/ruby-ci.yaml +27 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/open-gcs/ext.rb +13 -0
- data/lib/open-gcs/file.rb +33 -0
- data/lib/open-gcs/version.rb +3 -0
- data/lib/open-gcs.rb +49 -0
- data/open-gcs.gemspec +34 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b5bd0665602d92ae4d880849bd975fe1451e661d1480dee9a1c2f57f4f8f5e13
|
4
|
+
data.tar.gz: 7276cb5c8e8cd9189630be034983df5782f1843d173bddae2c3f0c74a022c4d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 52f8f17a7ad1313e812032714ce65f1c529eace8699e7afa6c756462b65e2f2d61d9cb59c1384f9dd3ec7ea4502088e996b6f5e9794550d92406ac47e540c9db
|
7
|
+
data.tar.gz: c6c7892f033ba01adff2fded91d3acc4a256ce10df8fd976d85e9e6a7550fdb5fdc4980e5d0efb214f544d982484c76f7146c322899ff3f9d147366fcc14ff96
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Ruby CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
ruby-version: ['3.2', '3.1', '3.0', '2.7', '2.6', '2.5']
|
17
|
+
|
18
|
+
steps:
|
19
|
+
- uses: actions/checkout@v3
|
20
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
21
|
+
uses: ruby/setup-ruby@v1
|
22
|
+
with:
|
23
|
+
ruby-version: ${{ matrix.ruby-version }}
|
24
|
+
- name: Install dependencies
|
25
|
+
run: bundle install
|
26
|
+
- name: Run tests
|
27
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Tomohiro Ogoke
|
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,55 @@
|
|
1
|
+
# OpenGCS
|
2
|
+
|
3
|
+
OpenGCS is a wrapper to open files from Google Cloud Storage (GCS), inspired by [OpenURI](https://github.com/ruby/open-uri).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'open-gcs'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install open-gcs
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Basic Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
OpenGCS.open_gcs('gs://your-bucket/your-file') { |file|
|
27
|
+
file.each_line { |line| puts line }
|
28
|
+
}
|
29
|
+
```
|
30
|
+
|
31
|
+
### Wrapping to `File::Open`
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class YourClass
|
35
|
+
using OpenGCSExt
|
36
|
+
|
37
|
+
File::Open('gs://your-bucket/your-file') { |file|
|
38
|
+
file.each_line { |line| puts line }
|
39
|
+
}
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
## Development
|
44
|
+
|
45
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
46
|
+
|
47
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tomog105/open-gcs.
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "open-gcs"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/open-gcs/ext.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "open-gcs"
|
2
|
+
|
3
|
+
module OpenGCSExt
|
4
|
+
refine File.singleton_class do
|
5
|
+
def open(uri, *rest, **options, &block)
|
6
|
+
if OpenGCS.get_path_info(uri).first == OpenGCS::TARGET_SCHEME
|
7
|
+
OpenGCS.open_gcs(uri, *rest, **options, &block)
|
8
|
+
else
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "google/cloud/storage"
|
2
|
+
require "tempfile"
|
3
|
+
|
4
|
+
module OpenGCS
|
5
|
+
module File
|
6
|
+
STORAGE_OPTION_KEYS = %i(
|
7
|
+
project_id
|
8
|
+
credentials
|
9
|
+
scope
|
10
|
+
retries
|
11
|
+
timeout
|
12
|
+
endpoint
|
13
|
+
project
|
14
|
+
keyfile
|
15
|
+
)
|
16
|
+
DOWNLOAD_OPTION_KEYS = %i(
|
17
|
+
verify
|
18
|
+
encryption_key
|
19
|
+
range
|
20
|
+
skip_decompress
|
21
|
+
)
|
22
|
+
OPTION_KEYS = STORAGE_OPTION_KEYS + DOWNLOAD_OPTION_KEYS
|
23
|
+
|
24
|
+
def self.download(bucket, filename, **options)
|
25
|
+
storage = Google::Cloud::Storage.new(**options.select { |o, v| v && STORAGE_OPTION_KEYS.include?(o) })
|
26
|
+
file = storage.bucket(bucket, skip_lookup: true).file(filename, skip_lookup: true)
|
27
|
+
|
28
|
+
tf = Tempfile.open(["open-gcs"])
|
29
|
+
file.download(tf.path, **options.select { |o, v| v && DOWNLOAD_OPTION_KEYS.include?(o) })
|
30
|
+
tf
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/open-gcs.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "open-gcs/file"
|
3
|
+
require "open-gcs/version"
|
4
|
+
|
5
|
+
module OpenGCS
|
6
|
+
TARGET_SCHEME = "gs"
|
7
|
+
|
8
|
+
def self.open_gcs(uri, *rest, **options, &block)
|
9
|
+
scheme, bucket, path = get_path_info(uri)
|
10
|
+
|
11
|
+
unless scheme == TARGET_SCHEME
|
12
|
+
raise(ArgumentError, "Invalid uri: #{uri} (This method is only support for gcs object uri.")
|
13
|
+
end
|
14
|
+
|
15
|
+
if String === rest.first || Integer === rest.first
|
16
|
+
mode = rest.first
|
17
|
+
else
|
18
|
+
mode = options[:mode]
|
19
|
+
end
|
20
|
+
if !mode.nil? && !mode.match?(/\Ar[bt]?(?:\Z|:([^:]+))/) && mode != ::File::RDONLY
|
21
|
+
raise(ArgumentError, "Invalid access mode: #{mode}. (This method is only support for read mode.)")
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
tf = File.download(bucket, path, **options.select { |key| File::OPTION_KEYS.include?(key) })
|
26
|
+
file = ::File.open(tf.path, *rest, **options.reject { |key| File::OPTION_KEYS.include?(key) })
|
27
|
+
if block_given?
|
28
|
+
begin
|
29
|
+
yield file
|
30
|
+
ensure
|
31
|
+
tf.close!
|
32
|
+
end
|
33
|
+
else
|
34
|
+
file
|
35
|
+
end
|
36
|
+
ensure
|
37
|
+
tf&.close if tf&.path
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.get_path_info(uri)
|
42
|
+
unless String === uri
|
43
|
+
[]
|
44
|
+
else
|
45
|
+
parsed_uri = URI.parse(uri)
|
46
|
+
[*parsed_uri.select(:scheme, :host), parsed_uri&.path.delete_prefix("/")]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/open-gcs.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "open-gcs/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "open-gcs"
|
8
|
+
spec.version = OpenGCS::VERSION
|
9
|
+
spec.authors = ["tomog105"]
|
10
|
+
spec.email = ["knowide@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{GCS file open wrapper.}
|
13
|
+
spec.description = %q{GCS file open wrapper.}
|
14
|
+
spec.homepage = "https://github.com/tomog105/open-gcs"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.required_ruby_version = '>= 2.5'
|
27
|
+
|
28
|
+
spec.add_dependency "google-cloud-storage", "~> 1.9"
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler"
|
31
|
+
spec.add_development_dependency "rake"
|
32
|
+
spec.add_development_dependency "test-unit", '~> 3.2', ">= 3.2.7"
|
33
|
+
spec.add_development_dependency "test-unit-rr"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: open-gcs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tomog105
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-06-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: google-cloud-storage
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: test-unit
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 3.2.7
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '3.2'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 3.2.7
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: test-unit-rr
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
description: GCS file open wrapper.
|
90
|
+
email:
|
91
|
+
- knowide@gmail.com
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- ".github/workflows/ruby-ci.yaml"
|
97
|
+
- ".gitignore"
|
98
|
+
- ".travis.yml"
|
99
|
+
- Gemfile
|
100
|
+
- LICENSE.txt
|
101
|
+
- README.md
|
102
|
+
- Rakefile
|
103
|
+
- bin/console
|
104
|
+
- bin/setup
|
105
|
+
- lib/open-gcs.rb
|
106
|
+
- lib/open-gcs/ext.rb
|
107
|
+
- lib/open-gcs/file.rb
|
108
|
+
- lib/open-gcs/version.rb
|
109
|
+
- open-gcs.gemspec
|
110
|
+
homepage: https://github.com/tomog105/open-gcs
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '2.5'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubygems_version: 3.4.10
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: GCS file open wrapper.
|
133
|
+
test_files: []
|