Configure Rails Paperclip gem for use locally on Cloud9

rails
heroku

#1

I’m building a Rails app that allows users to attach an image to a model via a form. Heroku doesn’t allow storage of images, so I’m using the Paperclip gem to save attached images to AWS S3.

Heroku provides excellent detailed instructions to configure paperclip settings in the Production.rb environment:

config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch(‘S3_BUCKET_NAME’),
access_key_id: ENV.fetch(‘AWS_ACCESS_KEY_ID’),
secret_access_key: ENV.fetch(‘AWS_SECRET_ACCESS_KEY’),
s3_region: ENV.fetch(‘AWS_REGION’),
}
}

It also makes it clear which commands to run in order to configure the Heroku app settings to access my S3 storage account.

heroku config:set S3_BUCKET_NAME=your_bucket_name heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key heroku config:set AWS_REGION=your_aws_region

I’ve got everything working perfectly in Production on Heroku, but I’m struggling to figure out how to configure my development environment on Cloud9 to save images in the same way. I want to use Paperclip to save images to S3 rather than saving locally to the SQlite database on Cloud9.

I have placed the same credentials code in my Development.rb file:

config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch(‘S3_BUCKET_NAME’),
access_key_id: ENV.fetch(‘AWS_ACCESS_KEY_ID’),
secret_access_key: ENV.fetch(‘AWS_SECRET_ACCESS_KEY’),
s3_region: ENV.fetch(‘AWS_REGION’),
}
}

but I don’t know how to write the commands to tell Cloud9 my S3 credentials. Does anyone know what “equivalent” configuration commands I should run in the command line to tell Cloud9 my S3 credentials? (similar to these Heroku commands, I assume)…

heroku config:set S3_BUCKET_NAME=your_bucket_name heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key heroku config:set AWS_REGION=your_aws_region

Thanks!


#2

Well. Classic move. I figured it out.

Cloud9 allows you to set ENV variables through the UI. Use the Run Project panel and you’ll find an ENV button where you can input your variable names and values. I’ve got it working! Cloud9 connected to AWS. :slight_smile:


#3

Can you point out where this button is? I can only find a “Run Configurations” panel that is as clear as mud as to how this updates my otherwise working existing project.


#4

When you create a run configuration, you can click at the top right and add environment variables using the key -> value system there.


#5

Umm. I found that but I’m none the wiser. There are no configurations already there and I don’t seem to be able to create one. My server runs quite happily so presumably it’s running a default configuration and I’d like to simply tweak that - is that possible?
BTW any idea why I’m currently looking at a gray page with only a small ‘bright’ circle at top right around my ‘icon’?


#6

Can you post a screenshot here, I’m not sure I understand?


#7

If you want to store your images in S3, I think you will also need to install an additional gem:

gem 'aws-sdk', '~> 2.3.0'

Then you can add your amazon credentials in your YAML file (whoever keep in mind that your workspaces are public, so you should use something like Figaro to keep those credentials well safe and concealed):

development:
  access_key_id: 123...
  secret_access_key: 123...
test:
  access_key_id: abc...
  secret_access_key: abc...
production:
  access_key_id: 456...
  secret_access_key: 456...

Then in your Model you can use something like:

class User
  has_attached_file :download,
                    :storage => :s3,
                    :s3_credentials => Proc.new{|a| a.instance.s3_credentials }

  def s3_credentials
    {:bucket => "xxx", :access_key_id => "xxx", :secret_access_key => "xxx"}
  end
end

I personally think that using Carrierwave (instead of Paperclip) is much more straightforward and easy to combine with S3, you might want to give it a try as well.


#8

BigChief45 - Thanks, I know all of this apart from how I save the credentials securely on Cloud9. Currently I set them as Linux environment variables from my console because launching my dev server but it appears there is a better way to achieve what I need. Will try and get a screenshot tonight to show where I’m confused.


#9

Making progress - I think. What I’ve previously been doing is starting the Rails server from a simple terminal using a shell script containing:
rails server -b $IP -p $PORT
I now also start a delayed_job worker thread from a different terminal window using another shell script…
bundle exec rake jobs:work
So I’m guessing this is wrong and I should be creating a project and using ‘Run’ to start my server and worker thread.

So I’ve now managed to create a project, set the security environment variables and make this project the ‘default’ but it’s not clear to me how I can start both my worker thread and server thread. Will “Stop” stop both of them if I push the worker thread into the background?

If there is a good primer on ‘cloud9 projects’ and how I should be using them, please let me know and I’ll go and read up.
Thanks,
Papadeltasierra


#10

Actually, starting your Rails server using the rails server command is the Rails way of doing it.

Cloud9 Runners and the Run button is just an additional convenience added by Cloud9, and you are not obligated to use it.

For delayed jobs in Rails you might also want to take a look at the Whenever gem


#11

Hi,

I’m working on a project called End Bias Wiki and we’re encouraging submissions for Hacktoberfest and also encouraging people to use Cloud9 for development. I was wondering if you could elaborate on how Figaro would prevent viewing config information in public workspaces? It doesn’t seem clear to me upon first viewing. Thanks.