heroku+s3+paperclip - ruby-on-rails

Guys,
I'm having a problem with s3...I'm trying to configure the s3 this way to work with the paperclip:
has_attached_file :photo,
:storage => :s3,
:bucket => 'gallerybucket',
:styles => { :small => ["150", :png], :large => ["500", :png], :very_large => ['750x500>', :png] },
:path => ":rails_root/public/images/:class/:attachment/:id/:style_:basename.png",
:url => "/images/:class/:attachment/:id/:style_:basename.png",
:default_url => "/images/sem_imagem.gif",
:s3_credentials => {
:access_key_id => ENV['ac'],
:secret_access_key => ENV['sc']
}
but it always shows me this error. I don't understand what I'm doing wrong here. Is there some configuration missing?

If you don't have an s3 account already go get one here:
http://aws.amazon.com/s3/
You need to add this to your contact model:
app/models/contact.rb
has_attached_file :picture,
:styles => {:large => "275x450>"},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "appname/:attachment/:style/:id.:extension"
Make sure you appname is your rails app name on heroku. And make sure you rename picture to whatever you have named your picture.
Then you need a config file in config/s3.yml.
development:
bucket: bucked_name
access_key_id: key
secret_access_key: secret
production:
bucket: bucked_name
access_key_id: key
secret_access_key: secret
Make sure you get the key and secret correct.
In your gem file make sure you have these gems install :
gem "aws-s3", :require => "aws/s3"
gem "paperclip"

Sounds like you added the variables to you heroku account, but did you add them to your .bashrc file?
export ACCESS_KEY_ID='acckeyid'
export SECRET_ACCESS_KEY='secacckey'
Then in your code:
:s3_credentials => {
:access_key_id => ENV['ACCESS_KEY_ID'],
:secret_access_key => ENV['SECRET_ACCESS_KEY']
}
I have a blog post I wrote that talks about this a little as well.

Related

File upload to Amazon S3 using paperclip

I'm uploading files to S3 using paperclip so would like to know if i can set the path something like,
:path => "/advertisements/:username/:filename”
the thing is that :usename is from other model; i'm uploading files on model_2 and :username comes from model_1. How can i set the path to indicate the :username
Sample:
:path => "/advertisements/#model_1.username/:filename”
Any ideas?
Thanks in advance!
Here is nice explanation:
please view the answer.
Rails 4, Paperclip, Amazon S3 Config Amazon Path
Model:
#Image Upload
Paperclip.options[:command_path] = 'C:\RailsInstaller\ImageMagick'
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "****",
:storage => :s3,
:bucket => '****',
:s3_credentials => S3_CREDENTIALS,
:url => "/:image/:id/:style/:basename.:extension",
:path => ":image/:id/:style/:basename.:extension"
config/application.rb
# Paperclip (for Amazon) (we use EU servers)
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}
config/s3.yml
Amazon AWS Config
development:
access_key_id: **********
secret_access_key: **************
bucket: ****
production:
access_key_id: ***********
secret_access_key: ***********
bucket: ****
I hope this is what you're looking for :)

Trouble uploading files to Amazon s3 in development

Able to upload files to Amazon s3 in my production environment with Heroku, but unable to do this in my development environment. Here is how the situation looks currently
Development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
After following the advice of this S.O. Post, I created a file application.yml in my config directory with the following code:
AWS_ACCESS_KEY_ID: "***"
AWS_SECRET_ACCESS_KEY: "***"
AWS_BUCKET: "themoderntrunk"
I didn't change my design model though
has_attached_file :photo, :styles => { :thumbnail => "80x80#",
:small => "150x150>"
}
When I'm uploading file's now, i get this error
missing required :bucket option
Many people I've seen had this same problem, but none of the answers have been able to solve my problem. Any guidance would be truly appreciated. Thanks.
Allegorically, this particular issue has commonly been resolved using Fog, Ruby's canonical cloud services library.
# Gemfile
gem 'fog'
Run bundle install, then modify your configuration file as follows:
# config/environments/development.rb
Paperclip::Attachment.default_options.merge!(
:storage => :fog,
:fog_credentials => {
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
},
:fog_directory => ENV['AWS_BUCKET'],
:bucket => ENV['AWS_BUCKET']
)
Restart your server to reload the environment – uploads should work.
UPDATE:
Upon a rereading of your question, I noticed that you're to passing an environment global named ENV['S3_BUCKET_NAME'], but the variable you should actually pass is named ENV['AWS_BUCKET']. I suspect this is why the missing required :bucket option was being thrown. Renaming the variable may resolve your original issue:
# config/environments/development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'], # Matches global declaration in `application.yml`
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}

AWS Credentials Missing when pushing to Heroku

Answered: I misspelled AWS_SECRET_ACCESS_KEY when setting the environment variables for Heroku. I left off the second S.
I'm building a Rails 4 application, and using Paperclip and AWS for image uploading. Every time I try to upload an image, I get AWS::Errors::MissingCredentialsError (Missing Credentials in the Heroku logs.
In my gem file I have
gem 'paperclip'
gem 'aws-sdk'
In my production.rb file I have
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
I have an initializer named aws-sdk.rb that only has this in it:
AWS.config({
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
})
And in my relevant model file I have
has_attached_file :picture,
:storage => :s3,
:s3_credentials => {
:bucket => 'MyBucketName',
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},
styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
I didn't think that I needed to explicitly include the s3 credentials with the has_attached_file method? I tried it anyway. Still not working.
When I run heroku config it shows my access key, key id, and bucket names correctly.
I have no idea what's going wrong.

Paperclip + AWS S3, Prefixing remote path with my local path

I'm using Paperclip with a Rails 4 app and Amazon S3 storage. On my development machine, the site is running at
/Users/Jeff/Sites/example.com/web
When I upload a file with Paperclip to S3, the remote path in S3 inherits my local folder structure.
http://s3.amazonaws.com/example_com_bucket/Users/Jeff/Sites/example.com/web/public/assets/uploads/my_class/8/medium/some_image.png?1383060287
Why is this happening? How do I strip that part out? I tried changing the :path property but that only seemed to affect the "application" part of the path (e.g. after /assets/uploads) My site is still in development, so I don't care about having to preserve links.
My config is...
config.paperclip_defaults = {
:storage => :s3,
:path => '/:class/:attachment/:id_partition/:style/:filename',
:s3_credentials => {
:bucket => 'example_com_bucket',
:access_key_id => '...',
:secret_access_key => '...'
}
}
I had this exact same issue when I was using the :url parameter where I should have been using the :path parameter:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:url => '/product/:attachment/:id/:style/:filename'
I fixed it by changing my config to this:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:path => '/product/:attachment/:id/:style/:filename'

Rails download S3 file

I'm uploading files to S3 with paperclip, and now I would like to download them from the same app. So I'm doing what lots of pages says, but if I use 'aws-sdk' it says that AWS::S3::S3Object method 'find' doesn't exist, and If I use 'aws-s3' gem, it says that I need to use 'aws-sdk'.
In controller I'm calling:
aws_object = AWS::S3::S3Object.find #component.folder.path, 'bucket-name'
send_data(aws_object.value, :type => #component.folder_content_type)
EDIT:
My model looks like:
attr_accessible :folder
has_attached_file :folder,
:path => ":rails_root/data/folders/:id/:basename.:extension",
:storage => :s3,
:s3_credentials => {
:bucket => "my-bucket-name",
:access_key_id => "XXXXXXXXX",
:secret_access_key => "XXXXXXXXX"
}
This worked for me:
http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/
There's a example to download too.
The secret was ".read" :
data = open(asset.uploaded_file.url)
send_data data.read, :type => data.content_type, :x_sendfile => true,:filename => asset.file_name

Resources