class BFS::Bucket::GS
GS
buckets are operating on Google Cloud Storage
Attributes
Public Class Methods
Initializes a new GoogleCloudStorage bucket.
@param [String] name the bucket name. @param [Hash] opts options. @option opts [String] :project_id project ID. Defaults to GCP_PROJECT env var. @option opts [String, Hash, Google::Auth::Credentials] :credentials
the path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object.
@option opts [String] :prefix custom namespace within the bucket @option opts [Integer] :retries number of times to retry requests. Default: 3. @option opts [Integer] :timeout request timeout, in seconds. @option opts [String] :acl set the default ACL. @option opts [Google::Cloud::Storage] :client custom client. @option opts [String] :encoding Custom encoding.
# 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
Public Instance Methods
Copies a file.
# 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
Creates a new file and opens it for writing
# File lib/bfs/bucket/gs.rb, line 63 def create(path, encoding: self.encoding, perm: self.perm, **opts, &block) opts[:metadata] = norm_meta(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
Iterates over the contents of a bucket using a glob pattern
# 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 returns the object info
# 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
Lists the contents of a bucket using a glob pattern
# 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
Opens an existing file for reading
# 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
Deletes a file.
# File lib/bfs/bucket/gs.rb, line 87 def rm(path, **opts) path = full_path(path) file = @bucket.file(path) file&.delete(**opts) end
Private Instance Methods
# File lib/bfs/bucket/gs.rb, line 115 def file_info(name, file) BFS::FileInfo.new(path: name, size: file.size, mtime: file.updated_at.to_time, content_type: file.content_type, metadata: norm_meta(file.metadata)) end
# File lib/bfs/bucket/gs.rb, line 104 def walk(pattern, **opts) prefix = pattern[%r{^[^*?\{\}\[\]]+/}] prefix = File.join(*[@prefix, prefix].compact) if @prefix opts = opts.merge(prefix: prefix) if prefix @bucket.files(**opts).all do |file| name = trim_prefix(file.name) yield(name, file) if File.fnmatch?(pattern, name, File::FNM_PATHNAME) end end