How to automate a paperclip upload to amazon s3 in rails app - ruby-on-rails

I have some images that I want to move from myBucket/... to myBucket/someFolder/.... I have paperclip and aws-sdk setup to put new images in that location when uploaded on a form, but I can't figure out how to move the images to the location defined in production.rb:
config.paperclip_defaults = {
:storage => :s3,
:url => ":s3_eu_url",
:s3_protocol => "https",
:path => ":class/images/000/000/:id/:style/:basename.:extension",
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
How do you call paperclip on an image in a remote location in a controller method? I want something in my illustrationsController like the following:
def convert
#illustration = Illustration.find(params[:id])
Paperclip.someMagicMethod(#illustration.image_url)
end
Thanks.

Related

Rails Paperclip adds extra folder pointing to Cloudfront (between :url and :path)

I'm having trouble with loading images uploaded to s3 via my cloudfront distribution. I can get it to work fine loading straight from s3, but not from cloudfront.
I have set the config.action_controller.asset_host to my distribution url and have set up paperclip
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-sa-east-1.amazonaws.com',
:url => ':asset_host',
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
with the appropriate code in the model :path =>
':class/:id/:style/:class:style:id.:extension'
and every time I upload a file and try to access it i get
https://XXXXXX.cloudfront.net/images/:class/:id/:style/:class:style:id.:extension
I honestly can't figure out where the /images/ is coming from that is added between the url and the path. And I would be very greatful for any help or tips to solve this!
Thanks

Paperclip including bucket name in cloudfront url

When I try to use Cloudfront as my CDN the url incorrectly includes the bucket name such as
cloudfronturl.net/bucketname/pathToImage
instead of
cloudfronturl.net/pathToImage
on my image model
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:url => ':s3_domain_url',
:s3_host_name => ENV['CLOUDFRONT_URL']
}
I could try using a gsub on the image urls to replace ['AWS_BUCKET']+"/" but is there a way to configure my cloudfront or paperclip to do this automatically?
This was a pretty silly mistake. The url, path and host alias need to go on the same level as the s3_credentials
:url => ':s3_alias_url',
:s3_host_alias => ENV['CLOUDFRONT_URL'],
:path => ":attachment/:id/:style.:extension",
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
Seems as if your not specifying a path within your Paperclip config. You may need something like this
path: '/images/folder_name/:id/:style.:extension',
This is what i do and have never encountered the issue you specify with Cloudfront, i use it all the time.

Rails Paperclip S3 - missing required :bucket option

I'm trying to use Amazon S3 for Paperclip attachments. First, I'm trying to get it to work in development environment on my iMac.
I have created the Amazon buckets = ndeavor-dev and ndeavor-pro. In the code below, I have substituted the bucket name and keys. I have the gem's paperclip and aws-sdk.
The error I get is:
ArgumentError at /attachments
missing required :bucket option
I have tried this in my config/environments/development.rb:
config.paperclip_defaults = {
:storage => :s3,
:s3_protocol => 'http',
:bucket => ENV['AWS_BUCKET'],
:s3_credentials => {
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
And I tried this (moving the :bucket):
config.paperclip_defaults = {
:storage => :s3,
:s3_protocol => 'http',
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Thanks for the help!
Like dcro says, you need to set the AWS_BUCKET environment variable properly.
To do this, create a file at config/application.yml and put the following in it, using your Amazon credentials:
AWS_ACCESS_KEY_ID: "whatever_the_key_is"
AWS_SECRET_ACCESS_KEY: "whatever_the_secret_is"
AWS_BUCKET: "ndeavor-dev"
Then restart your server. You'll then be able to use your models something like this:
has_attached_file :attachment ,
:storage => :s3 ,
:s3_credentials => {:bucket => ENV['AWS_BUCKET' ],
:access_key_id => ENV['AWS_ACCESS_KEY_ID' ],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']},
:s3_protocol => "https" ,
:s3_host_name => "s3-eu-west-1.amazonaws.com"

Getting S3 Credentials from the database at startup in rails

Using a combination of the rails-settings gem and paperclip, I've got my application set up such that one can specify S3 credentials in the database (through an interface in the front end) and can then declare attachments like this;
class Asset < ActiveRecord::Base
has_attached_file :attachment,
:default_url => "content/no-img.jpg",
:s3_credentials => {
:access_key_id => Settings.AWS_ACCESS_KEY_ID,
:secret_access_key => Settings.AWS_SECRET_ACCESS_KEY
}
This works great, but this isn't the only type of attachment in the application (there are other things such as client logos). I'm finding that I need to specify the S3 credentials on every one. I've already tucked some of the defaults away in my app config file;
class Application < Rails::Application
#Paperclip Defaults
config.paperclip_defaults =
{
:storage => :s3,
:bucket => "my-bucket-name"
}
And this all works great, but if I try to pull down the username and password into the app config too, I get the following error;
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
A fairly self explanatory error message, but how do I get around it? If I put the config in the environment files I get the same issue, and if I put them in an initializer like this;
#Paperclip Defaults
MyApp::Application.config.paperclip_defaults =
{
:storage => :s3,
:bucket => "my-bucket-name",
:s3_credentials => {
:access_key_id => RailsSettings::Settings.AWS_ACCESS_KEY_ID,
:secret_access_key => RailsSettings::Settings.AWS_SECRET_ACCESS_KEY
}
}
Then the values don't seem to get picked up and paperclip defaults back to storing the files in my local system directory. Could anyone advise on the best approach to handling this?
EDIT
So after re-reading the documentation I discovered "After Initializers", which gets around the whole active-record not being loaded issue. I have this in my app config;
config.after_initialize do
#Paperclip Defaults
config.paperclip_defaults =
{
:storage => :s3,
:bucket => "my-bucket-name",
:s3_credentials => {
:access_key_id => RailsSettings::Settings.AWS_ACCESS_KEY_ID,
:secret_access_key => RailsSettings::Settings.AWS_SECRET_ACCESS_KEY
}
}
end
And specify this in each model;
has_attached_file :attachment,
:default_url => "content/no-img.jpg",
:path => "#{Rails.env}/:client_name/#{DateTime.now.year}/:name/:basename/:basename-:style.:extension"
yet it still isn't picking up the defaults I specify (it still tries to put them in the public/system folder)
Right, sorted. If anyone has this issue, here's the solution (you basically need to use the "initializers" method to switch active record on before you set the config);
initializer "active_record.initialize_database" do |app|
app.config.paperclip_defaults =
{
:storage => :s3,
:bucket => "my-bucket-name",
:s3_credentials => {
:access_key_id => RailsSettings::Settings.AWS_ACCESS_KEY_ID,
:secret_access_key => RailsSettings::Settings.AWS_SECRET_ACCESS_KEY
}
}
end

Using a non - US s3 server with rails, heroku and paperclip

I'm trying to set this up using the article on herokus developer site.
https://devcenter.heroku.com/articles/paperclip-s3
# config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:url => 'https://s3-ap-southeast-2.amazonaws.com'
}
}
However all of my images are pointing to the us http://s3.amazonaws.com/ server
How can i set paperclip to use the sydney server?
UPDATE:
paperclip successfully uploads files to the sydney server, it just wrongly uses the US server rather than the sydney one when trying to view them
Paperclip provides a handful of options, to optimize the configuration of your bucket.
The ones you're interested are url and s3_alias_url.
Your configuration would look like something like this :
config.paperclip_defaults = {
:storage =>:s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
},
:url => ':s3_alias_url',
:s3_alias_url => 's3-ap-southeast-2.amazonaws.com'
}
You can find more information and an other example in this google groups discussion.

Resources