shrinerb/shrine-imgix

Cloning Object with Shrine/Imgix Attachment using .dup fails

mikecx opened this issue · 2 comments

I've got an Object with an attacher using shrine-imgix that uses S3 as the storage backend. When I call .dup on the Object it throws #<NoMethodError: undefined method `>' for nil:NilClass> in the Amazon S3 Library at aws-sdk-s3-1.8.0/lib/aws-sdk-s3/plugins/expect_100_continue.rb.

I get the feeling that shrine-imgix should be defining its own version of the copy method but I can't quite seem to figure out what should be in there. Stack trace is below.

File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-s3-1.5.0/lib/aws-sdk-s3/plugins/expect_100_continue.rb" line 18 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-s3-1.5.0/lib/aws-sdk-s3/plugins/bucket_dns.rb" line 33 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/aws-sdk-core/rest/handler.rb" line 8 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/aws-sdk-core/plugins/user_agent.rb" line 13 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/seahorse/client/plugins/endpoint.rb" line 45 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/aws-sdk-core/plugins/param_validator.rb" line 24 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/seahorse/client/plugins/raise_response_errors.rb" line 14 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-s3-1.5.0/lib/aws-sdk-s3/plugins/sse_cpk.rb" line 22 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-s3-1.5.0/lib/aws-sdk-s3/plugins/dualstack.rb" line 26 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-s3-1.5.0/lib/aws-sdk-s3/plugins/accelerate.rb" line 35 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/aws-sdk-core/plugins/jsonvalue_converter.rb" line 20 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/aws-sdk-core/plugins/idempotency_token.rb" line 17 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/aws-sdk-core/plugins/param_converter.rb" line 24 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/aws-sdk-core/plugins/response_paging.rb" line 10 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/seahorse/client/plugins/response_target.rb" line 23 in call
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-core-3.6.0/lib/seahorse/client/request.rb" line 70 in send_request
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-s3-1.5.0/lib/aws-sdk-s3/client.rb" line 5335 in put_object
File "/app/vendor/bundle/ruby/2.4.0/gems/aws-sdk-s3-1.5.0/lib/aws-sdk-s3/object.rb" line 876 in put
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/storage/s3.rb" line 397 in put
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/storage/s3.rb" line 245 in upload
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-imgix-0.4.0/lib/shrine/storage/imgix.rb" line 52 in upload
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine.rb" line 330 in copy
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine.rb" line 319 in put
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/upload_options.rb" line 32 in put
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine.rb" line 303 in _store
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine.rb" line 233 in store
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/hooks.rb" line 95 in block in store
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/hooks.rb" line 101 in around_store
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/hooks.rb" line 95 in store
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/logging.rb" line 86 in block in store
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/logging.rb" line 101 in block in log
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/logging.rb" line 161 in benchmark
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/logging.rb" line 101 in log
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/logging.rb" line 86 in store
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/rack_file.rb" line 62 in store
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine.rb" line 211 in upload
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/hooks.rb" line 56 in block in upload
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/hooks.rb" line 62 in around_upload
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/hooks.rb" line 56 in upload
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/rack_file.rb" line 56 in upload
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine.rb" line 635 in store!
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/copy.rb" line 40 in copy
File "/app/vendor/bundle/ruby/2.4.0/gems/shrine-2.8.0/lib/shrine/plugins/copy.rb" line 27 in initialize_copy
File "/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/core.rb" line 411 in initialize_dup
File "/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/core.rb" line 411 in initialize_dup
File "/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/inheritance.rb" line 230 in initialize_dup
File "/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.1.4/lib/active_model/validations.rb" line 283 in initialize_dup
File "/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/attribute_methods/dirty.rb" line 58 in initialize_dup
File "/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/timestamp.rb" line 51 in initialize_dup
File "/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/associations.rb" line 278 in initialize_dup
File "/app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.4/lib/active_record/aggregations.rb" line 8 in initialize_dup
File "/app/app/jobs/copy_job.rb", line 23 in dup
janko commented

The error comes from Shrine's copy plugin, which overrides #dup on the model instance (in your case the ActiveRecord::Base model) to create a copy of the original model's attachment and assign the copy to the duplicate.

The problem is that for some reason your attachment (the Shrine::UploadedFile object) is missing the size metadata value, so when the Shrine::UploadedFile object gets passed directly as the :body parameter for upload, aws-sdk-s3 assumes it will have a #size and does a comparison with >, causing the error you're seeing.

Can you check your upload flow and try to figure out where the size metadata is left out? Note that when doing direct uploads to S3 you need to assign it manually on the client side (or alternative use the restore_cached_data plugin).

Aha, thanks for the quick response. There are a few places in the flow where we set this, i'll try and track down which one is causing the issue. Thanks!