Class: Storazzo::GCS::Client

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/storazzo/gcs/client.rb

Constant Summary

Constants included from Colors

Colors::PREPEND_ME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#bug, #deb, #err, #fatal, #if_deb?, #linux?, #mac?, #ppp, #pverbose, #slugify, #warn

Methods included from Colors

#azure, #blue, #deb2, #gray, #green, #orange, #pgreen, #pred, #purple, #pwhite, #pyellow, #red, #white, #yellow

Constructor Details

#initialize(project_id = nil, bucket_name = nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
# File 'lib/storazzo/gcs/client.rb', line 13

def initialize(project_id = nil, bucket_name = nil)
  @project_id = project_id || autodetect_project_id
  @bucket_name = bucket_name || ENV['GCS_BUCKET'] || "#{@project_id}-storazzo"
  deb "GCS Client initialized for project: #{@project_id}, bucket: #{@bucket_name}"
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



11
12
13
# File 'lib/storazzo/gcs/client.rb', line 11

def bucket_name
  @bucket_name
end

#project_idObject (readonly)

Returns the value of attribute project_id.



11
12
13
# File 'lib/storazzo/gcs/client.rb', line 11

def project_id
  @project_id
end

#storageObject (readonly)

Returns the value of attribute storage.



11
12
13
# File 'lib/storazzo/gcs/client.rb', line 11

def storage
  @storage
end

Instance Method Details

#autodetect_project_idObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/storazzo/gcs/client.rb', line 34

def autodetect_project_id
  # Priority 1: Environment Variable
  return ENV['GOOGLE_CLOUD_PROJECT'] if ENV['GOOGLE_CLOUD_PROJECT']
  
  # Priority 2: Config file
  config = Storazzo::RicDiskConfig.instance
  config.load
  return config.project_id if config.project_id && config.project_id != 'YOUR-PROJECT-ID'

  # Priority 3: GCloud CLI default
  begin
    `gcloud config get-value project 2>/dev/null`.strip
  rescue StandardError
    nil
  end
end

#bucket_exists?(bucket_name) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/storazzo/gcs/client.rb', line 51

def bucket_exists?(bucket_name)
  bucket_name = bucket_name.gsub('gs://', '').split('/').first
  !storage.bucket(bucket_name).nil?
rescue StandardError => e
  warn "Error checking bucket #{bucket_name}: #{e.message}"
  false
end

#download_file(bucket_name, remote_path, local_path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/storazzo/gcs/client.rb', line 72

def download_file(bucket_name, remote_path, local_path)
  bucket_name = bucket_name.gsub('gs://', '').split('/').first
  bucket = storage.bucket(bucket_name)
  raise "Bucket #{bucket_name} not found!" unless bucket

  file = bucket.file(remote_path)
  raise "Remote file #{remote_path} not found in bucket #{bucket_name}!" unless file

  deb "Downloading gs://#{bucket_name}/#{remote_path} to #{local_path}..."
  file.download(local_path)
end

#ensure_bucket_existsObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/storazzo/gcs/client.rb', line 23

def ensure_bucket_exists
  return true if bucket_exists?(@bucket_name)

  deb "Bucket #{@bucket_name} not found. Creating it in project #{@project_id}..."
  storage.create_bucket(@bucket_name)
  true
rescue StandardError => e
  err "Failed to ensure/create bucket #{@bucket_name}: #{e.message}"
  false
end

#list_bucketsObject



59
60
61
# File 'lib/storazzo/gcs/client.rb', line 59

def list_buckets
  storage.buckets.map(&:name)
end

#upload_file(local_path, bucket_name, remote_path) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/storazzo/gcs/client.rb', line 63

def upload_file(local_path, bucket_name, remote_path)
  bucket_name = bucket_name.gsub('gs://', '').split('/').first
  bucket = storage.bucket(bucket_name)
  raise "Bucket #{bucket_name} not found!" unless bucket

  deb "Uploading #{local_path} to gs://#{bucket_name}/#{remote_path}..."
  bucket.create_file(local_path, remote_path)
end