No commit activity in last 3 years
No release in over 3 years
Gently wraps you s3 resource urls into ActiveModel::Record
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.9
~> 10.0
~> 3.2.0
~> 1.21.0

Runtime

 Project Readme

HasS3Attachment

A simple gem, which manages Rails model file attachments hosted on aws s3.

Usage

Typical use case is when you have single paged web app backed by Rails app, and you upload your attachments right from the browser to s3 via CORS - as a result you have only location of your file on s3. Then you may wanna link this attachment to some model in your app:

class VideoReport < ActiveRecord::Base
  include HasS3Attachment

  has_s3_attachment(
    :video,
      s3_options: {
        region: 'us-west-2',
        key: 'key-xxx',
        secret: 'secret-xxx'
      },
      host_alias: 'cdn-example.com'
  )
end

To get it working you must have s3_bucket_paths attr of type string declared on your model. Set s3 location on an instance of your model as following (to associate your model with attachment):

video_report.video_s3_bucket = 'your_bucket'
video_report.video_s3_path = '/path/to/file.mp4'

Then you'll be able to to call the next methods:

video_report.video_url # returns full attachment url
video_report.delete_video # removes attachment from s3, may be used in conjunction with `before_destroy` ActiveRecord callback
video_report.open_video do |f|
  # f is an input stream of your attachment
end

Multiple attachments, one model

You can add multiple attachments to your model like this:

class User < ActiveRecord::Base
  has_s3_attachment(
    :photo,
    s3_options: {
      region: 'us-west-2',
      key: 'key-xxx',
      secret: 'secret-xxx'
    },
    host_alias: 'cdn-example.com'
  )

  has_s3_attachment(
    :document,
    s3_options: {
      region: 'us-west-1',
      key: 'key-xxx1',
      secret: 'secret-xxx'
    }
  )
end

Installation

Add this line to your application's Gemfile:

gem 'has_s3_attachment'

And then execute:

$ bundle

Or install it yourself as:

$ gem install has_s3_attachment

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/pavloo/has_s3_attachment/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request