Deploying on Heroku - Images disappears after upload - ruby-on-rails

I deployed an application on Heroku which I wrote on Ruby on Rails. It is a movie review app.
I am able to upload images from my computer, to the web app online. Everything else works as per my expectations.
The images disappear after a day. My requirement is to have the image continue to render.
I am using Paperclip gem from rails. This only happens on the deployed version and not on localhost.

The default upload location on Heroku is into temporary storage. This is because you will get a different web worker each time you deploy.
You need to use S3 or another location to store your files. Luckily this is well documented for Paperclip on Heroku.
The main configuration difference is this. Add gem 'aws-sdk' to your Gemfile and then adjust your config file in config/environments/production.rb:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}

Related

Sitemap generator not uploading sitemap

I started playing with a test app trying to upload sitemaps using Amazon S3. I've been following https://github.com/kjvarga/sitemap_generator trying to figure out the gem and have only been half successful. A sitemap will generate in the public folder, but not upload to the S3 bucket.
I've added the config/sitemap.rb found in the tutorial above.
require 'rubygems'
require 'sitemap_generator'
require 'aws-sdk'
SitemapGenerator::Sitemap.create_index = true
SitemapGenerator::Sitemap.default_host = 'https://www.myapp.herokuapp.com'
SitemapGenerator::Sitemap.create do
add '/home', :changefreq => 'daily', :priority => 0.9
end
SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new(fog_provider: 'AWS',
aws_access_key_id: 'KEY',
aws_secret_access_key: 'SECRET',
fog_directory: 'DIR',
fog_region: 'REGION')
I type in
rails sitemap:refresh
in my terminal and it generates the maps. It just doesn't upload them. No errors, no clues as to what didn't happen, nothing. It even tells me that google and bing are successfully pinged.
Of course I can visit my AWS bucket and manually upload these files but that feels...wrong. I've used shrine for images in the past and am used to uploading to a cache. There must be something I missed.
Check your secrets, maybe you don't have the aws account env vars so S3 adapter would never works or policy in your bucket

AWS Permanent Redirect Error with Paperclip & Rails

Up until today, I have had no issues setting up S3 on my development environment. I am now constantly getting the following permanent redirect error:
AWS::S3::Errors::PermanentRedirect at /locations/1
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
Paperclip is working perfectly fine. The issue only arose when I tried to move hosting images onto S3.
1/ the AWS-SDK gems is installed
// gemfile
gem 'aws-sdk', '< 2.0'
2/ Paperclip defaults are properly setup; both access keys are configured correctly in my bash_profile
//development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => 'traipse-temp',
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
3/ I have a S3 bucket created titled "traipse-temp" set to US Standard
Does anyone have any insight as to why I am receiving a permanent redirect error? I have setup 4 other applications this exact same why but only with this latest project did I receive this first-time error.
Thanks in advance.

Upload image with RoR and Heroku

I recently deployed my ruby on rails app on Heroku and I am using the pg gem for my database.
I created a page where I can upload images and have them show on the main page and they are saved in a public folder within the app.
When I deployed it was working fine, but after sometime the images disappeared. When I looked at my Heroku logs I got the following:
ActionController::RoutingError (No route matches [GET] "/assets/products/6/photos/medium/shanab.jpg"):
I think the images are saved, but I don't know how to check if they are still there.
but after sometime the images disappeared
Heroku runs an ephemeral file system which means that each time you deploy a new version of your code, the old files/code are overwritten:
Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.
This means that even if your images are working for a time, each new deploy will basically overwrite them. This is probably the cause of the problem, although I can't be sure without further inspection.
--
The way around this is to use a CDN, most common (although incorrect) is to tie your app with S3.
I don't know which asset-processing gem you're using, but if it's Paperclip, you'll want to use the inbuilt S3 functionality to put all your image assets onto Amazon's servers:
#app/models/x.rb
class x < ActiveRecord::Base
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

Amazon S3 settings for Development and Production

I am using CarrierWave and Fog to upload images and process them to Amazon S3.
Below is my Fog settings.
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'AKIAJ23D1I25B2P2HX6A',
:aws_secret_access_key => 'WV64nQAd111+ZelqKgffrzvViG0lEeTTnEOonXHkg'#,
#:region => 'us-west-2'
}
config.fog_directory = "<TESTING>"
end
Is there a way to have two settings that gets used when the app is in production and development environment so that we dont mess with the files in the product and can delete it in dev one.
Yes you can use different settings by specifying in the particular environment. I am using this in my project but what I have done is I am using different directories for both. Like in the production.rb I am using project_directory and in development.rb I am using project_dev_directory. I have specified the same settings in both the environments. If you need to specify different settings you can do that too. Hope this helps.

Routing error for images Ror

I run my app locally on 3000 and all is fine, but when i upload to heroku it is sub standard.
I am using paperclip gem,
my Heroku logs issues this error
ActionController::RoutingError (No route matches [GET] "/images/medium/missing.png"):,
There seems to be no path to this instance in my pipeline, If it runs locally and not in heroku i figure it's a Heroku config problem?.
For heroku you have to additionally use the aws-sdk gem like shorlty described in the excellent paperclip-intro.
Sample configuration:
# config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Best info point is usually heroku itself, see this example configuration guide
paperclip gem means you are uploading images, and want to access the image via that GET request right?
that path looks like it is for the heroku server itself. Heroku doesn't support file uploads, get amazon s3 setup or some other storage facility.
it works locally because your local setup allows for file uploads and storage.

Resources