Class: Journeybook::Storage::S3Client
- Inherits:
-
Object
- Object
- Journeybook::Storage::S3Client
- Defined in:
- app/services/journeybook/storage/s3_client.rb
Defined Under Namespace
Instance Method Summary collapse
- #delete(storage_key) ⇒ Object
-
#initialize(configuration: Journeybook.configuration, client: nil) ⇒ S3Client
constructor
A new instance of S3Client.
- #public_url(storage_key) ⇒ Object
- #upload(file) ⇒ Object
Constructor Details
#initialize(configuration: Journeybook.configuration, client: nil) ⇒ S3Client
Returns a new instance of S3Client.
12 13 14 15 |
# File 'app/services/journeybook/storage/s3_client.rb', line 12 def initialize(configuration: Journeybook.configuration, client: nil) @configuration = configuration @client = client end |
Instance Method Details
#delete(storage_key) ⇒ Object
42 43 44 45 46 |
# File 'app/services/journeybook/storage/s3_client.rb', line 42 def delete(storage_key) return if storage_key.blank? s3_client.delete_object(bucket: bucket, key: storage_key) end |
#public_url(storage_key) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'app/services/journeybook/storage/s3_client.rb', line 48 def public_url(storage_key) base_url = @configuration.s3_public_base_url.to_s.delete_suffix("/") return "#{base_url}/#{storage_key}" if base_url.present? raise Error, "Journeybook S3 bucket and region are required." if bucket.blank? || region.blank? "https://#{bucket}.s3.#{region}.amazonaws.com/#{storage_key}" end |
#upload(file) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/journeybook/storage/s3_client.rb', line 17 def upload(file) raise Error, "Image file is required." unless file.respond_to?(:read) content = file.read file.rewind if file.respond_to?(:rewind) original_filename = File.basename(file.original_filename.to_s) content_type = file.content_type.to_s storage_key = build_storage_key(original_filename) s3_client.put_object( bucket: bucket, key: storage_key, body: content, content_type: content_type ) Upload.new( storage_key: storage_key, original_filename: original_filename, content_type: content_type, byte_size: content.bytesize, checksum: Digest::SHA256.hexdigest(content) ) end |