file not being saved to S3 with paperclip amazon configuration - ruby-on-rails-3

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'

Related

Paperclip::Errors::NotIdentifiedByImageMagickError + Heroku

I currently have an application using paperclip that allows users to upload their creatives. This has worked flawless thus far when it comes to a user uploading an image file. We have since tested to upload a .mov file and I get this error:
Creative Paperclip::Errors::NotIdentifiedByImageMagickError
The weird thing, this error is only generated on Heroku. I can upload .mov files just fine on my local host.
My Current Gem Setup:
paperclip (3.4.1, 3.4.0)
paperclip-aws (1.6.7, 1.6.6)
paperclip-ffmpeg (0.10.2)
cocaine (0.5.1, 0.4.2)
Event.rb
has_attached_file :creative,
:processors => [:ffmpeg],
:styles => {
:thumb => [:geometry => "250x150", :format => 'png'],
:custcreative => [:geometry => "275x75", :format => 'png'],
:creativepreview => ["275x195",:png]
},
:url => "***",
:path => "***",
:s3_domain_url => "***",
:storage => :s3,
:s3_credentials => Rails.root.join("config/s3.yml"),
:bucket => '***',
:s3_permissions => :public_read,
:s3_protocol => "http",
:convert_options => { :all => "-auto-orient" },
:encode => 'utf8'
Spending hours trying to figure out why this works locally but throwing error on Heroku.
I even tried removing the :style setting, but still did not work.
TIA
EDIT
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/MidPen20130413-2-1mzetus.mov[0]'
Well, here is the answer in case any other newbies like us come across the same problem. The issue is with the geometry method that is being used for image cropping. The way it is suggested in railscasts assumes the file is in a local system and that needs to be changed.
OLD METHOD:
def avatar_geometry(style = :original)
#geometry ||= {}
#geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
end
NEW METHOD
def avatar_geometry(style = :original)
#geometry ||= {}
avatar_path = (avatar.options[:storage] == :s3) ? avatar.url(style) : avatar.path(style)
#geometry[style] ||= Paperclip::Geometry.from_file(avatar_path)
end

AWS::Errors::MissingCredentialsError using paperclip and aws-s3 in rails 3.1

I am trying to upload image at aws.
class Asset < ActiveRecord::Base
belongs_to :post
attr_accessible :image
has_attached_file :image, :styles => { :medium => "640x480>",
:thumb => "100x100#"},
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'yourbucket'
end
s3.yml
development:
access_key_id: xxxxxxxx
secret_code: xxxxx
I am getting a message
AWS::Errors::MissingCredentialsError in PostsController#create
Missing Credentials.
Unable to find AWS credentials. You can configure your AWS credentials
a few different ways:
* Call AWS.config with :access_key_id and :secret_access_key
* Export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to ENV
* On EC2 you can run instances with an IAM instance profile and credentials
will be auto loaded from the instance metadata service on those
instances.
* Call AWS.config with :credential_provider. A credential provider should
either include AWS::Core::CredentialProviders::Provider or respond to
the same public methods.
= Ruby on Rails
In a Ruby on Rails application you may also specify your credentials in
the following ways:
Via a config initializer script using any of the methods mentioned above
(e.g. RAILS_ROOT/config/initializers/aws-sdk.rb).
Via a yaml configuration file located at RAILS_ROOT/config/aws.yml.
This file should be formated like the default RAILS_ROOT/config/database.yml
file.
I believe I am doing the last step.
Gemfile
gem 'rails', '3.1.3'
gem 'mysql'
gem 'koala'
gem 'paperclip'
gem 'aws-s3'
gem 'aws-sdk'
The following worked for me:
Create a file in your initializers called aws.rb
Put in the following in your aws.rb file:
AWS.config(
access_key_id: 'your_access_key',
secret_access_key: 'your_secret_access_key')
Then my paperclip options looks like this:
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "missing_:style.png", :default_url => 'missing_:style.png', :storage =>
:s3, :bucket => "<my_bucket>"

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!

How to use Seed data with Paperclip + S3

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

amazon s3+paperclip - AWS::S3::NoSuchBucket

I’ve used Uploadify and paperclip in Rails3 and I’m getting this error -
AWS::S3::NoSuchBucket (The specified bucket does not exist):
app/controllers/cards_controller.rb:79:in `create'
app/controllers/cards_controller.rb:78:in `create'
app/middleware/flash_session_cookie_middleware.rb:16:in `call'
My s3.yml file is
————————————————–
development:
bucket: bucket-dev
access_key_id: ###
secret_access_key: ###
test:
bucket: bucket-test
access_key_id: ###
secret_access_key: ###
production:
bucket: bucket-pro
access_key_id:###
secret_access_key: ###
————————————————–
and the model has -
————————————————–
has_attached_file :photo,
:storage => :s3,
:s3_credentials => “#{::Rails.root.to_s}/config/s3.yml”,
:path => “/:style/:filename”,
:styles => {:medium => “300×300>”,:thumb => “100×100>”}
————————————————–
Safalmj, do you have buckets created on S3 called 'bucket-test' and 'bucket-pro'? If not, sign into the AWS Console and create them first.
Wow. I was just having this very same issue. Then I read something about Paperclip creating buckets where they don't exist. I appended a 1 to the end of my already created bucket name, and all of a sudden it worked. I think it might have something to do with the other end. Try setting the bucket name to something other than the one you've probably already created... Highly counter intuitive...