How to use Seed data with Paperclip + S3 - ruby-on-rails-3

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

Related

Paperclip on S3 - retrieve deleted files?

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.

Dynamic generate bucket name for S3

Im trying to create an dynamic bucket name depending on my polymorphic association type.
My first approach was trying something like this:
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image, :styles => { :small => "200x200>", :thumb => "50x50>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => self.imageable_type.to_s
end
If i try to create a new object i got the next error:
NoMethodError: undefined method `imageable_type' for #< Class:0x007fd3fe0b15d8
I find out on the S3 documentation this:
bucket: This is the name of the S3 bucket that will store your files. Remember that the bucket must be unique across all of Amazon S3. If the bucket does not exist Paperclip will attempt to create it. The bucket name will not be interpolated. You can define the bucket as a Proc if you want to determine it's name at runtime. Paperclip will call that Proc with attachment as the only argument.
The problem is that i don't get how i can get this working to set the name of my polymorphic association as the name of the bucket.
Any help will be appreciated.
Hope it help somebody,
The final solution was based on this post: rails paperclip S3 with dynamic bucket name
Read the post for get a better explanation of how use the Proc.
The final code:
class PostImage < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image, :styles => {
:square=> "170x170#",
:rectangle=> "220x170#",
:large => "600x400#" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:bucket => lambda { |attachment| "#{attachment.instance.imageable_type.to_s.downcase}-gallery" }
end

inconsistant photo url using paperclip when query in two apps

I am using paperclip to upload photos in my two rails app which share a single database. Now the problem is, if I upload a photo in app-a, for instance, paperclip gives me a url as:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-a/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Here is what I set up in my item model:
class Item < ActiveRecord::Base
attr_accessible :photo, :photo_url, :poll_id, :brand, :number_of_votes
has_attached_file :photo,
:styles => { :thumbnail => "100x100#",
:small => "150x150>",
:medium => "250x250>",
:large => "400x400>" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url=>"/item_:id/created_at_:created_at/:style.jpg"
Paperclip.interpolates :created_at do |attachment, style|
attachment.instance.created_at
end
end
In the other app app-b, when I query the url with item.photo.url(:large), it gave me:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-b/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Therefore I got a wrong url.
Are there any ways I can do that by configuring paperclip? Thanks!
As described here : https://github.com/thoughtbot/paperclip#defaults , you can change the default config, in your case I guess you want to change the :local_root so it doesn't include the app name.

migrated rails3 app to heroku, now paperclip+s3 not uploading files - nothing in heroku logs

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!

file not being saved to S3 with paperclip amazon configuration

I am developing a Rails 3 app that has user upload for there profile picture. I have followed a number of online configurations and I think I am really close to getting it working but I can't seem to get the file to go to the amazon bucket for some reason.
Here is my setup
user model has this
has_attached_file :photo,
:styles => {:small => "83x83>"},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:style/:filename",
:bucket => 'mybucket'
my yml file looks like so
development:
bucket: "dev"
access_key_id: "kjlkjlkjlkjlkjlkjlkj"
secret_access_key: "kjlkjlkjlkjlkjlkjlkj/kjlkjlkjlkjlkjlkjlkj"
test:
bucket: "test"
access_key_id: "kjlkjlkjlkjlkjlkjlkj"
secret_access_key: "kjlkjlkjlkjlkjlkjlkj/kjlkjlkjlkjlkjlkjlkj"
production:
bucket: "prod"
access_key_id: "kjlkjlkjlkjlkjlkjlkj"
secret_access_key: "kjlkjlkjlkjlkjlkjlkj/kjlkjlkjlkjlkjlkjlkj"
my action in my controller does this
def upload_image
current_user.update_attributes(params[:user])
redirect_to "/profile/#{current_user.id}"
end
I am inspected the user.photo object in the rails console and I don't see anything in the :errors param. I am pretty stumped at this point. I did use my FTP program (transmit) to connect to the S3 account using the same creds and it worked just fine so I am pretty sure my config is correct.
In your User model you call
:bucket => 'mybucket'
But in your yml file, none of the buckets are called mybucket. Try making sure the names match.
Change your configuration to this:
has_attached_file :photo,
:styles => {:small => "83x83>"},
:storage => :s3,
:s3_credentials => YAML.load_file("#{RAILS_ROOT}/config/s3.yml"),
:path => "/:style/:filename",
:bucket => 'mybucket'