Class: ActiveStorage::Service::GCSService
- Inherits:
-
ActiveStorage::Service
- Object
- ActiveStorage::Service
- ActiveStorage::Service::GCSService
- Defined in:
- lib/active_storage/service/gcs_service.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #delete(key) ⇒ Object
-
#download(key) ⇒ Object
FIXME: Add streaming when given a block.
- #exist?(key) ⇒ Boolean
-
#initialize(project:, keyfile:, bucket:) ⇒ GCSService
constructor
A new instance of GCSService.
- #upload(key, io, checksum: nil) ⇒ Object
- #url(key, expires_in:, disposition:, filename:) ⇒ Object
Methods inherited from ActiveStorage::Service
Constructor Details
#initialize(project:, keyfile:, bucket:) ⇒ GCSService
Returns a new instance of GCSService.
7 8 9 10 |
# File 'lib/active_storage/service/gcs_service.rb', line 7 def initialize(project:, keyfile:, bucket:) @client = Google::Cloud::Storage.new(project: project, keyfile: keyfile) @bucket = @client.bucket(bucket) end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
5 6 7 |
# File 'lib/active_storage/service/gcs_service.rb', line 5 def bucket @bucket end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
5 6 7 |
# File 'lib/active_storage/service/gcs_service.rb', line 5 def client @client end |
Instance Method Details
#delete(key) ⇒ Object
24 25 26 |
# File 'lib/active_storage/service/gcs_service.rb', line 24 def delete(key) file_for(key).try(:delete) end |
#download(key) ⇒ Object
FIXME: Add streaming when given a block
18 19 20 21 22 |
# File 'lib/active_storage/service/gcs_service.rb', line 18 def download(key) io = file_for(key).download io.rewind io.read end |
#exist?(key) ⇒ Boolean
28 29 30 |
# File 'lib/active_storage/service/gcs_service.rb', line 28 def exist?(key) file_for(key).present? end |
#upload(key, io, checksum: nil) ⇒ Object
12 13 14 15 |
# File 'lib/active_storage/service/gcs_service.rb', line 12 def upload(key, io, checksum: nil) # FIXME: Ensure integrity by sending the checksum for service side verification bucket.create_file(io, key) end |
#url(key, expires_in:, disposition:, filename:) ⇒ Object
32 33 34 35 |
# File 'lib/active_storage/service/gcs_service.rb', line 32 def url(key, expires_in:, disposition:, filename:) file_for(key).signed_url(expires: expires_in) + "&" + { "response-content-disposition" => "#{disposition}; filename=\"#{filename}\"" }.to_query end |