Reputation: 13
I'm building a fairly basic Ruby on Rails app, I'll be using about 2000 images, and this is my first real dive into aws/s3. The app won't have any user interaction, so I'm not sure if it's better to have all of the images on the app, and then upload them to my bucket, or add them to my bucket manually, and then download them to the app from there. The AWS documentation is a bit all over the place.
I currently have carrierwave installed and not sure what the next steps should be, or how to retrieve images from S3 into rails. I'll be using Heroku as well, but I've already set up the config with my AWS credentials.
uploaders/photo_uploader.rb
class PhotoUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def content_type_whitelist
/image\//
end
end
initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_provider = 'fog/aws'
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"]
}
config.fog_directory = ENV["S3_BUCKET"]
end
Upvotes: 0
Views: 2361
Reputation: 15393
First step is to integrate image uploading and you can utilize a number of libraries to make this happen.
You want to grab dotenv-rails
gem so you can securely manage the credentials you will need from AWS S3. This is a dedicated resource for production ready RoR app.
The next gem you want is the carrierwave-aws
and the carrierwave
gem that will manage everything and so that's three gems thus far. Fourth and final gem is mini_magick
which is a requirement in order to use the methods available by carrierwave
.
Second step is to sign up to an AWS account to use the S3 bucket. You cannot have the images on the app because if you do, you will not be able to deploy to Heroku with the images. Heroku will get rid of them.
Once you've installed these gems, you run a bundle install
and then build out the basic functionality.
Here is some documentation on carrierwave
: https://github.com/carrierwaveuploader/carrierwave
The documentation in the above link will walk you through how to properly install carrierwave
.
So you will do something like:
rails generate uploader Photo
In your photo_uploader.rb
file, you want to uncomment this code:
def extension_whitelist
%w(jpg jpeg gif png)
end
You want this uncommented to serve as a validator of the type of files you can upload. So if its not a jpg jpeg gif png
RoR will throw an error. This whitelist is handy so I strongly recommend it.
Next, you have to set up your mapping between your uploader and the database.
So, fast forwarding to the part where you need to connect AWS to your app. This is where your dotenv-rails
gem comes in. By the way, all these gems can be found in rubygems.org.
In the root of your folder, you are going to create a file called .env
.
In the .env
file you are going to add these:
S3_BUCKET_NAME=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
Never push the AWS keys to any codebase versioning tool like Github.
You want to go into your .gitignore
file and ensure the .env
file is included. This way git will not track that file.
To get your AWS credentials, go to your name in the AWS console and click on it and you will see a dropdown with
my security credentials
as an option.
Next, create your bucket.
To test successful integrating with your RoR app, go to rails console
and run this command:
ENV.fetch('S3_BUCKET_NAME')
If you get an error at this stage, you may need to go to config/application.rb
and add:
require "dotenv-rails"
Once having done that, go back into rails c
and run ENV.fetch('S3_BUCKET_NAME')
again and you should be goood to go if you followed the steps correctly.
You should have an initializers
folder and in there you are going to create carrierwave.rb
file.
Inside of that file you are going to paste all the code thats under the Usage section of this documentation:
https://github.com/sorentwo/carrierwave-aws
Go back to your photo_uploader.rb
file and change storage :file
to storage :aws
.
Home stretch here, go back to carrierwave.rb
file and there is one line of code you need to completely remove from what you copy and pasted from the above link and it is this line here:
config.asset_host = "http://example.com/
Now you can start up your rails server
and instead of pointing to your local file system it should now be pointing to your bucket.
Upvotes: 1
Reputation: 7777
You need to upload these all images using application, after install carrierwave
and fog-aws
then you need to create model
controller
and form
for uploading images.
OK, currently you have confused how to show image after uploaded, Right?
The simple is if image uploaded properly then the imagine the table is images
and model is Image
and column is picture
because you have did not provided those names.
images_controller
class ImagesController < ApplicationController
def index
@images = Image.all
end
end
view/images/index.html.erb
<% @images.each do |image| %>
<%= image_tag image.picture.url %>
<% end %>
Note
This not to promote a product
If you need to see a sample with source code then this is the BitBucket repository and this is the live Heroku app and Stripe test card number a CVC code must be provided type anything like 232
etc.
Upvotes: 0