Search code examples
ruby-on-railsamazon-web-servicesamazon-s3uploadsidekiq

Rails 4 Sidekiq image upload to Amazon S3


Hello everyone,

I'm using S3 gem for image upload to my Amazon S3 bucket. I don't want to use paperclip or carrierwave. So i have an initializer on my rails 4 app:

AWS_S3 ||= S3::Service.new(:access_key_id => AWS_ACCESSKEY_Id, :secret_access_key => AWS_SECRET_KEY)
S3 ||= AWS_S3.buckets.find("my_bucket_name")

Then i have an Amazon worker to perform the job asynchronously:

class AmazonWorker 
  include Sidekiq::Worker  

  def perform(current_user)
    User.find(current_user).products.each do |i|
    photo = S3.objects.build("i.photo_name")
    photo.content = open("http://www.myphoto.com/photo.jpg")
    photo.save
    end
  end
end

I perform the job by calling it with this method:

AmazonWorker.perform_async(@current_user.id)

When i test it locally, it's working. But on my heroku production server i get this error:

2013-12-10T13:37:56Z 2 TID-or2zt0omw AmazonWorker JID-xx155c13f6d0a3c29012b789 INFO: start
2013-12-10T13:37:56Z 2 TID-or2zt0omw AmazonWorker JID-xx155c13f6d0a3c29012b789 INFO: fail: 0.01 sec
2013-12-10T13:37:56Z 2 TID-or2zt0omw WARN: {"retry"=>true, "queue"=>"default", "class"=>"AmazonWorker", "args"=>[3], "jid"=>"xx155c13f6d0a3c29012b789", "enqueued_at"=>1386682676.8495147, "error_message"=>"Invalid key name: ", "error_class"=>"ArgumentError", "failed_at"=>2013-12-10 13:37:56 UTC, "retry_count"=>0}
2013-12-10T13:37:56Z 2 TID-or2zt0omw WARN: Invalid key name:
2013-12-10T13:37:56Z 2 TID-or2zt0omw WARN: /app/vendor/bundle/ruby/2.0.0/gems/s3-0.3.17/lib/s3/object.rb:31:in `key='
/app/vendor/bundle/ruby/2.0.0/gems/s3-0.3.17/lib/s3/object.rb:210:in `initialize'
/app/vendor/bundle/ruby/2.0.0/gems/s3-0.3.17/lib/s3/objects_extension.rb:5:in `new'
/app/vendor/bundle/ruby/2.0.0/gems/s3-0.3.17/lib/s3/objects_extension.rb:5:in `build'
/app/app/workers/amazon_worker.rb:6:in `block in perform'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/relation/delegation.rb:13:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/relation/delegation.rb:13:in `each'
/app/app/workers/amazon_worker.rb:5:in `perform'
/app/vendor/bundle/ruby/2.0.0/gems/sidekiq-2.17.0/lib/sidekiq/processor.rb:49:in `block (3 levels) in process'
/app/vendor/bundle/ruby/2.0.0/gems/sidekiq-2.17.0/lib/sidekiq/middleware/chain.rb:122:in `call'
/app/vendor/bundle/ruby/2.0.0/gems/sidekiq-2.17.0/lib/sidekiq/middleware/chain.rb:122:in `block in invoke'
/app/vendor/bundle/ruby/2.0.0/gems/newrelic_rpm-3.7.0.177/lib/new_relic/agent/instrumentation/sidekiq.rb:30:in `block in call'

What did i do wrong?

Many thanks JD.


Solution

  • Looking at the error message, especially "error_message"=>"Invalid key name: ", I don't think your problem is with sidekiq but with the aws-s3 gem - which I'm guessing you're using based on your question, this SO post seems to reference a similar error with the gem.

    I would consider using aws-sdk gem instead, I know it's a lot of stuff they give you in the gem when you just need S3 but I switched a while back and it works, it's maintained by Amazon and the documentation to upload to S3 directly is straightforward.

    Hope this helps