Class: BFS::Bucket::GS

Inherits:
Abstract
  • Object
show all
Defined in:
lib/bfs/bucket/gs.rb

Overview

GS buckets are operating on Google Cloud Storage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, prefix: nil, acl: nil, client: nil, **opts) ⇒ GS

Initializes a new GoogleCloudStorage bucket.

Parameters:

  • name (String)

    the bucket name.

  • opts (Hash)

    options.

Options Hash (**opts):

  • :project_id (String)

    project ID. Defaults to GCP_PROJECT env var.

  • :credentials (String, Hash, Google::Auth::Credentials)

    the path to the keyfile as a String, the contents of the keyfile as a Hash, or a Google::Auth::Credentials object.

  • :prefix (String)

    custom namespace within the bucket

  • :retries (Integer)

    number of times to retry requests. Default: 3.

  • :timeout (Integer)

    request timeout, in seconds.

  • :acl (String)

    set the default ACL.

  • :client (Google::Cloud::Storage)

    custom client.

  • :encoding (String)

    Custom encoding.



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

#nameObject (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.

Raises:

  • (BFS::FileNotFound)


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] = 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

#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

Raises:

  • (BFS::FileNotFound)


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

Raises:

  • (BFS::FileNotFound)


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