Upload image using paperclip, fog and rackspace - ruby-on-rails

I need to upload an logo image to rackspace using fog & paperclip.
Paperclip::Attachment.default_options.update({
:path => "images/:class/:id/:attachment/:style/img_:fingerprint",
:storage => :fog,
:fog_credentials => {
:provider => 'Rackspace',
:rackspace_username => 'blablabla',
:rackspace_api_key => 'blablabla',
:persistent => false
},
:fog_directory => 'blablabla',
:fog_public => true,
:fog_host => 'http://blablabla.rackcdn.com'
})
I have this settings in config/initializers/paperclip_defaults.rb
But how to initialize the logo to catch those settings. Please help me i was in a confusion here.

You don't need to initialize the logo to "catch" those settings
Let me explain how it works:
Paperclip simply creates an entry into your db, and stores your file on Rackspace. Accessing the file, with paperclip will simply be a case of ensuring paperclip is able to load the RackSpace URL correctly
I would do this:
#config/application.rb
config.paperclip_defaults = {
styles: { :medium => "x500", :thumb => "x200" },
default_url: "placeholder.png"
}
#config/environments/production.rb
Paperclip::Attachment.default_options.merge!({
:path => "images/:class/:id/:attachment/:style/img_:fingerprint",
:storage => :fog,
:fog_credentials => {
:provider => 'Rackspace',
:rackspace_username => 'blablabla',
:rackspace_api_key => 'blablabla',
:persistent => false
},
:fog_directory => 'blablabla',
:fog_public => true,
:fog_host => 'http://blablabla.rackcdn.com'
})
This allows you to upload to Rackspace in production mode. You don't need to change the model, and would call your paperclip objects like this:
#model.image.url #-> yields rackspace URL
If you need more help / clarity, please let me know. I have just used your Rackspace code & moved from an initializer to your config files

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'

trouble with dots using rails paperclipftp

I got trouble with paperclipftp
Here my config :
has_attached_file :resume_img, :styles => { :thumb => "100x100#",:screen => '690x780#' },:retina => { :quality => 70 },
:path => "username.com/uploads/:attachment/:id/:style/:filename",
:url => "http://username.com/:attachment/:id/:style/:filename",
:storage => :ftp,
:ftp_credentials => { :host => 'ftp.username.com', :username => 'username', :password => '*******' },
:ftp_passive_mode => false,
:ftp_timeout => 90,
:ftp_verify_size_on_upload => false,
:ftp_debug_mode => false
My FTP is hosted by dreamhost, I need to upload inside the folder username.com but something strange appears rails escape the dots so it upload to usernamecom/uploads/
It does the same with the url it try to find the image at http://usernamecom/
Any idea???
Try it maybe escaped with
"username\.com/uploads/: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

Rails paperclip amazon aws s3 gem, how to change image url?

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.

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!

Resources