Class: BFS::Bucket::GS
- Inherits:
-
Abstract
- Object
- Abstract
- BFS::Bucket::GS
- Defined in:
- lib/bfs/bucket/gs.rb
Overview
GS buckets are operating on Google Cloud Storage
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#cp(src, dst, **opts) ⇒ Object
Copies a file.
-
#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object
Creates a new file and opens it for writing.
-
#glob(pattern = '**/*', **opts) ⇒ Object
Iterates over the contents of a bucket using a glob pattern.
-
#info(path, **_opts) ⇒ Object
Info returns the object info.
-
#initialize(name, prefix: nil, acl: nil, client: nil, **opts) ⇒ GS
constructor
Initializes a new GoogleCloudStorage bucket.
-
#ls(pattern = '**/*', **opts) ⇒ Object
Lists the contents of a bucket using a glob pattern.
-
#open(path, encoding: self.encoding, tempdir: nil, **opts, &block) ⇒ Object
Opens an existing file for reading.
-
#rm(path, **opts) ⇒ Object
Deletes a file.
Constructor Details
#initialize(name, prefix: nil, acl: nil, client: nil, **opts) ⇒ GS
Initializes a new GoogleCloudStorage bucket.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/bfs/bucket/gs.rb', line 23 def initialize(name, prefix: nil, acl: nil, client: nil, **opts) super(**opts) @prefix = prefix client ||= Google::Cloud::Storage.new(**opts) @name = name.to_s @bucket = client.bucket(@name) @bucket.default_acl.send(:"#{acl}!") if @bucket.default_acl.respond_to?(:"#{acl}!") end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/bfs/bucket/gs.rb', line 8 def name @name end |
Instance Method Details
#cp(src, dst, **opts) ⇒ Object
Copies a file.
94 95 96 97 98 99 100 |
# File 'lib/bfs/bucket/gs.rb', line 94 def cp(src, dst, **opts) src = full_path(src) file = @bucket.file(src) raise BFS::FileNotFound, trim_prefix(src) unless file file.copy(full_path(dst), **opts) end |
#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object
Creates a new file and opens it for writing
63 64 65 66 67 68 69 70 71 |
# File 'lib/bfs/bucket/gs.rb', line 63 def create(path, encoding: self.encoding, perm: self.perm, **opts, &block) opts[:metadata] = (opts[:metadata]) path = full_path(path) BFS::Writer.new(path, encoding: encoding, perm: perm) do |t| File.open(t, encoding: encoding) do |file| @bucket.create_file(file, path, **opts) end end.perform(&block) end |
#glob(pattern = '**/*', **opts) ⇒ Object
Iterates over the contents of a bucket using a glob pattern
44 45 46 47 48 49 50 |
# File 'lib/bfs/bucket/gs.rb', line 44 def glob(pattern = '**/*', **opts) Enumerator.new do |acc| walk(pattern, **opts) do |name, file| acc << file_info(name, file) end end end |
#info(path, **_opts) ⇒ Object
Info returns the object info
53 54 55 56 57 58 59 60 |
# File 'lib/bfs/bucket/gs.rb', line 53 def info(path, **_opts) path = full_path(path) file = @bucket.file(path) raise BFS::FileNotFound, trim_prefix(path) unless file name = trim_prefix(file.name) file_info(name, file) end |
#ls(pattern = '**/*', **opts) ⇒ Object
Lists the contents of a bucket using a glob pattern
35 36 37 38 39 40 41 |
# File 'lib/bfs/bucket/gs.rb', line 35 def ls(pattern = '**/*', **opts) Enumerator.new do |acc| walk(pattern, **opts) do |name, _| acc << name end end end |
#open(path, encoding: self.encoding, tempdir: nil, **opts, &block) ⇒ Object
Opens an existing file for reading
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bfs/bucket/gs.rb', line 74 def open(path, encoding: self.encoding, tempdir: nil, **opts, &block) path = full_path(path) file = @bucket.file(path) raise BFS::FileNotFound, trim_prefix(path) unless file temp = Tempfile.new(File.basename(path), tempdir, encoding: encoding) temp.close file.download(temp.path, **opts) File.open(temp.path, encoding: encoding, &block) end |
#rm(path, **opts) ⇒ Object
Deletes a file.
87 88 89 90 91 |
# File 'lib/bfs/bucket/gs.rb', line 87 def rm(path, **opts) path = full_path(path) file = @bucket.file(path) file&.delete(**opts) end |