Paperclip AWS::S3::Errors::NoSuchKey error on copy

2019-06-25 21:56发布

问题:

I'm trying to copy avatars from a different model into my User model but am getting an AWS::S3::Errors::NoSuchKey error when I try to do that.

Here's the code that throws the error:

old_avatar = OldAvatar.find(1)
user = User.find(old_avatar.user_id)
user.avatar = old_avatar.avatar
user.save

The user.avatar = old_avatar.avatar line is what throws it.

Here is my OldAvatar model...

has_attached_file :avatar, 
                    :styles => { 
                      :t => '20x20#',
                      :s => '40x40#',
                      :m => '50x50#',
                      :b => '80x80#',
                      :f => '100x100#' 
                    }, 
                    :storage => :s3, 
                    :s3_credentials => { 
                      :access_key_id => APP_CONFIG['s3_access_key_id'], 
                      :secret_access_key => APP_CONFIG['s3_secret_access_key']
                    },
                    :path => ":attachment/:id/:basename:normalized_style.:extension", 
                    :url => "/:attachment/:id/:basename:normalized_style.:extension",
                    :bucket => "old_bucket"

And here is my User model...

has_attached_file :avatar, 
                      :styles => { 
                        :t => '20x20#',
                        :s => '40x40#',
                        :m => '50x50#',
                        :b => '80x80#',
                        :f => '100x100#'
                      },
                      :storage => :s3, 
                      :s3_credentials => { 
                        :access_key_id => APP_CONFIG['s3_access_key_id'], 
                        :secret_access_key => APP_CONFIG['s3_secret_access_key']
                      },
                      :bucket => "new_bucket",
                      :path => ":attachment/:id_partition/:basename_:style.:extension",
                      :url => "/:attachment/:id_partition/:basename_:style.:extension"

Do note, I'm copying between two different buckets (as noted in the different model model code), so maybe that has something to do with it?

回答1:

Turns out I had some inconsistent paths for some early images and so they didn't match the path I set for Paperclip (thus the wrong image URL was generated).

So, problem solved.



回答2:

I had this issue because of a misnamed size identifier using :regular instead of :normal. Since there was no :regular, it gave me this error.