Class: ActiveStorage::Service::GCSService

Inherits:
ActiveStorage::Service show all
Defined in:
lib/active_storage/service/gcs_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveStorage::Service

configure

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

#bucketObject (readonly)

Returns the value of attribute bucket.



5
6
7
# File 'lib/active_storage/service/gcs_service.rb', line 5

def bucket
  @bucket
end

#clientObject (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

Returns:

  • (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