Using Delayed Paperclip With S3 Direct Upload

妖精的绣舞 提交于 2020-01-24 11:15:28

问题


I'm using Delayed Paperclip alongside direct uploads to S3. My model is called Photo and its attachment is image.

Images are uploaded to S3 using JavaScript from the Photo form. The file is stored in the location that Paperclip expects the original image to be located, and the file details are saved to hidden fields. When the form is submitted, these attributes are written to the Photo model:

image_file_name image_file_size image_content_type

Because writing these attributes alone doesn't seem to be enough to trigger Delayed Paperclip to process the image, after Photo.save I call Photo.image.reprocess! which does get DelayedPaperclip to create a new Sidekiq job which successfully processes the image.

The problem, is that when I call Photo.save in the PhotosController, the file is copied to a temp directory from S3, then back to S3. This happens outside of the job and is blocking:

[paperclip] copying image_assets/grab-original.tiff to local file /var/folders/bv/x495g9g10m7119680c9ssqmr0000gn/T/94943834d26bcb8b471f4eeb2a7f899d20141125-3895-1oqom7l
[AWS S3 200 2.601589 0 retries] get_object(:bucket_name=>"example-com-development",:key=>"image_assets/grab-original.tiff")

[paperclip] saving image_assets/grab-original.tiff
[AWS S3 200 2.47114 0 retries] put_object(:acl=>:public_read,:bucket_name=>"example-com-development",:cache_control=>"max-age=29030400",:content_length=>534472,:content_type=>"image/tiff",:data=>Paperclip::AttachmentAdapter: grab.tiff,:key=>"image_assets/grab-original.tiff")

Why is Paperclip copying the file down and back again?


回答1:


My approach was wonky.Even if it had worked it wouldn't have added the image_processing attribute to the Photo model.

After digging into the Delayed Paperclip API, the following seems to have done the trick:

Inside PhotosController#create:

# Ensure we are flagged as processing
@media_item.photo.prepare_enqueueing_for(:image)

if @media_item.save
   # Add Job
   @media_item.photo.enqueue_delayed_processing
end

respond_with(:admin, @galleryable, @media_item)

I have requested a better API here: https://github.com/jrgifford/delayed_paperclip/issues/116



来源:https://stackoverflow.com/questions/27127385/using-delayed-paperclip-with-s3-direct-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!