Hot questions for Using Spree in amazon s3

Question:

I am using Spree 3.1. To use Heroku, I need AWS S3 tp store pictures. Spent so much time with KeyError both local and Heroku.


Answer:

And like to share here:

  1. In AWS S3 document to create IAM USER for ID and access_key_id and secret_access_key. To make it work, I need to create profile and set admin permission.

  2. Only paste this in needs environment eg: config/environments/production.rb and development.rb

     config.paperclip_defaults = {
       storage: :s3,
       s3_credentials: {
       bucket: ENV.fetch('S3_BUCKET_NAME'),
       access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
       secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
       s3_region: ENV.fetch('AWS_REGION'),
       }
      }
    

    Do not replace the value. Instead For Heroku:

    heroku config:set S3_BUCKET_NAME=your-bucket-name AWS_ACCESS_KEY_ID=your-access-key-id AWS_SECRET_ACCESS_KEY=your-secret-access-key AWS_REGION=your-region
    

And for local: We add values in .env

    $ cat .env
     S3_BUCKET_NAME=your-bucket-name-development
     AWS_ACCESS_KEY_ID=your-access-key-id
     AWS_SECRET_ACCESS_KEY=your-secret-access-key

Don't "quote" those values

Question:

I'm having a hard time getting Amazon s3 and Heroku with Spree to work nicely together. I'm getting this error anytime I try to upload an image: ArgumentError (missing required :bucket option).

I'm not sure what I may be doing wrong, any help would be appreciated, thank you.

Spree.rb

Spree.config do |config|
    attachment_config = {

      s3_credentials: {
        access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
        secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
        bucket:            ENV['S3_BUCKET_NAME']
      },

      storage:        :s3,
      s3_headers:     { "Cache-Control" => "max-age=31557600" },
      s3_protocol:    "https",
      bucket:         ENV['S3_BUCKET_NAME'],
      url:            ":s3_domain_url",

      styles: {
          mini:     "48x48>",
          small:    "100x100>",
          product:  "240x240>",
          large:    "600x600>"
      },

      path:           "/:class/:id/:style/:basename.:extension",
      default_url:    "/:class/:id/:style/:basename.:extension",
      default_style:  "product"
    }

    attachment_config.each do |key, value|
      Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
    end
end

Spree.user_class = "Spree::User"

heroku log

2015-09-29T03:52:43.095532+00:00 app[web.1]: Command :: file -b --mime '/tmp/74f262efbb371339275f4ac4988d008220150929-3-b06f2w20150929-3-s4rw17'
2015-09-29T03:52:43.124567+00:00 app[web.1]:    (1.1ms)  BEGIN
2015-09-29T03:52:43.139844+00:00 app[web.1]:   Spree::Asset Load (1.1ms)  SELECT  "spree_assets".* FROM "spree_assets" WHERE (spree_assets.position IS NOT NULL) AND "spree_assets"."viewable_id" = $1 AND "spree_assets"."viewable_type" = $2  ORDER BY spree_assets.position DESC LIMIT 1  [["viewable_id", 1], ["viewable_type", "Spree::Variant"]]
2015-09-29T03:52:43.152510+00:00 app[web.1]:   Spree::Variant Load (1.3ms)  SELECT  "spree_variants".* FROM "spree_variants" WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."id" = $1 LIMIT 1  [["id", 1]]
2015-09-29T03:52:43.165804+00:00 app[web.1]:   Spree::Product Load (2.0ms)  SELECT  "spree_products".* FROM "spree_products" WHERE "spree_products"."id" = $1 LIMIT 1  [["id", 1]]
2015-09-29T03:52:43.176737+00:00 app[web.1]:   Spree::Taxon Load (1.9ms)  SELECT "spree_taxons".* FROM "spree_taxons" INNER JOIN "spree_products_taxons" ON "spree_taxons"."id" = "spree_products_taxons"."taxon_id" WHERE "spree_products_taxons"."product_id" = $1  [["product_id", 1]]
2015-09-29T03:52:43.184663+00:00 app[web.1]:   SQL (1.4ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2015-09-29 03:52:43.179125' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies" WHERE 1=0  ORDER BY spree_taxonomies.position, spree_taxonomies.created_at)
2015-09-29T03:52:42.960450+00:00 app[web.1]: Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/74f262efbb371339275f4ac4988d008220150929-3-b06f2w.png[0]' 2>/dev/null
2015-09-29T03:52:42.995241+00:00 app[web.1]: Command :: convert '/tmp/74f262efbb371339275f4ac4988d008220150929-3-b06f2w.png[0]' -auto-orient -resize "600x600>" -strip -auto-orient -colorspace sRGB '/tmp/74f262efbb371339275f4ac4988d008220150929-3-b06f2w20150929-3-s4rw17'
2015-09-29T03:52:43.100987+00:00 app[web.1]: Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/74f262efbb371339275f4ac4988d008220150929-3-b06f2w.png[0]' 2>/dev/null
2015-09-29T03:52:43.125718+00:00 app[web.1]: Command :: file -b --mime '/tmp/74f262efbb371339275f4ac4988d008220150929-3-1t46ljy.png'
2015-09-29T03:52:43.143777+00:00 app[web.1]:   SQL (1.2ms)  INSERT INTO "spree_assets" ("type", "viewable_type", "viewable_id", "attachment_file_name", "attachment_content_type", "attachment_file_size", "attachment_updated_at", "attachment_width", "attachment_height", "alt", "created_at", "updated_at", "position") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING "id"  [["type", "Spree::Image"], ["viewable_type", "Spree::Variant"], ["viewable_id", 1], ["attachment_file_name", "default-profile-pic.png"], ["attachment_content_type", "image/png"], ["attachment_file_size", 13648], ["attachment_updated_at", "2015-09-29 03:52:42.422226"], ["attachment_width", 400], ["attachment_height", 400], ["alt", ""], ["created_at", "2015-09-29 03:52:43.136153"], ["updated_at", "2015-09-29 03:52:43.136153"], ["position", 1]]
2015-09-29T03:52:43.161572+00:00 app[web.1]:   SQL (1.4ms)  UPDATE "spree_variants" SET "updated_at" = '2015-09-29 03:52:43.153272' WHERE "spree_variants"."id" = $1  [["id", 1]]
2015-09-29T03:52:43.169740+00:00 app[web.1]:   SQL (1.1ms)  UPDATE "spree_products" SET "updated_at" = '2015-09-29 03:52:43.166331' WHERE "spree_products"."id" = $1  [["id", 1]]
2015-09-29T03:52:43.178740+00:00 app[web.1]:   SQL (1.0ms)  UPDATE "spree_taxons" SET "updated_at" = '2015-09-29 03:52:43.177236' WHERE 1=0
2015-09-29T03:52:43.185643+00:00 app[web.1]: [paperclip] saving /spree/images/15/original/default-profile-pic.png
2015-09-29T03:52:43.249013+00:00 heroku[router]: at=info method=POST path="/admin/products/nintendo/images" host=shuabe-store.herokuapp.com request_id=d5fea436-8149-4cc6-a4a3-c61552f3bd4b fwd="104.230.226.189" dyno=web.1 connect=0ms service=931ms status=500 bytes=1669
2015-09-29T03:52:43.244220+00:00 app[web.1]: Completed 500 Internal Server Error in 871ms (ActiveRecord: 36.4ms)
2015-09-29T03:52:43.242853+00:00 app[web.1]:    (1.1ms)  ROLLBACK
2015-09-29T03:52:43.246449+00:00 app[web.1]: 
2015-09-29T03:52:43.246452+00:00 app[web.1]: ArgumentError (missing required :bucket option):
2015-09-29T03:52:43.246454+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/paperclip-4.2.4/lib/paperclip/storage/s3.rb:218:in `bucket_name'
2015-09-29T03:52:43.246456+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/paperclip-4.2.4/lib/paperclip/storage/s3.rb:251:in `s3_bucket'
2015-09-29T03:52:43.246458+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/paperclip-4.2.4/lib/paperclip/storage/s3.rb:255:in `s3_object'
2015-09-29T03:52:43.246459+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/paperclip-4.2.4/lib/paperclip/storage/s3.rb:360:in `block in flush_writes'
2015-09-29T03:52:43.246460+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/paperclip-4.2.4/lib/paperclip/storage/s3.rb:331:in `each'
2015-09-29T03:52:43.246462+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/paperclip-4.2.4/lib/paperclip/storage/s3.rb:331:in `flush_writes'
2015-09-29T03:52:43.246463+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/paperclip-4.2.4/lib/paperclip/attachment.rb:238:in `save'
2015-09-29T03:52:43.246465+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/paperclip-4.2.4/lib/paperclip/has_attached_file.rb:91:in `block in add_active_record_callbacks'
2015-09-29T03:52:43.246467+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:444:in `instance_exec'
2015-09-29T03:52:43.246468+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:444:in `block in make_lambda'
2015-09-29T03:52:43.246470+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:226:in `call'
2015-09-29T03:52:43.246471+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:226:in `block in halting_and_conditional'
2015-09-29T03:52:43.246472+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `call'
2015-09-29T03:52:43.246474+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `block in call'
2015-09-29T03:52:43.246475+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `each'
2015-09-29T03:52:43.246478+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `call'
2015-09-29T03:52:43.246479+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:88:in `run_callbacks'
2015-09-29T03:52:43.246481+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/callbacks.rb:302:in `create_or_update'
2015-09-29T03:52:43.246482+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/persistence.rb:120:in `save'
2015-09-29T03:52:43.246483+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/validations.rb:37:in `save'
2015-09-29T03:52:43.246485+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/attribute_methods/dirty.rb:21:in `save'
2015-09-29T03:52:43.246486+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/transactions.rb:286:in `block (2 levels) in save'
2015-09-29T03:52:43.246488+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
2015-09-29T03:52:43.246489+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
2015-09-29T03:52:43.246491+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
2015-09-29T03:52:43.246493+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
2015-09-29T03:52:43.246494+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
2015-09-29T03:52:43.246495+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
2015-09-29T03:52:43.246514+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/transactions.rb:286:in `block in save'
2015-09-29T03:52:43.246516+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
2015-09-29T03:52:43.246517+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/transactions.rb:285:in `save'
2015-09-29T03:52:43.246518+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/bundler/gems/spree-c4f3907dd279/backend/app/controllers/spree/admin/resource_controller.rb:53:in `create'
2015-09-29T03:52:43.246520+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
2015-09-29T03:52:43.246521+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/abstract_controller/base.rb:198:in `process_action'
2015-09-29T03:52:43.246522+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal/rendering.rb:10:in `process_action'
2015-09-29T03:52:43.246524+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
2015-09-29T03:52:43.246529+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:115:in `call'
2015-09-29T03:52:43.246531+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:115:in `call'
2015-09-29T03:52:43.246532+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:553:in `block (2 levels) in compile'
2015-09-29T03:52:43.246534+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:503:in `call'
2015-09-29T03:52:43.246535+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:503:in `call'
2015-09-29T03:52:43.246536+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:88:in `run_callbacks'
2015-09-29T03:52:43.246538+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/abstract_controller/callbacks.rb:19:in `process_action'
2015-09-29T03:52:43.246539+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal/rescue.rb:29:in `process_action'
2015-09-29T03:52:43.246540+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
2015-09-29T03:52:43.246542+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/notifications.rb:164:in `block in instrument'
2015-09-29T03:52:43.246543+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
2015-09-29T03:52:43.246544+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/notifications.rb:164:in `instrument'
2015-09-29T03:52:43.246546+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
2015-09-29T03:52:43.246547+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
2015-09-29T03:52:43.246548+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
2015-09-29T03:52:43.246549+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/abstract_controller/base.rb:137:in `process'
2015-09-29T03:52:43.246551+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionview-4.2.3/lib/action_view/rendering.rb:30:in `process'
2015-09-29T03:52:43.246552+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal.rb:196:in `dispatch'
2015-09-29T03:52:43.246553+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
2015-09-29T03:52:43.246554+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_controller/metal.rb:237:in `block in action'
2015-09-29T03:52:43.246555+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:76:in `call'
2015-09-29T03:52:43.246557+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
2015-09-29T03:52:43.246558+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:45:in `serve'
2015-09-29T03:52:43.246559+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/journey/router.rb:43:in `block in serve'
2015-09-29T03:52:43.246560+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/journey/router.rb:30:in `each'
2015-09-29T03:52:43.246561+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/journey/router.rb:30:in `serve'
2015-09-29T03:52:43.246563+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:821:in `call'
2015-09-29T03:52:43.246564+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/engine.rb:518:in `call'
2015-09-29T03:52:43.246565+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/railtie.rb:194:in `public_send'
2015-09-29T03:52:43.246567+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/railtie.rb:194:in `method_missing'
2015-09-29T03:52:43.246568+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/mapper.rb:51:in `serve'
2015-09-29T03:52:43.246569+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/journey/router.rb:43:in `block in serve'
2015-09-29T03:52:43.246570+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/journey/router.rb:30:in `each'
2015-09-29T03:52:43.246571+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/journey/router.rb:30:in `serve'
2015-09-29T03:52:43.246572+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/routing/route_set.rb:821:in `call'
2015-09-29T03:52:43.246574+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/warden-1.2.3/lib/warden/manager.rb:35:in `block in call'
2015-09-29T03:52:43.246575+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in `catch'
2015-09-29T03:52:43.246577+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in `call'
2015-09-29T03:52:43.246578+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/etag.rb:24:in `call'
2015-09-29T03:52:43.246579+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/conditionalget.rb:38:in `call'
2015-09-29T03:52:43.246584+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
2015-09-29T03:52:43.246586+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
2015-09-29T03:52:43.246587+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/flash.rb:260:in `call'
2015-09-29T03:52:43.246588+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:225:in `context'
2015-09-29T03:52:43.246589+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220:in `call'
2015-09-29T03:52:43.246590+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/cookies.rb:560:in `call'
2015-09-29T03:52:43.246592+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/query_cache.rb:36:in `call'
2015-09-29T03:52:43.246593+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
2015-09-29T03:52:43.246594+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
2015-09-29T03:52:43.246595+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:84:in `run_callbacks'
2015-09-29T03:52:43.246597+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
2015-09-29T03:52:43.246598+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
2015-09-29T03:52:43.246599+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
2015-09-29T03:52:43.246600+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-09-29T03:52:43.246602+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:38:in `call_app'
2015-09-29T03:52:43.246603+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:20:in `block in call'
2015-09-29T03:52:43.246604+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-09-29T03:52:43.246605+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-09-29T03:52:43.246606+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-09-29T03:52:43.246608+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/rack/logger.rb:20:in `call'
2015-09-29T03:52:43.246609+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-09-29T03:52:43.246610+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2015-09-29T03:52:43.246611+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-09-29T03:52:43.246612+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.3/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-09-29T03:52:43.246613+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/static.rb:116:in `call'
2015-09-29T03:52:43.246615+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2015-09-29T03:52:43.246616+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/engine.rb:518:in `call'
2015-09-29T03:52:43.246617+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/application.rb:165:in `call'
2015-09-29T03:52:43.246618+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/puma-2.13.4/lib/puma/configuration.rb:78:in `call'
2015-09-29T03:52:43.246620+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/puma-2.13.4/lib/puma/server.rb:541:in `handle_request'
2015-09-29T03:52:43.246621+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/puma-2.13.4/lib/puma/server.rb:388:in `process_client'
2015-09-29T03:52:43.246622+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/puma-2.13.4/lib/puma/server.rb:270:in `block in run'
2015-09-29T03:52:43.246623+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/puma-2.13.4/lib/puma/thread_pool.rb:106:in `call'
2015-09-29T03:52:43.246624+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/puma-2.13.4/lib/puma/thread_pool.rb:106:in `block in spawn_thread'
2015-09-29T03:52:43.246626+00:00 app[web.1]:

development.rb

Rails.application.configure do

  config.cache_classes = false 
  config.eager_load = false

  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  config.action_mailer.raise_delivery_errors = false
  config.active_support.deprecation = :log 
  config.active_record.migration_error = :page_load
  config.assets.debug = true

  config.assets.digest = true
  config.assets.raise_runtime_errors = true



config.paperclip_defaults = {
   :storage => :s3,
   :s3_credentials => {
     :bucket => ENV['S3_BUCKET_NAME'],
     :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
     :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
   }
 }

end

production.rb

Rails.application.configure do
  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

  config.assets.js_compressor = :uglifier
  config.assets.compile = false 
  config.assets.digest = true
  config.log_level = :debug

  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_formatter = ::Logger::Formatter.new
  config.active_record.dump_schema_after_migration = false



    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'gmail.com', 
    user_name:            ENV['EMAIL_ID'],
    password:             ENV['EMAIL_PASSWORD'],
    authentication:       'plain',
    enable_starttls_auto: true
    }

config.paperclip_defaults = {
   :storage => :s3,
   :s3_credentials => {
     :bucket => ENV['S3_BUCKET_NAME'],
     :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
     :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
   }
 }    

end

Gemfile

source 'https://rubygems.org'

ruby "2.2.1"

gem 'aws-sdk', '< 2.0'
gem 'puma', '~> 2.13.4'
gem 'braintree', '~> 2.48.1'
gem 'rails', '4.2.3'
gem 'pg'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

gem 'rails_12factor', group: :production


gem 'spree', github: 'spree/spree', branch: '3-0-stable'
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '3-0-stable'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'

Answer:

Do:

 heroku config 

to check your heroku environment variables and make sure you have set S3_BUCKET_NAME.

If it is not set, you should set the environment variable using this command:

heroku config:add S3_BUCKET_NAME=your_bucket_name

See heroku reference for Configuration and Config Vars

Question:

I have tried reading a lot of things online, none of them helped. So need a concrete answer. How do I configure S3 to work with Spree? I am using Spree 3.0.1 with Rails 4.2.1.

I have spree.rb as:

Spree.config do |config|
   # Example:
   # Uncomment to override the default site name.
  #  config.site_name = "Gazella Running Costumes"
  #  config.logo = "store/rungazella.png"

  #S3 configuration
  if Rails.env.production? then
       #production. Store images on S3.
       # development will default to local storage
      attachment_config = {
      s3_credentials: {
        access_key_id: ENV["S3_KEY"],
        secret_access_key: ENV["S3_SECRET"],
        bucket: ENV["S3_BUCKET"],
      },


      storage:        :s3,
      s3_headers:     { "Cache-Control" => "max-age=31557600" },
      s3_protocol:    "https",
      bucket:         ENV["S3_BUCKET"],

      path:          ":rails_root/public/:class/:attachment/:id/:style/:basename.:extension",
      default_url:   "/:class/:attachment/:id/:style/:basename.:extension",
      default_style: "product",
      }

      attachment_config.each do |key, value|
           Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
      end
  end
Spree.user_class = "Spree::User"
end

Also my Gemfile is

source 'https://rubygems.org'
gem 'rails', '4.2.1'
ruby '2.2.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
    gem 'byebug'
    gem 'web-console', '~> 2.0'
    gem 'spring'
end
gem 'spree', '3.0.1'
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '3-0-stable'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'
gem 'rails_12factor', group: :production
gem 'pg'
gem 'aws-sdk'

Answer:

First you need to add the gem 'aws-sdk' with version less than 2.0:

gem 'aws-sdk', '< 2.0'

then, you need to add this code in the spree initializer:

Rails.env.production?
  attachment_config = {
    s3_credentials: {
      access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
      secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      bucket:            ENV['S3_BUCKET_NAME']
    },

    storage:        :s3,
    s3_headers:     { "Cache-Control" => "max-age=31557600" },
    s3_protocol:    "https",
    bucket:         ENV['S3_BUCKET_NAME'],
    url:            ":s3_domain_url",

    path:           "/:class/:id/:style/:basename.:extension",
    default_url:    "/:class/:id/:style/:basename.:extension",
    default_style:  "product"
  }

  attachment_config.each do |key, value|
    Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
  end
end

Question:

I have gone through many documentations but still lying on the same issue, I am trying to upload my product images on s3 locally and on heroku as well, but not able to do so. On localhost image is getting uploaded but not reflecting on s3 bucket console and on heroku Iam encountering a error page Please Help

config/initializers/spree.rb

Spree.config do |config|
   # Example:
   # Uncomment to override the default site name.
  #  config.site_name = "Gazella Running Costumes"
  #  config.logo = "store/rungazella.png"

  #S3 configuration
  if Rails.env.production? then
       #production. Store images on S3.
       # development will default to local storage
      attachment_config = {
      s3_credentials: {
        access_key_id: "xxx",
        secret_access_key: "xxxxx",
        bucket: "xx",
      },


      storage:        :s3,
      s3_headers:     { "Cache-Control" => "max-age=31557600" },
      s3_protocol:    "https",
      bucket:         "xx",

      path:          ":rails_root/public/:class/:attachment/:id/:style/:basename.:extension",
      default_url:   "/:class/:attachment/:id/:style/:basename.:extension",
      default_style: "product",
      }

      attachment_config.each do |key, value|
           Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
      end
  end
Spree.user_class = "Spree::User"
end  

Gemfile

source 'https://rubygems.org'

ruby '2.2.2'
gem 'rails', '4.2.4'
group :development do
    gem 'sqlite3'
end 
group :production do
    gem 'pg'
    gem 'rails_12factor'
end
gem 'aws-sdk', '< 2.0'     
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

group :development, :test do
  gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
end
gem 'spree', '3.0.4'
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '3-0-stable'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'

I have also tried commenting out

path:          ":rails_root/public/:class/:attachment/:id/:style/:basename.:extension"

in spree.rb file as per a previous post on stackoverflow but this even did not helped


Answer:

Create a config/s3.yml file and add s3 config there.

development:
    AWS_ACCESS_KEY: xxx
    AWS_SECRET_TOKEN: xxx
    AWS_BUCKET: xx
production:
    AWS_ACCESS_KEY: ENV['AWS_ACCESS_KEY']
    AWS_SECRET_TOKEN: ENV['AWS_SECRET_TOKEN']
    AWS_BUCKET: ENV['AWS_BUCKET']
test:
    AWS_ACCESS_KEY: xxx
    AWS_SECRET_TOKEN: xxx
    AWS_BUCKET: ''

In config/application.rb, add at end of file.

\# This constant need to be loaded before it being used in config/initializers/paperclip.rb
S3_CREDENTIALS = YAML.load_file(File.join(Rails.root, 'config', 's3.yml'))[Rails.env]

Then your config/initializers/spree.rb will look something like,

Spree.config do |config|
end
Spree.user_class = "Spree::User"
attachment_config = {
    s3_credentials: {
        access_key_id: S3_CREDENTIALS['AWS_ACCESS_KEY'],
        secret_access_key: S3_CREDENTIALS['AWS_SECRET_TOKEN'],
        bucket: S3_CREDENTIALS['AWS_BUCKET']
    },
    storage:        :s3,
    s3_protocol:    "http"
}
attachment_config.each do |key, value|
    Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
end