Rails 3 - Amazon S3 Paperclip EU Problem - ruby-on-rails-3

I'm using:
Paperclip 2.3.16
Rails 3.0.9
Ruby 1.9.2
AWS - S3 0.6.2
I'm trying to use paperclip the upload to the EU (Ireland) based bucket. I have the following in my model:
has_attached_file :image, :styles => { :grid => '90x128#', :list => '140x200#', :original => '400x548'},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:url => 'flyers/:id/:style/:basename.:extension',
:path => 'flyers/:id/:style/:basename.:extension',
:bucket => 'fsight'
In my environment.rb I have set the write to use the AWS/s3 Default Host to the relevant EU one by using:
require "aws/s3"
AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com"
This works fine, and it allows me to upload the images, and I can verify the image upload / delete using the AWS Management consoler.
However, I have a problem when trying to display the images on my site. The images do not load and I have identified the cause, as the URL generated uses the old default host. Eg:
What it should be:
https://s3-eu-west-1.amazonaws.com/fsight/flyers/50/full/4759543368588654950.jpg
What it actually is: http://s3.amazonaws.com/fsight/flyers/50/full/4759543368588654950.jpg?1314801178
As you can see, it uses the old default host.
I tried placing:
Paperclip.interpolates(:s3_eu_url) do |att, style|
"#{att.s3_protocol}://s3-eu-west-1.amazonaws.com/#{att.bucket_name}/#{att.path(style)}"
end
But then started receiving the following error:
wrong number of arguments (0 for 1)
Extracted source (around line #9):
<img src= <%= #event.image.url(:original) %>
I know Paperclip has some issues with using EU Buckets, but could anybody help me with this?

You don't need to work around the EU Problem anymore.
The default aws-s3 storage backend in paperclip was replaced by the AWS SDK for Ruby, that is also the amazon recommended way when working with AWS.
Just insert
gem 'aws-sdk'
into your Gemfile and run bundle install.
If you want something like https://s3-eu-west-1.amazonaws.com/some_path_goes_here, try to configure your model's has_attached_file with the following options
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:s3_permissions => :private,
:s3_protocol => 'https',
:s3_host_name => 's3-eu-west-1.amazonaws.com',
:path => ":filename"
If you don't want to use https you can remove the :s3_protocol and if you want to change the region, option :s3_host_name is the right way to go. You can also put this into a configuration file.
Hope this helps.

Did you try this workaround?
Paperclip et les European S3 buckets
Or even this one?
Paperclip, S3, and European Buckets

I added
Paperclip::Attachment.default_options[:s3_host_name] = 's3-eu-west-1.amazonaws.com'
to paperclip.rb in the initializers folder and it works fine for me.

Same problem here, just solved passing the following option to has_attached_file:
:url => ':s3_domain_url'
For more info see here http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3 :
Normally, this won't matter in the slightest and you can leave the default (which is path-style, or :s3_path_url). But in some cases paths don't work and you need to use the domain-style (:s3_domain_url).

Related

Pydio Amazon S3 custom server

I trying to integrate S3fs into Pydio to use my own storage servers (so not amazon).
Accessing an s3fs mount as local filesystem from Pydio is malfunctioning, there are bunch of commands like ls which doesnt work on it therefore I must use the aws-sdk to interface with it from pydio.
The problem is that from the Amazon SDK it's only possible to select Amazons own servers through a region drop down list. To complicate things I also need to use proxy to access my own s3 storage.
Did anyone manage to implement this?
Using just the amazon Sdk how would this look like from php?
What I tried:
<?php
require_once("/usr/share/pydio/plugins/access.s3/aS3StreamWrapper/lib/wrapper/aS3StreamWrapper.class.php");
use Aws\S3\S3Client;
if (!in_array("s3", stream_get_wrappers())) {
$wrapper = new aS3StreamWrapper();
$wrapper->register(array('protocol' => 's3',
'http' => array(
'proxy' => 'proxy://10.0.0.1:80',
'request_fulluri' => true,
),
'acl' => AmazonS3::ACL_OWNER_FULL_CONTROL,
'key' => "<key>",
'secretKey' => "<secret>",
'region' => "s3.myprivatecloud.lan"));
}
?>
Thanks
if this is still a pending question, FYI in the latest versions (v6 beta 2) we've changed the access.s3 plugin to use the last version of aws-sdk, and also we added some parameters to easily use this plugin pointing to alternative s3-compatible storages.
-c

Ruby on Rails: Active Admin and Heroku - dashboard error

I have some Ruby on Rails applications that are using Active Admin gem. Rails version is 3.2.6, Active Admin version is 0.4.4.
I am pushing these applications to Heroku, then doing migrations and everything is working fine. But after some time (when application restarts) i'm starting to get 404 error when trying to open admin page (like myapp/admin). In logs there is an error:
ActionController::RoutingError (uninitialized constant Admin::DashboardController)
Moreover, if i'm trying to open some other admin page (like myapp/admin/videos - to administer videos) everything is still working fine, but error 404 persists when opening Dashboard page.
I have tried to put
config.cache_classes = true
config.assets.compile = true
to my config files, but all the same.
Basically the scheme is as follows:
I make some changes to the app, commit the changes with "git add .", "git commit" and push it to Heroku
I open the /admin page on Heroku and it works fine
After application restarts i get 404 error when visiting /admin, but everything still works when accessing other admin pages, not dashboard
GoTo 1
I'm still unsure if the error appears when the app is restarted by itself (not by "heroku restart").
Any ideas why this is happening? Maybe someone can advice how to switch off this Dashboard and use my myapp/admin/videos as the default admin page?
I had this very same problem, and since it complaints about the ActiveAdmin Dashboard, and it is now deprecated I proceeded to update my dashboard.rb file to the new Dashboard style and that solved the problem.
(I got that file from here).
Hope it helps.
I got the same issue.
Check whether you have to upgrade ActiveAdmin to new version
When upgrading to a new version of ActiveAdmin you may need to run
rails generate active_admin:assets
If you get:
uninitialized constant Admin::DashboardController
Use the New default page for admin/dashboard.rb
which is like following,
ActiveAdmin.register_page "Dashboard" do
menu :priority => 1, :label => proc{ I18n.t("active_admin.dashboard") }
content :title => proc{ I18n.t("active_admin.dashboard") } do
div :class => "blank_slate_container", :id => "dashboard_default_message" do
span :class => "blank_slate" do
span I18n.t("active_admin.dashboard_welcome.welcome")
small I18n.t("active_admin.dashboard_welcome.call_to_action")
end
end
# Here is an example of a simple dashboard with columns and panels.
#
# columns do
# column do
# panel "Recent Posts" do
# ul do
# Post.recent(5).map do |post|
# li link_to(post.title, admin_post_path(post))
# end
# end
# end
# end
# column do
# panel "Info" do
# para "Welcome to ActiveAdmin."
# end
# end
# end
end # content
end

ActionMailer sending real emails in test mode! - How to turn off?

Newly signed up users to my little app must be approved by the admin (me) before they can gain access to the site. I've succeeded in generating such emails in development with an after_create :send_admin_email in my user model which works great. My problem is that I'm generating multiple users during my tests (using FactoryGirl) and each test user created sends off a real email. Running my tests is like pouring molasses in January and I've got to delete hundreds of emails sent to my inbox. How do I turn that off?
Action Mailer Basics in the Rails Guides tells me that "By default Action Mailer does not send emails in the test environment. They are just added to the ActionMailer::Base.deliveries array."
Moreover, in config/environments/test.rb I've got:
config.action_mailer.delivery_method = :test
That's in addition to in config/environment.rb having:
# Configuration for using SendGrid on Heroku
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => 'app[my app number]#heroku.com',
:password => '[something super secret]',
:domain => '[let's get this party started!.com]',
:enable_starttls_auto => true
}
ActionMailer::Base.delivery_method = :smtp
I'm sure that I'm missing something simple and basic. I've searched around and related questions and posts deal with how to test that ActionMailer actually sent email.
Humble gratitude in advance for any thoughts or help.
Addendum: Following answer to similar question found at Is it possible to turn off ActionMailer emails when cucumber testing is happening on development? I was able to stop the email sending madness. Still, I had to add ActionMailer::Base.delivery_method = :test to several rspec files. Is there a way I can shut this down globally? Anyone have any thoughts on what's going on?
So I've figured it out. Having the line ActionMailer::Base.delivery_method = :smtp in config/environment.rb overrides ActionMailer::Base.delivery_method = :test in config/environments/test.rb.
So, delete that line, ActionMailer::Base.delivery_method = :smtp'
from config/environment.rb and place it in config/environments/production.rb.
That allows you to place ActionMailer::Base.delivery_method = :test in config/environments/test.rb and the version you want in config/environments/development.rb. I made development.rb :test as I populated my database using Faker and changed it to smtp so I was sure that real emails were sent as an additional check.
Note: You must restart your server for these changes to take effect.
Another note: Heroku's current SendGrid Instructions put the SendGrid Heroku configuration code in a new config/initializers/mail.rb file which will likely require removing its last line and placing the desired version in each config/environments/[production.rb, development.rb, test.rb]
Perhaps useful...
My config/environment.rb did not contain ActionMailer::Base.delivery_method = :smtp and my config/environments/test.rb did contain ActionMailer::Base.delivery_method = :test but Rails still delivered mailers while testing.
I simply added the following to config/environments/test.rb to fix:
config.action_mailer.perform_deliveries = false
I faced the similar situation in Rails4.2 (ActiveJob integration with ActionMailer) even if I didn't write delivery_method = :smtp in config/environment.rb.
In my case, The issue here happened after using "resque" as background worker. Finally, I found out that the following config was WRONG:
config/initializers/active_job.rb:
Rails.application.config.active_job.queue_adapter = :resque # here is WRONG
...because it effected to test mode as well.
So, I set "queue_adapter = :resque" only in config/environments/{development,production.rb}. Now, that works as I expect.

Heroku Cedar: How to scale WEB dynos based on time of day

I want my Rails 3.1 app to scale up to 1 web dyno at 8am, then down to 0 web dynos at 5pm.
BUT, I do not want to sign up for a paid service, and I cannot count on my home computer being connected to the internet.
It seems like the Heroku Scheduler should make this trivial. Any quick solutions or links?
The answer is 'yes' you can do this from scheduler and it's trivial once you know the answer:
Add a heroku config var with your app name: heroku config:add APP_NAME:blah
Add gem 'heroku' to your Gemfile
In order to verify, manually scale up/down your app: heroku ps:scale web=2
Add a rake task to lib/tasks/scheduler.rake:
desc "Scale up dynos"
task :spin_up => :environment do
heroku = Heroku::Client.new('USERNAME', 'PASSWORD')
heroku.ps_scale(ENV['APP_NAME'], :type=>'web', :qty=>2)
end
# Add a similar task to Spin down
Add the Scheduler addon: heroku addons:add scheduler:standard
Use the Scheduler web interface to add "rake spin_up" at whatever time you like
Add a rake spin_down task and schedule it for whenever.
Notes:
Step 1 is needed because I couldn't find any other way to be certain of the App name (and I use 'staging' and 'production' environments for my apps.
Step 3 is required because otherwise the ruby command errors out as it requires that you first agree (via Yes/No response) that you will be charged money as a result of this action.
In step 4, I couldn't find any docs about how to do this with an API key via the heroku gem, so it looks like user/pass is required.
Hope this helps someone else!
Just implemented this approach (good answer above #dnszero), thought I would update the answer with Heroku's new API.
Add your app name as a heroku config variable
require 'heroku-api'
desc "Scale UP dynos"
task :spin_up => :environment do
heroku = Heroku::API.new(:api_key => 'YOUR_ACCOUNT_API_KEY')
heroku.post_ps_scale(ENV['APP_NAME'], 'web', 2)
end
This is with heroku (2.31.2), heroku-api (0.3.5)
You can scale your web process to zero by
heroku ps:scale web=0
or back to 1 via
heroku ps:scale web=1
you'd then have to have a task set to run at 8 that scales it up and one that runs at 17 that scales it down. Heroku may require you to verify your account (ie enter credit card details) to use the Heroku Scheduler plus then you'd have to have the Heroku gem inside your app and your Heroku credentials too so it can turn your app on or off.
But like Neil says - you get 750hrs a month free which can't roll over into the next month so why not just leave it running all the time?
See also this complete gist, which also deals with the right command to use from the Heroku scheduler: https://gist.github.com/maggix/8676595
So I decided to implement this in 2017 and saw that the Heroku gem used by the accepted answer has been deprecated in favor of the 'platform-api' gem. I just thought I'd post what worked for me, since i haven't seen any other posts with a more up-to-date answer.
Here is my rake file that scales my web dynos to a count of 2. I used 'httparty' gem to make a PATCH request with appropriate headers to the Platform API, as per their docs, in the "Formation" section.
require 'platform-api'
require 'httparty'
desc "Scale UP dynos"
task :scale_up => :environment do
headers = {
"Authorization" => "Bearer #{ENV['HEROKU_API_KEY']}",
"Content-Type" => "application/json",
"Accept" => "application/vnd.heroku+json; version=3"
}
params = {
:quantity => 2,
:size => "standard-1X"
}
response = HTTParty.patch("https://api.heroku.com/apps/#{ENV['APP_NAME']}/formation/web", body: params.to_json, headers: headers)
puts response
end
As an update to #Ren's answer, Heroku's Platform API gem makes this really easy.
heroku = PlatformAPI.connect_oauth(<HEROKU PLATFORM API KEY>)
heroku.formation.batch_update(<HEROKU APP NAME>, {"updates" =>
[{"process" => <PROCESS NAME>,
"quantity" => <QUANTITY AS INTEGER>,
"size" => <DYNO SIZE>}]
})
If you're running on the cedar stack, you won't be able to scale to zero web dynos without changing the procfile and deploying.
Also, why bother if you get one free dyno a month (750 dyno hours, a little over a month in fact)?

How do you configure MongoMapper 0.11 with MONGOHQ_URL?

I want to run MongoMapper on Heroku. This necessitates MongoHQ. All the documentation, like http://mongomapper.com/documentation/getting-started/rails.html, tells you to create an initializer to connect to Mongo with the MONGOHQ_URL environment variable. However, recent versions on MongoMapper don't require an initializer, it just connects automatically now based on the content of config/mongo.yml.
Here inlies the problem: mongo.yml doesn't seem to allow me to connect with a URL, only with host, port, etc. I tried the following and it didn't work for me:
production:
url: <%= ENV['MONGOHQ_URL'] %>
Any other ideas on how to get this working?
(the error I get when deploying to heroku is that the app is trying to connect to localhost:27017 and failing.)
Wait I figured it out...
production:
uri: <%= ENV['MONGOHQ_URL'] %>
(uri, not url)