module Google::Cloud::Gemserver::GCS

# Google Cloud Storage

Interacts with Google Cloud Storage by providing methods that upload and download files to and from Google Cloud Storage.

Public Class Methods

bucket() click to toggle source

@private Fetches the bucket used to store gem files for the gemserver. If it does not exist a bucket is created.

@return [Google::Cloud::Storage::Bucket]

# File lib/google/cloud/gemserver/gcs.rb, line 53
def self.bucket
  return unless proj_id
  bucket = cs.bucket proj_id
  bucket ? bucket : cs.create_bucket(proj_id)
end
copy_to_host(path) click to toggle source

@private Downloads a given file from Google Cloud Storage.

@param [String] path Path to the file.

# File lib/google/cloud/gemserver/gcs.rb, line 146
def self.copy_to_host path
  return unless proj_id
  file = get_file path
  folder = extract_dir path
  begin
    FileUtils.mkpath(folder) unless Dir.exist?(folder)
    file.download path
  rescue
    puts "Could not download #{file.name}." if file
  end
end
cs() click to toggle source

@private Creates a Google::Cloud::Storage::Project object with the current project ID.

@return [Google::Cloud::Storage::Project]

# File lib/google/cloud/gemserver/gcs.rb, line 43
def self.cs
  return unless proj_id
  Google::Cloud::Storage.new project: proj_id, keyfile: ENV["GOOGLE_APPLICATION_CREDENTIALS"]
end
delete_file(file) click to toggle source

Deletes a given file from Google Cloud Storage.

@param [String] file Name of the file to be deleted.

# File lib/google/cloud/gemserver/gcs.rb, line 90
def self.delete_file file
  return unless proj_id
  get_file(file).delete
end
extract_dir(path) click to toggle source

@private Extracts the parent directory from a file path

@param [String] path Path of the file.

@return [String]

# File lib/google/cloud/gemserver/gcs.rb, line 164
def self.extract_dir path
  parts = path.split("/")
  parts.map { |x| x != parts.last ? x : nil }.join("/")
end
files(prefix = nil) click to toggle source

@private Retrieves all files in the bucket corresponding to the project the gemserver was deployed. If specified, only files with a certain prefix will be retrieved.

@param [String] prefix Prefix of the file name. Optional

@return [Google::Cloud::Storage::File::List]

# File lib/google/cloud/gemserver/gcs.rb, line 103
def self.files prefix = nil
  return unless proj_id
  bucket.files prefix: prefix
end
get_file(file) click to toggle source

Retrieves a file from Google Cloud Storage from a project's corresponding bucket.

@param [String] file Name of the file to be retrieved.

@return [Google::Cloud::Storage::File]

# File lib/google/cloud/gemserver/gcs.rb, line 66
def self.get_file file
  return unless proj_id
  bucket.file file
end
on_gcs?(file_path) click to toggle source

@private Checks if a given file exists on Google Cloud Storage.

@param [String] file_path Path of the file on Google Cloud Storage.

@return [Boolean]

# File lib/google/cloud/gemserver/gcs.rb, line 137
def self.on_gcs? file_path
  return false unless proj_id
  get_file(file_path) != nil
end
proj_id() click to toggle source

@private Fetches the project ID of the Google Cloud Platform project the gemserver was deployed to.

@return [String]

# File lib/google/cloud/gemserver/gcs.rb, line 34
def self.proj_id
  Google::Cloud::Gemserver::Configuration.new[:proj_id]
end
sync(file_path) click to toggle source

@private Checks if a file exists on both Google Cloud Storage and the local file system. If the file is on Cloud Storage, but missing on the file system it will be downloaded.

@param [String] file_path File path of the file to be synced.

@return [Boolean]

# File lib/google/cloud/gemserver/gcs.rb, line 116
def self.sync file_path
  return true unless proj_id
  on_cloud = on_gcs? file_path
  on_host = File.exist? file_path

  if on_cloud && !on_host
    copy_to_host file_path
    true
  elsif on_cloud && on_host
    true
  else
    false
  end
end
upload(file, dest = nil) click to toggle source

Uploads a given file to a project's corresponding bucket on Google Cloud Storage. A destination path of the file can be provided. By default the path of the file is the same on Google Cloud Storage.

@param [String] file Path to the file to be uploaded. @param [String] dest Destination path of the file on Google Cloud Storage. Optional.

@return [Google::Cloud::Storage::File]

# File lib/google/cloud/gemserver/gcs.rb, line 81
def self.upload file, dest = nil
  return unless proj_id
  bucket.create_file file, dest
end