bfs-gs 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22b3892b324405cf0bb342b72dba4e6bf3cd8c16943af7b225f8d2367c7a5ba4
4
- data.tar.gz: 1b6902a3577e89bbed1a0357a26327dca0cebabc8663322014501c82b7ae687b
3
+ metadata.gz: 15b66903e512e68c645067504a13c6e67b49892291b0c5c6e8efe620f0add955
4
+ data.tar.gz: 5ac5868a81d4631356ab1980deb860ef39dd59c9b39b15e4e681db8855c6b854
5
5
  SHA512:
6
- metadata.gz: edd42acc0466e51285005fdeaf66aceb6de7287c9ea430f80a245221c2a224f75e11aeed7e937ad12d9e5a1cd69428e3d9d7840f7ffce52d957011f2541fac1c
7
- data.tar.gz: ec15157af7129148f231b3e91a771cea2bbb4b7c4f5b3cd47e9d92a808bf2210d56fea54cdb0a6288a07d8184fb8a900a2f949d39327c65ec53e052c45423a95
6
+ metadata.gz: 39bf4f7cda53d992997b65c6a8fc4adf8015616e99787c3c981e03020df2969a4d9bf6c2914d872074ab420d76748fd06a91342ef44e32c956fa8dccacc02fd3
7
+ data.tar.gz: '01924ee8d89b523203dc4aa02ebb2cb0b25d82233d5503af95274524a65634d8660abe5632c1ddbbf88a11befa54beaabab4650152071448f9f91912fa7c8e24'
data/lib/bfs/bucket/gs.rb CHANGED
@@ -15,6 +15,7 @@ module BFS
15
15
  # @option opts [String] :project_id project ID. Defaults to GCP_PROJECT env var. Required.
16
16
  # @option opts [String, Hash, Google::Auth::Credentials] :credentials
17
17
  # the path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object.
18
+ # @option opts [String] :prefix custom namespace within the bucket
18
19
  # @option opts [Integer] :retries number of times to retry requests. Default: 3.
19
20
  # @option opts [Integer] :timeout request timeout, in seconds.
20
21
  # @option opts [String] :acl set the default ACL.
@@ -27,8 +28,9 @@ module BFS
27
28
  end
28
29
  opts[:project_id] ||= ENV['GCP_PROJECT'] || ENV['GCLOUD_PROJECT']
29
30
 
30
- acl = opts.delete(:acl)
31
- client = opts.delete(:client) || Google::Cloud::Storage.new(opts)
31
+ @prefix = opts.delete(:prefix)
32
+ acl = opts.delete(:acl)
33
+ client = opts.delete(:client) || Google::Cloud::Storage.new(opts)
32
34
 
33
35
  @name = name.to_s
34
36
  @bucket = client.bucket(@name)
@@ -37,25 +39,31 @@ module BFS
37
39
 
38
40
  # Lists the contents of a bucket using a glob pattern
39
41
  def ls(pattern='**/*', opts={})
42
+ prefix = pattern[%r{^[^\*\?\{\}\[\]]+/}]
43
+ prefix = File.join(*[@prefix, prefix].compact) if @prefix
44
+ opts = opts.merge(prefix: prefix) if prefix
45
+
40
46
  Enumerator.new do |y|
41
- @bucket.files(opts).each do |file|
42
- y << file.name if File.fnmatch?(pattern, file.name, File::FNM_PATHNAME)
47
+ @bucket.files(opts).all do |file|
48
+ name = trim_prefix(file.name)
49
+ y << name if File.fnmatch?(pattern, name, File::FNM_PATHNAME)
43
50
  end
44
51
  end
45
52
  end
46
53
 
47
54
  # Info returns the object info
48
55
  def info(path, _opts={})
49
- path = norm_path(path)
56
+ path = full_path(path)
50
57
  file = @bucket.file(path)
51
- raise BFS::FileNotFound, path unless file
58
+ raise BFS::FileNotFound, trim_prefix(path) unless file
52
59
 
53
- BFS::FileInfo.new(path, file.size, file.updated_at, file.content_type, file.metadata)
60
+ name = trim_prefix(file.name)
61
+ BFS::FileInfo.new(name, file.size, file.updated_at.to_time, file.content_type, file.metadata)
54
62
  end
55
63
 
56
64
  # Creates a new file and opens it for writing
57
65
  def create(path, opts={}, &block)
58
- path = norm_path(path)
66
+ path = full_path(path)
59
67
  temp = BFS::TempWriter.new(path) do |t|
60
68
  File.open(t, binmode: true) do |file|
61
69
  @bucket.create_file(file, path, opts)
@@ -72,9 +80,9 @@ module BFS
72
80
 
73
81
  # Opens an existing file for reading
74
82
  def open(path, opts={}, &block)
75
- path = norm_path(path)
83
+ path = full_path(path)
76
84
  file = @bucket.file(path)
77
- raise BFS::FileNotFound, path unless file
85
+ raise BFS::FileNotFound, trim_prefix(path) unless file
78
86
 
79
87
  temp = Tempfile.new(File.basename(path), binmode: true)
80
88
  temp.close
@@ -85,18 +93,18 @@ module BFS
85
93
 
86
94
  # Deletes a file.
87
95
  def rm(path, opts={})
88
- path = norm_path(path)
96
+ path = full_path(path)
89
97
  file = @bucket.file(path)
90
98
  file.delete(opts) if file
91
99
  end
92
100
 
93
101
  # Copies a file.
94
102
  def cp(src, dst, opts={})
95
- src = norm_path(src)
103
+ src = full_path(src)
96
104
  file = @bucket.file(src)
97
- raise BFS::FileNotFound, src unless file
105
+ raise BFS::FileNotFound, trim_prefix(src) unless file
98
106
 
99
- file.copy(norm_path(dst), opts)
107
+ file.copy(full_path(dst), opts)
100
108
  end
101
109
  end
102
110
  end
@@ -1,55 +1,41 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe BFS::Bucket::GS do
4
- let(:contents) { {} }
5
- let(:mock_client) { double Google::Cloud::Storage, bucket: mock_bucket }
3
+ # silence warnings
4
+ module Google::Auth::CredentialsLoader
5
+ def warn_if_cloud_sdk_credentials(*); end
6
+ module_function :warn_if_cloud_sdk_credentials # rubocop:disable Style/AccessModifierDeclarations
7
+ end
6
8
 
7
- let :mock_bucket do
8
- bucket = double Google::Cloud::Storage::Bucket,
9
- default_acl: double(Google::Cloud::Storage::Bucket::Acl)
10
- allow(bucket).to receive(:create_file) do |io, name, _|
11
- contents[name] = double_file(name, io.read)
12
- end
13
- allow(bucket).to receive(:files) do |_|
14
- contents.values
15
- end
16
- allow(bucket).to receive(:file) do |name|
17
- contents[name]
18
- end
19
- bucket
9
+ sandbox = { project: 'bsm-affiliates', bucket: 'bsm-bfs-unittest' }.freeze
10
+ run_spec = \
11
+ begin
12
+ s = Google::Cloud::Storage.new(project_id: sandbox[:project])
13
+ !s.bucket(sandbox[:bucket]).nil?
14
+ rescue Signet::AuthorizationError
15
+ false
20
16
  end
21
17
 
22
- def double_file(name, data)
23
- file = double Google::Cloud::Storage::File,
24
- name: name,
25
- data: data,
26
- size: data.bytesize,
27
- content_type: 'text/plain',
28
- metadata: {},
29
- updated_at: Time.now
30
- allow(file).to receive(:download) do |path, _|
31
- File.open(path, 'wb') {|f| f.write file.data }
32
- end
33
- allow(file).to receive(:delete) do |_|
34
- contents.delete(file.name)
35
- true
36
- end
37
- allow(file).to receive(:copy) do |dst, _|
38
- contents[dst] = double_file(dst, file.data)
39
- end
40
- file
18
+ RSpec.describe BFS::Bucket::GS, if: run_spec do
19
+ let(:prefix) { "x/#{SecureRandom.uuid}/" }
20
+
21
+ subject do
22
+ described_class.new sandbox[:bucket], project_id: sandbox[:project], prefix: prefix
23
+ end
24
+ after :all do
25
+ bucket = described_class.new sandbox[:bucket], project_id: sandbox[:project], prefix: 'x/'
26
+ bucket.ls.each {|name| bucket.rm(name) }
41
27
  end
42
28
 
43
- subject { described_class.new 'mock-bucket', client: mock_client }
44
29
  it_behaves_like 'a bucket'
45
30
 
46
31
  it 'should resolve from URL' do
47
- expect(Google::Cloud::Storage).to receive(:new).with(project_id: 'my-project').and_return(mock_client)
48
- expect(mock_client).to receive(:bucket).with('mock-bucket').and_return(mock_bucket)
49
- expect(mock_bucket.default_acl).to receive(:private!).with(no_args)
50
-
51
- bucket = BFS.resolve('gs://mock-bucket?acl=private&project_id=my-project')
32
+ bucket = BFS.resolve("gs://#{sandbox[:bucket]}?acl=private&project_id=#{sandbox[:project]}")
52
33
  expect(bucket).to be_instance_of(described_class)
53
- expect(bucket.name).to eq('mock-bucket')
34
+ expect(bucket.name).to eq(sandbox[:bucket])
35
+ end
36
+
37
+ it 'should enumerate over a large number of files' do
38
+ bucket = described_class.new sandbox[:bucket], project_id: sandbox[:project], prefix: 'm/'
39
+ expect(bucket.ls('**/*').count).to eq(2121)
54
40
  end
55
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bfs-gs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitrij Denissenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-26 00:00:00.000000000 Z
11
+ date: 2018-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bfs
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.3.3
19
+ version: 0.3.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.3.3
26
+ version: 0.3.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: google-cloud-storage
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  version: '0'
69
69
  requirements: []
70
70
  rubyforge_project:
71
- rubygems_version: 2.7.7
71
+ rubygems_version: 2.7.6
72
72
  signing_key:
73
73
  specification_version: 4
74
74
  summary: GS bucket adapter for bfs