module Google::Cloud::Gemserver::GCS
Interacts with Google
Cloud
Storage by providing methods that upload and download files to and from Google
Cloud
Storage.
Public Class Methods
@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
@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
@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
@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
@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
@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
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