NoSuchKey error for an existing object - ruby-on-rails

I have my S3 account set up in Rails 3 like so:
AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com"
#Set up AWS S3
AWS::S3::Base.establish_connection!(
:access_key_id => 'itsa',
:secret_access_key => 'secret'
)
BUCKET='wetten'
But for some reason it says that my files do not exist. I have given 'Everyone' Open/Download permissions on the files in S3.
In the Rails console, for this file:
>> AWS::S3::S3Object.exists?('/xml/BWBR0001821.xml', 'wetten')
true
>> AWS::S3::S3Object.find('/xml/BWBR0001821.xml', 'wetten')
AWS::S3::NoSuchKey: No such key `/xml/BWBR0001821.xml'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/aws-s3-0.6.3/lib/aws/s3/object.rb:177:in `find'
from (irb):11
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from C:/Users/Maarten/RubymineProjects/wetten-api/script/rails:6:in `require'
from C:/Users/Maarten/RubymineProjects/wetten-api/script/rails:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
What's wrong?
UPDATE: I noticed that in object.rb, the AWS S3 gem (v0.6.3) does the following:
bucket = Bucket.find(bucket_name(bucket), :marker => key.previous, :max_keys => 1)
where key is '/xml/BWBR0001821.xml', and key.previous is '/xml/BWBR0001821.xmk'... What the hell? This seems like a bug to me.

Found the error. Apparently, the find method gets confused when by the first forward slash. Using xml/BWBR0001821.xml as a file name works:
>> AWS::S3::S3Object.find('xml/BWBR0001821.xml', 'wetten')
#<AWS::S3::S3Object:0x49387284 '/wetten/xml/BWBR0001821.xml'>

Related

How to add iframe to sanitized tags in ruby on rails 4.2.6?

I am trying to add iframe to santized tags by using
config.action_view.sanitized_allowed_tags
I tried to find what tags are already allowed by using the console.
uraai#raiuorial:~/workspace/corse (master) $ heroku run rails c
Running rails c on ⬢ fa4... up, run.9396
Loading production environment (Rails 4.2.6)
irb(main):001:0> puts helper.sanitized_allowed_tags.to_a
NoMethodError: undefined method `sanitized_allowed_tags' for #<ActionView::Base:0x007f18ea91ea60>
from /app/vendor/bundle/ruby/2.3.0/gems/metamagic-3.1.7/lib/metamagic/view_helper.rb:30:in `method_missing'
from (irb):1
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
from /app/bin/rails:8:in `require'
from /app/bin/rails:8:in `<main>'
irb(main):002:0>
Any idea how to add it without ignoring the other tags? Thanks
Please have a look on the next example:
module Tapp
class Application < Rails::Application
# In config/application.rb
config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a', 'br', 'iframe']
# ...
end
My app called Tapp, I'm pretty sure you will have another name here ;)
Then in the console:
[retgoat#iMac-Roman ~/workspace/tapp]$ rc
Loading development environment (Rails 4.2.6)
[1] pry(main)> Tapp::Application.config.action_view[:sanitized_allowed_tags]
=> ["strong", "em", "a", "br", "iframe"]

Getters/settings not working when using the rails-setting gem - method missing error

I'm using the rails-settings gem: https://github.com/huacnlee/rails-settings-cached
The getter/setter method are not working.
I've added the following line to my user model as directed:
include RailsSettings::Extend
I've also tried adding it to role.rb (as I am using rolify gem)
However is am still getting the following error message:
NoMethodError: undefined method `settings' for #<User:0x007f8dd017a560>
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/activerecord-3.2.13/lib/active_record/attribute_methods.rb:149:in `method_missing'
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/rolify-3.2.0/lib/rolify/role.rb:73:in `method_missing'
from (irb):24
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
When I run:
user = User.find(2)
user.settings.color = :red
Can anyone see what the issue is?
Ok, I've solved the issue.
I had not restarted the console session.....restarting it got it working!

Getting AWS S3 to work with rails

So, I'm trying to get S3 working with Paperclip. I'm in the process of setting up Aws with rails. So far I've installed the 'aws/s3' gem and setup my access_keys on AWS.
I then added the keys to a s3.yml file:
# config/s3.yml
access_key_id: ******************
secret_access_key: ******************
Next I ran:
2.0.0p195 :015 > require 'aws/s3'
=> false
2.0.0p195 :016 > AWS::S3::Base.establish_connection!(YAML.load_file('config/s3.yml').symbolize_keys!)
=> #<AWS::S3::Connection:0x007feb03ab2bd8 #options={:server=>"s3.amazonaws.com", :port=>80, :access_key_id=>"******************", :secret_access_key=>"******************"}, #access_key_id="*****************", #secret_access_key="******************", #http=#<Net::HTTP s3.amazonaws.com:80 open=false>>
2.0.0p195 :017 > AWS::S3::Bucket.create('****')
=> AWS::S3::BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it.
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/aws-s3-0.6.3/lib/aws/s3/error.rb:38:in `raise'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/aws-s3-0.6.3/lib/aws/s3/base.rb:72:in `request'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/aws-s3-0.6.3/lib/aws/s3/base.rb:88:in `put'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/aws-s3-0.6.3/lib/aws/s3/bucket.rb:79:in `create'
from (irb):17
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0p195 :018 >
I can't tell if it's working, or what to do next. I am using this as a guideline but my results look different. What am I doing wrong?
It seems your bucket has already created, you can try create one with different name

Rails CarrierWave store image from url gives Can't convert nil into string

Using CarrierWave and Amazon S3, I'm able to store images from local files, but when I try to store them from certain url, I get 'TypeError: can't convert nil into String'
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:149:in `join'
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:149:in `cache_path'
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:121:in `block in cache!'
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:112:in `cache!'
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/store.rb:56:in `store!'
from (irb):5
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /Users/bashar/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
This is the code I'm trying
require 'open-uri'
image = "open(image_url)
#object.image.store!(file)
obviously #object holds the Uploader image and it's been working fine using local file systems. I tried to add a
def root
Rails.root.join 'public/'
end
to my uploader as well with no luck.
Any idea?
To tell carrier wave to download a remote url, you use this syntax:
#object.remote_image_url = "http://www.example.com/file.png"
#object.save

Rails, Omniauth, Authlogic, and Facebook - first step

I'm trying to get Facebook logins working on my app using Rails, Authlogic, and Omniauth. I'm following the first steps of http://railscasts.com/episodes/235-omniauth-part-1 as precisely as possible.
I have:
added the omniauth gem, run bundle install.
created a new Authentication nifty-scaffold with fields user_id, provider, and uid.
added the route: match '/auth/:provider/callback' => 'authentications#create'
added to config/initializers/omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, '20...my_app_id', 'bc...my_app_secret'
end
The server then fails to restart:
/Users/san/.rvm/gems/ruby-1.9.2-p0/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:
in 'const_missing_from_s3_library': uninitialized constant
OmniAuth::Strategies::Facebook (NameError)
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in 'const_missing'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:22:in 'const_get'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:22:in 'provider'
from /Users/san/Documents/sanj/myapp/config/initializers/omniauth.rb:2:in 'block in <top (required)>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'instance_eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'initialize'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/omniauth-1.0.0/lib/omniauth/builder.rb:7:in 'initialize'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:33:in 'new'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:33:in 'build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'block in build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'each'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'inject'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.10/lib/action_dispatch/middleware/stack.rb:79:in 'build'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:162:in 'app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application/finisher.rb:35:in 'block in <module:Finisher>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:25:in 'instance_exec'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:25:in 'run'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:50:in 'block in run_initializers'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:49:in 'each'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/initializable.rb:49:in 'run_initializers'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:134:in 'initialize!'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/application.rb:77:in 'method_missing'
from /Users/san/Documents/sanj/myapp/config/environment.rb:5:in '<top (required)>'
from /Users/san/Documents/sanj/myapp/config.ru:3:in 'block in <main>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'instance_eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:46:in 'initialize'
from /Users/san/Documents/sanj/myapp/config.ru:1:in 'new'
from /Users/san/Documents/sanj/myapp/config.ru:1:in '<main>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:35:in 'eval'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/builder.rb:35:in 'parse_file'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:162:in 'app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:253:in 'wrapped_app'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/rack-1.2.3/lib/rack/server.rb:204:in 'start'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands/server.rb:65:in 'start'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:30:in 'block in <top (required)>'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:27:in 'tap'
from /Users/san/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.10/lib/rails/commands.rb:27:in '<top (required)>'
from script/rails:7:in 'require'
from script/rails:7:in '<main>'
I had to do 'require "omniauth"' or 'require "omniauth-facebook"' in my config/development.rb file.
The omniauth gem was just bumped to version 1.0 as of yesterday (http://intridea.com/2011/11/2/omniauth-1-0), and there are some changes to how the gem is used that are not covered in the railscast.
I found my solution on this page: https://github.com/intridea/omniauth/wiki/OmniAuth-1.0
The biggest different is that each strategy is now contained within its own gem, so for facebook, you would change the line in your gem file that currently reads "gem 'omniauth'" to "gem 'omniauth-facebook'".
I didn't have to change any other code, and you may have to run "bundle update" to get everything to click through.
I just ran into this problem and this is what was wrong with me:
You can't have your facebook oauth gem in the Development list in your gem file. Heroku only reads the Production ones, so move it up out of the Development list.

Resources