asset_sync not uploading to S3 - ruby-on-rails

I'm have trouble uploading my assets to S3 with asset_sync
production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3-eu-west-1.amazonaws.com"
config.assets.digest = true
config.assets.enabled = true
config.assets.initialize_on_precompile = true
asset_sync.rb
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = 'eu-west-1'
end
end
heroku config
AWS_ACCESS_KEY_ID: XXX
AWS_SECRET_ACCESS_KEY: XXX
FOG_DIRECTORY: bucket_name
FOG_PROVIDER: AWS
FOG_REGION: 'eu-west-1'
$export
declare -x AWS_ACCESS_KEY_ID= XXX
declare -x AWS_SECRET_ACCESS_KEY= XXX
declare -x FOG_DIRECTORY="bucket_name"
declare -x FOG_PROVIDER="AWS"
http://blog.firmhouse.com/complete-guide-to-serving-your-rails-assets-over-s3-with-asset_sync
ones push on heroku assets points to //bucket_name.s3-eu-west-1.amazonaws.com/assets/icons/name_xxxxxxxxxx.png
and when running $rake assets:precompile the files dose not get uploaded to S3 and gets only precompile locally. Any idea ? Thanks a lot.
EDIT:
I've just changed the Gemfile from :
group :assets do
gem 'asset_sync'
end
to the global gems
gem 'asset_sync'
and now I having warning message [WARNING] fog: followed redirect to bucket_name.s3-external-3.amazonaws.com, connecting to the matching region will be more performant
I think I can figure this out, but only the css files gets uploaded.not the js file and images.

Your bucket_name needs to be the bucket you have on S3
You should change these commands:
Heroku
FOG_DIRECTORY: your_bucket_real_name
Local
declare -x FOG_DIRECTORY=your_bucket_real_name
Also, you should change this in your production.rb file:
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
I think that will resolve your issue. I'm using S3 on EU-West with exactly the same setup (bar the differences I've cited), and it's working in the most part :)

Related

Getting "Expected(200) <=> Actual(400 Bad Request)" error for asset_sync gem

I am trying to set up existing rails 3.2.11 project in production environment.
We have used asset_sync gem for syncing assets in s3. But when I ran the precompile command I am getting Expected(200) <=> Actual(400 Bad Request) error. As per the documentation I have added required configuration in the file (application.yml file).
production:
FOG_DIRECTORY: xxxxxx
FOG_PROVIDER: xxxxxx
FOG_REGION: xxxxxx
ASYNC_UPLOAD_BUCKET: xxxxxx
AWS_ACCESS_KEY_ID: xxxxxx
AWS_BUCKET: xxxxxx
AWS_SECRET_ACCESS_KEY: xxxxxx
As per the doc created separate initializer file (config/initializers/asset_sync.rb)
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = ENV['FOG_REGION']
end
When I checked the response for the compilation it is showing message
<Message>The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.</Message>
Can any one let me know how can I fix this issue.

Fail to deploy in heroku when using carrier wave and S3

I am a newbie in rails.
Right now using a tutorial for ruby and rails. In one of the section we using carrier wave and AWS S3 for photo storage.
when i deploy in heroku, there is an errors like this :
"ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key"
i write on the carrir_wave.rb as follow:
if Rails.env.production?
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['******************'],
:aws_secret_access_key => ENV['*************************']
}
config.fog_directory = ENV['*******************']
end
end
when I am running my test in terminal, all test was good.
I had been setting the credential on heroku through:
$ heroku config:set S3_ACCESS_KEY=<access key>
$ heroku config:set S3_SECRET_KEY=<secret key>
$ heroku config:set S3_BUCKET=<bucket name>
I am really appreciate with any one help.
Create some carrierwave.rb filr in config/initializers folder and place following code snippet in carrierwave.rb file
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS', # required
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], # required
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] # required
}
config.fog_directory = ENV['S3_BUCKET_NAME'] # required
end
and place your values for following keys in some .env file in project folder
1) AWS_ACCESS_KEY_ID
2) AWS_SECRET_ACCESS_KEY
3) S3_BUCKET_NAME
Then re-run your server from terminal

Missing required arguments: aws_access_key_id, aws_secret_access_key

I'm using carriervawe and fog with S3 bucket. I get the error in the title in development (when I run rails s or rake db:migrate) with the following code:
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
config.asset_host = "http://xxx.cloudfront.net"
config.fog_directory = 'xxx'
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
config.storage = :fog
end
I've also tried using (as suggested here)
<%= ENV['AWS_ACCESS_KEY_ID'] %>
but I get this error:
syntax error, unexpected '<' (SyntaxError)
My variables are in application.yml file
AWS_ACCESS_KEY_ID: AKIAIxxx...
AWS_SECRET_ACCESS_KEY: 1xxx...
Not sure why, but for some reason your environment variables are likely being evaluated to nil. I like to use the figaro gem to manage my environment variables.
Simply add
gem "figaro"
to your gemfile.
Then run
figaro install
which will create an application.yml file and add it to your .gitignore (very important for security reasons). After this, you should be able to add your AWS keys to application.yml and access them in your carrierwave config like your currently have.
If this is from the Michael Hartl tutorial, I solved my issues by renaming the initializer to carrierwave.rb instead of carrier_wave.rb, as suggested in the tutorial. I then re-ran the git and Heroku commands and it worked on the Heroku production server.

Heroku asset_sync gem not uploading to s3

My asset_sync gem does not upload to s3 when I precompile assets
asset_sync.rb
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
# To use AWS reduced redundancy storage.
# config.aws_reduced_redundancy = true
config.fog_directory = ENV['FOG_DIRECTORY']
# Invalidate a file on a cdn after uploading files
# config.cdn_distribution_id = "12345"
# config.invalidate = ['file1.js']
# Increase upload performance by configuring your region
config.fog_region = 'ap-southeast-2'
#
# Don't delete files from the store
# config.existing_remote_files = "keep"
#
# Automatically replace files with their equivalent gzip compressed version
# config.gzip_compression = true
#
# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
# config.manifest = true
#
# Fail silently. Useful for environments such as Heroku
# config.fail_silently = true
end
end
production.rb
config.assets.enabled = true
config.assets.digest = true
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
config.assets.initialize_on_precompile = true
application.rb
config.assets.enabled = true
config.assets.digest = true
When I precompile, I'm not even getting any message to show it is uploading.
Is there a reason why this is happening?
may be you have:
group :assets do
gem 'asset_sync'
end
you should put that gem out from that group
I know this issue is old... but I just inherited a Heroku app and am reading how to asset_sync with Heroku and came across this, which states ENV variables are not available when compiling assets on Heroku.

asset_sync gem error: rake assets:precompile fails : does not match the server certificate (OpenSSL::SSL::SSLError)

asset_sync gem error :(
The first error I started out with was related to the unf gem, so I added gem "unf", "~> 0.1.3" to my gemfile and tried things again. No luck :( After more research I may have an issue with another setting related to my aws bucket. My dns is hosted via route53 and I wanted to use a custom domain to serve my assets. This means my bucket will look like assets.domain.com instead of just a plain name. when I attempt to run rake assets:precompile it aborts with the error rake aborted!
hostname "assets.domain.com.s3-us-west-1.amazonaws.com" does not match the server certificate (OpenSSL::SSL::SSLError)" however that error appears to be wrong. In order to use your own sub domain i.e. assets. I read that the bucket must be setup as a static website. This means the url looks like assets.domain.com.s3-website-us-west-1.amazonaws.com which does not seem to match the error code.
am I missing a setting here? maybe I am crazy... thank you for your help.
my production.rb settings
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
AssetSync.config.run_on_precompile = true
config.action_controller.asset_host = "http://assets.domain.com"
config.assets.prefix = "/data"
config.assets.enabled = true
config.assets.compile = true
config.assets.initialize_on_precompile = true
# Generate digests for assets URLs.
config.assets.digest = true
my current initializer file:
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = ENV['MY_S3_ID']
config.aws_secret_access_key = ENV['MY_S3_SECRET']
# To use AWS reduced redundancy storage.
# config.aws_reduced_redundancy = true
config.fog_directory = ENV['MY_S3_BUCKET']
# Invalidate a file on a cdn after uploading files
# config.cdn_distribution_id = "12345"
# config.invalidate = ['file1.js']
# Increase upload performance by configuring your region
config.fog_region = ENV['MY_S3_ENDPOINT']
#
# Don't delete files from the store
# config.existing_remote_files = "keep"
#
# Automatically replace files with their equivalent gzip compressed version
# config.gzip_compression = true
#
# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
# config.manifest = true
#
# Fail silently. Useful for environments such as Heroku
# config.fail_silently = true
end
end
Fixed by adding: Fog.credentials = { path_style: true }
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = ENV['MY_S3_ID']
config.aws_secret_access_key = ENV['MY_S3_SECRET']
# To use AWS reduced redundancy storage.
# config.aws_reduced_redundancy = true
config.fog_directory = ENV['MY_S3_BUCKET']
# Invalidate a file on a cdn after uploading files
# config.cdn_distribution_id = "12345"
# config.invalidate = ['file1.js']
Fog.credentials = { path_style: true }
# Increase upload performance by configuring your region
config.fog_region = ENV['MY_S3_ENDPOINT']
#
# Don't delete files from the store
# config.existing_remote_files = "keep"
#
# Automatically replace files with their equivalent gzip compressed version
# config.gzip_compression = true
#
# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
# config.manifest = true
#
# Fail silently. Useful for environments such as Heroku
# config.fail_silently = true
end
end
Reference issues: https://github.com/rumblelabs/asset_sync/issues/236, https://github.com/fog/fog/issues/2357

Resources