开发者

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

开发者 https://www.devze.com 2023-02-16 22:39 出处:网络
Update: I\'ve switched to CarrierWave (finally got it to work), so although I still appreciate answers to this question I won\'t be able to try if they actually work since I\'ve completely removed DM-

Update: I've switched to CarrierWave (finally got it to work), so although I still appreciate answers to this question I won't be able to try if they actually work since I've completely removed DM-Paperclip from my code.


Hi there,

I'm developing a Sinatra-webapp using DataMapper and are now looking to add some upload-functionality with S3 as storage. I've tried CarrierWave, but I couldn't get that to work so now I'm trying 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: #{@ar开发者_如何学Pythonticle.errors.values.join(', ')}"
  rescue StandardError => e
    puts "Got an error trying to save the article #{e.to_s}"
  end

  redirect '/articles'
end

Yet when I create a new article it doesn't save anything to my S3 bucket and I don't get any errors either.

Any ideas what I'm doing wrong?


Hey! Please try my fork: https://github.com/solnic/dm-paperclip it includes many patches which have fixed some issues with S3. Within a month or two I will be releasing it.


Apart from the solutions already posted, I would like to add a recomendation.

In my experience, using DataMapper's raise_on_save_failure feature is not of much help for debugging options. I recommend you disable that feature and use something like the following code:

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

That way, you will get a full explanation of every issue DM encountered when trying to persist your model. I find it very useful not only for debugging but also for showing those messages to the consumers of my application.


Some time ago I did my fork especially for S3 in mind. My fork work with official AWS-SDK, instead of old aws-s3 which is mostly outdated.

If anybody will search for S3 solution for paperclip this is one that work (today)

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

take a look at readme to get how to configure paperclip for S3

0

精彩评论

暂无评论...
验证码 换一张
取 消