Module: FlatironS3Uploader

Defined in:
lib/flatiron_s3_uploader.rb,
lib/flatiron_s3_uploader/cli.rb,
lib/flatiron_s3_uploader/version.rb,
lib/flatiron_s3_uploader/exceptions.rb,
lib/flatiron_s3_uploader/file_uploader.rb,
lib/flatiron_s3_uploader/image_resizer.rb

Defined Under Namespace

Classes: CLI, FileNotFoundError, FileUploader, ImageResizer

Constant Summary collapse

VERSION =
'0.1.2'

Class Method Summary collapse

Class Method Details

.upload(filepath, bucket, options = {}) ⇒ Object

Uploads an image file from the specified filepath to the specified S3 bucket. Returns a URL for the uploaded image.

Options are:

:width

The width of the resized image. If a height is not specified, the height will scale based on the specified width.

:height

The height of the resized image. If a width is not specified, the width will scale based on the specified height.

:directory

The directory of the S3 bucket to upload to (i.e. ‘phase-3/lab-name’)

Examples:

FlatironS3Uploader.upload('path/to/image.png', 'bucket-name', { width: 400 })
# => "https://bucket-name.s3.amazonaws.com/image.png"

FlatironS3Uploader.upload('path/to/image.png', 'bucket-name', { width: 400, directory: 'folder/subfolder' })
# => "https://bucket-name.s3.amazonaws.com/folder/subfolder/image.png"


34
35
36
37
38
39
40
41
42
43
# File 'lib/flatiron_s3_uploader.rb', line 34

def self.upload(filepath, bucket, options = {})
  resizer = ImageResizer.new(filepath)
  filepath = resizer.resize(width: options[:width], height: options[:height]) if options[:width] || options[:height]
  mime_type = resizer.mime_type

  image = File.open(filepath)

  client = Aws::S3::Client.new
  FileUploader.new(client).upload(bucket, image, mime_type, options[:directory])
end