Rails paperclip amazon aws s3 gem, how to change image url? - ruby-on-rails

In my model I have:
has_attached_file :image,
:storage => :s3,
:styles => { :original => ["300x250>", :png], :small => ["165x138>", :png], :mini => ["120x120>", :png] },
:path => 'images/vind/:style/:id/:basename.:extension',
:url => 'images/vind/:style/:id/:basename.png',
:bucket => 'konkurrencerher',
:s3_credentials => {
:access_key_id => 'x',
:secret_access_key => 'x'
}
The problem is just that there is added the amazon s3 hostname to the url in view.
I have a solution to this, but is a bit ugly:
<%= image_tag(kon.photo.image.url(:small).gsub("http://s3.amazonaws.com/konkurrencerher", ""), :class => 'koni') %>
But, how is it possible to define the image url in the model, without the Amazon S3 hostname?

Take a look at Paperclip::Storage::S3, especially on the :s3_host_alias.
You can try configuring your has_attached_file with the following additional options
:url => ':s3_alias_url',
:s3_host_alias => "example.domain.net"
Hope this helps.

My solution created a file in the initializers map with this:
Paperclip.interpolates(:s3_path_url) { |attachment, style|
"#{(attachment.path).gsub("images/", "")}"
}
And then the url should be:
:url => ':s3_path_url'
This is a much better solution.

Related

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

How to store files on amazon S3 conditionally based on what environment you are in

I'm using Paperclip, and this code, along with the aws-s3 gem, allows me to store file uploads with Amazon S3:
has_attached_file :photo,
:styles => {
:tiny => "25x25#",
:shown => "40x40#",
:thumbnail => "50x50#",
:small => "150x150>",
:medium => "300x300>" },
:default_url => "/images/default_:style.jpg",
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "profile/:attachment/:style/:id.:extension"
However I do not want to store files on Amazon S3 when I am in my development environment. How do I set that in my application?
you could probably do something like
:storage => Rails.env.production? ? :s3 : :whatever
In the end of environment.rb:
APP_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/config.yml")[Rails.env]
In config/config.yml:
development:
use_amazon: false
test:
use_amazon: false
production:
use_amazon: true
And in your controller:
if APP_CONFIG['use_amazon']
#USING AMAZON S3
else
#USING SOMETHING ELSE
end
This should work.
Good Luck!

heroku+s3+paperclip

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.

Paperclip Amazon S3 setup with Heroku

has_attached_file :image, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :path => "/:style/:filename"
I'm not sure what :path => "/:style/:filename" is.
I also want to to include the style for this attached image, is that what the :path is?
the style I want is this: :styles => { :medium => "275x275>", :thumb => "175x155>" }
Basically what's going on here is that I'm setting up on heroku and I'm having to use S3 which seems straightforward just not used to this attachment convention stuff.
Also, I just signed up for an S3 account... but heroku was spouting that its free or something. What's the deal with that?
The 'path' specifies the location on S3 where the files will be stored. Thus, if you specify an attachment as:
has_attached_file :image,
:styles => { :medium => "275x275>", :thumb => "175x155>" },
:storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/amazon_s3.yml",
:path => "user/:attachment/:style/:id.:extension"
A sample URL will be:
http://s3.amazonaws.com/bucket/user/image/thumb/347853856.jpg
Finally, S3 is NOT free (Heroku simply states transfer / uploads are not counted in the usage based calculations). Heroku's documentation is excellent if you need further information.
Note that in Rails 3.1 and above, it should be Rails.root and not RAILS_ROOT

Resources