How to use Sinatra, Datamapper, DM-Paperclip and S3?

Update: I switched to CarrierWave (finally got it to work), so although I still appreciate the answers to this question, I can’t try if they really work, ve completely removed DM-Paperclip from my code.


Hi,

I am developing Sinatra-webapp using DataMapper and now I want to add some boot functions from S3 as storage. I tried CarrierWave , but I could not get this to work, so now I am trying to execute dm-paperclip. This is what I have right now:

Model:

class Article
  include DataMapper::Resource
  include Paperclip::Resource

  property :id,                       Serial
  property :created_at,               DateTime
  property :updated_at,               DateTime
  property :title,                    String
  property :body,                     Text

  has_attached_file :screenshot,
                    :storage          => :s3,
                    :s3_credentials   => {
                      :access_key_id      => 'my-access-key-id',
                      :secret_access_key  => 'my-secret_access-key',
                      :bucket             => 'my-bucket'
                    },
                    :styles => {
                      :medium => "300x300>",
                      :thumb => "100x100>"
                    }
end

Controller:

post '/articles/create' do
  @article = Article.new
  @article.title        = params[:title]
  @article.body         = params[:body]
  @article.screenshot   = params[:screenshot]

  begin
    @article.save
  rescue DataMapper::SaveFailureError => e
    puts "Error saving article: #{e.to_s} validation: #{@article.errors.values.join(', ')}"
  rescue StandardError => e
    puts "Got an error trying to save the article #{e.to_s}"
  end

  redirect '/articles'
end

But when I create a new article, it does not save anything in my S3 statement, and I also get no errors.

Any ideas what I'm doing wrong?

+3
3
+2

, .

, DataMapper raise_on_save_failure . - :

if model.save then
    return model
  else
    error = String.new
    model.errors.each do |e|
      error << "#{e[0]}\n"
    end
    raise ArgumentError, error
  end

, , . , , .

+1

Some time ago, I specifically designed my plug for the S3. My plug works with the official AWS-SDK, instead of the old aws-s3, which is mostly deprecated.

If anyone is looking for an S3 paperclip solution, this is the one that works (today)

https://github.com/krzak/dm-paperclip-s3

take a look at readme to learn how to set up a clip for S3

0
source

All Articles