I just made a massive cock-up in my rails app. I deleted half the users from the database on my dev site, thinking this would have no effect on the live site db, which seems to have led to the user avatars being removed on the live site!
I don't know much about paperclip or S3 but I'm begging for help if anyone knows if it is possible to get them back?
Does S3 have backups? Have the images really been deleted or just somehow detached from their associated user object?
I'm using paperclip in my user model file like so:
# paperclip avatars on S3
has_attached_file :avatar, {
:styles => { :medium => "200x200", :small => "100x100#", :thumb => "64x64#" },
:default_url => "/assets/profiles/avatar_default_200x200.png",
:path => "/avatars/:style/:id/:filename"
}.merge(PAPERCLIP_STORAGE_OPTIONS)
validates_attachment_size :avatar, :less_than => 2.megabytes,
:unless => Proc.new {|m| m[:image].nil?}
validates_attachment_content_type :avater, :content_type=>['image/jpeg', 'image/png', 'image/gif'],
:unless => Proc.new {|m| m[:image].nil?}
Thanks for any information!
If you haven't enabled versioning in S3, I guess you're out of luck.
In order to keep track of deleted objects from s3 you can add versioning to S3 buckets. This will cause S3 to keep versions of an object even after deletion.
Details can be found here.
Related
I have a set of images on Amazon S3, and I'd like to automatically generate thumbnails for them to serve on my site.
I've considered Cloudinary, but it seems that I'd have to copy over all my images to Cloudinary servers first. I want to keep them on S3.
I've considered Dragonfly, but it seems that Dragonfly only works with files I'd upload after installing Dragonfly. I already have uploaded all my files.
What's a good solution for me? I'm in a Rails environment (rails 3.2).
Thanks!
If it's just a 'set of images' it's not really that well structured. You're better off reorganizing the way you store and manage images.
Try Paperclip.
class ModelName < ActiveRecord::Base
attr_accessible :image #more here
has_attached_file :image, :styles => { :large => "450x450>", :medium => "300x300>", :thumb => "150x150>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "people/:style/:id/:filename",
:s3_protocol => "https"
def original_image_url
image.url
end
def large_image_url
image.url(:large)
end
def medium_image_url
image.url(:medium)
end
def small_image_url
image.url(:thumb)
end
#etc
end
Then simply do this to assign an existing image to an existing instance through the console:
require 'uri'
#instance.image = open(URI::encode(url))
#instance.save
# s3 will now contain the images in the appropriate sizes in the format specified above.
Since the original will also be saved, I'd advise to then delete the 'set of images' on s3 you started with, otherwise you'll be duplicating them.
I was wrong about Dragonfly. You can use Dragonfly on already-uploaded files. I'm using it on my project and it's working out great.
When I create a user model the avatar.png file gets properly uploaded to S3 in the defined path. Problem is that, when I try to "read/download" the user.avatar.url it always gives the default path i.e. default avatar.
My user.rb has this:
attr_accessible :avatar
has_attached_file :avatar,
:storage => :s3,
:bucket => "/avatars",
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
},
:path => "/avatars/:filename",
:default_url => "https://s3.amazonaws.com/avatars/default.png"
In my view I have:
user.avatar.url #<--- Which outputs https://s3.amazonaws.com/avatars/default.png
Any ideas how to get the right url and the right avatar (which does exists in S3)?
Or how to debug the attachment search path (i.e. the path where paperclip searches the file)?
The problem was that attachments file_name attribute didn't get saved and that was due to this line:
attr_accessor :avatar_file_name
Removing that line fixed the problem.
I'm migrating my app from Rails 3.0.9 to 3.2.3 and ruby 1.9.3. I was using paperclip(2.3.11) with aws-s3 gem to store my pictures.
Now I want to use the last version of paperclip(3.0.1) and then I have to use aws-sdk gem.
I've set my aws.yml file as:
development: &development
bucket: bucket_name_for_dev
access_key_id: *****
secret_access_key: *******
staging:
<<: *development
bucket: bucket_name_for_staging
production:
<<: *development
bucket: bucket_name_for_prod
my model contains :
has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "50x50>" }, :default_url => 'picture_missing.png',
:storage => :s3,
:bucket => "bucket_name_for_dev",
:s3_credentials => Rails.root.join("config/aws.yml"),
:path => "/presentation_pictures/:id/:style/:filename",
:url => ":s3_eu_url"
attr_accessible :picture
attr_accessor :picture_url
When trying to upload a file, I get the error:
AWS::S3::Errors::SignatureDoesNotMatch
The request signature we calculated does not match the signature you provided. Check your key and signing method.
I verified that my keys are accessible and set to the right value.
Here is also what I get from console:
[paperclip] Saving attachments.
[paperclip] saving /presentation_pictures/43/original/Image_1.png
[AWS S3 200 0.813272 0 retries] put_object(:acl=>:public_read,:bucket_name=>"*******_dev",:content_type=>"image/png",:data=>#<Paperclip::UploadedFileAdapter:0x2e144b4 #target=#<ActionDispatch::Http::UploadedFile:0x2dbb1fc #original_filename="Image 1.png", #content_type="image/png", #headers="Content-Disposition: form-data; name=\"presentation_picture[picture]\"; filename=\"Image 1.png\"\r\nContent-Type: image/png\r\n", #tempfile=#<File:/var/folders/tb/tbmv4LE9EwGNPr1QqU2S0E+++TI/-Tmp-/RackMultipart20120407-33502-1gajfe9>>, #tempfile=#<File:/var/folders/tb/tbmv4LE9EwGNPr1QqU2S0E+++TI/-Tmp-/Image 1.png20120407-33502-an4dcy>>,:key=>"presentation_pictures/43/original/Image_1.png")
Do you have any idea from where could come this problem?
Thx
I had the same issue. It turned out I was using the correct access key id but my secret key was copied incorrectly.
Double checking my secret key and correcting it fixed it for me.
My mistake was taking the root access key instead of the user access key. AWS changed it recently so you must create an individual AIM user for yourself, and then use that user's access key (not root)
Seems like you cannot access your aws the right way. You have to specify your host name, especially when using buckets in a non US region. Try using
:s3_host_name => 's3-eu-west-1.amazonaws.com'
in your has_attached_file options.
If you've configured your S3 to use https also add
:s3_protocol => 'https'
if not, just ignore it.
And if your bucket is a private one, add
:s3_permissions => :private
You also don't need to explicitly configure your bucket in the options parameter, as you've already declared it in your aws.yml and set it via :s3_credentials => Rails.root.join("config/aws.yml").
Hope this helps.
Hi I just migrated to heroku cedar stack. app is Rails 3, ive been using paperclip on s3 just fine previously. my gemfile has:
gem 'paperclip', '2.3.11'
gem 'aws-s3', '0.6.2'
my model file has:
class UserProfile < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :thumb => "150x200#" },
:default_style => :thumb,
:default_url => "missingAvatar.png",
:storage => :s3,
:s3_credentials => S3_CREDENTIALS
Ive created a new file to store S3_CREDENTIALS,:
# initializers/s3.rb
if Rails.env == "production"
# set credentials from ENV hash
S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "app_content"}
else
# get credentials from YML file
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
end
... with s3.yml containing my keys for local dev, and the keys set in heroku config:
S3_KEY => AK...
S3_SECRET => FFE...
as mentioned, everything works just fine on local. i can even see the existing avatars from before. just, when i try to upload anything new, i get no errors in heroku logs, but the picture never uploads.
ive went thru many stackoverflow issues, but none matching this. can anyone help??
Try adding the following to your model
class UserProfile < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :thumb => "150x200#" },
:default_style => :thumb,
:default_url => "missingAvatar.png",
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url => "/assets/avatar/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/avatar/:id/:style/:basename.:extension"
The missing path / default path might be causing an issue.
turns out i needed to upgrade my paperclip gem to '2.4.5'
i did this in my Gemfile, then bundle update, and it worked!
I'm trying to seed my database with member profiles and also member profile pictures with S3 and paperclip but it doesn't seem to be working.
I can create/edit existing members within the application to add pictures with paperclip + S3 and it works just fine but seeding it doesn't work. I have searched but can't find an answer.
I don't know what is your exact problem but you can try something like this in your seeds.rb file :
u = User.new({:name => 'username', :email => 'user#name.fr'...})
u.avartar = File.open('/Users/myAccount/avatars/user.png')
u.save!
In your User.rb file, you must have parperclip configured to work with amazon s3
has_attached_file :avatar,
:styles => { :large => "177x177>", :thumb => "60x60>" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/avatars/:style/:id/:filename"
You could find on dogan kaya berktas blog post detail about s3.yml