Class: GcloudStorage::Base
- Inherits:
-
Object
- Object
- GcloudStorage::Base
- Defined in:
- lib/gcloud_storage/base.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #bucket ⇒ Object
- #delete_file(file_path) ⇒ Object
- #expirable_url(file_path, num_secs = 300) ⇒ Object
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
- #service ⇒ Object
- #storage ⇒ Object
- #upload_file(file, dest_file_path) ⇒ Object
Constructor Details
#initialize(options) ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gcloud_storage/base.rb', line 10 def initialize() @connection = { credentials: } Error.missing_credentials unless .is_a?(Hash) unless [:storage] == :local_store = [] [:bucket_name, :project_id, :key_file].each do |option| option_value = [option] unless [:compute_instance] && option == :key_file << option if (option_value.nil? || !(option_value)) end end unless .empty? raise Error::Argument.new(":#{missing_options.join(', ')} are missing") end end # Cache service, storage and bucket to @connection self.bucket end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/gcloud_storage/base.rb', line 8 def connection @connection end |
Instance Method Details
#bucket ⇒ Object
66 67 68 69 70 |
# File 'lib/gcloud_storage/base.rb', line 66 def bucket @connection[:bucket] ||= self.storage.bucket( @connection[:credentials][:bucket_name] ) end |
#delete_file(file_path) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gcloud_storage/base.rb', line 99 def delete_file(file_path) begin file = self.bucket.file(file_path) file.delete rescue => e if e.class == NoMethodError raise Google::Cloud::Error.new( "Not Found: The object you specified doesn't exist!" ) else raise e end end end |
#expirable_url(file_path, num_secs = 300) ⇒ Object
72 73 74 75 |
# File 'lib/gcloud_storage/base.rb', line 72 def expirable_url(file_path, num_secs=300) file = self.bucket.file(file_path) file.signed_url(method: 'GET', expires: num_secs) end |
#service ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gcloud_storage/base.rb', line 35 def service storage_type = @connection[:credentials][:storage].present? ? @connection[:credentials][:storage] : (:gcloud) begin @connection[:service] ||= if storage_type == :local_store LocalStore.new else Google::Cloud::Storage.new( project_id: @connection[:credentials][:project_id], credentials: @connection[:credentials][:key_file] ) end rescue => e raise e end end |
#storage ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gcloud_storage/base.rb', line 54 def storage @connection[:storage] ||= ( begin tries ||= 3 self.service rescue => e retry unless (tries -= 1).zero? raise e end ) end |
#upload_file(file, dest_file_path) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/gcloud_storage/base.rb', line 77 def upload_file(file, dest_file_path) remote_file = nil file_path = (file.respond_to?(:tempfile))? file.tempfile : file begin retries ||= 2 remote_file = self.bucket.upload_file file_path, dest_file_path rescue => e unless (retries -= 1).zero? retry else raise e end else unless remote_file.md5 == Digest::MD5.base64digest(File.read(file_path)) raise Exception.new('Uploaded file is corrupted.') end remote_file end end |