Reputation: 5462
My Rails 3.2.11 app can't seem to find the AmazonS3 class below in production. I can call it in a rails console with something like AmazonS3.create(), but for some reason when I call it in PdfGen.create_pdf (which in turn is called from a Rails controller during a request), I get the bellow NameError.
Error I keep getting
Job PdfGen.create_pdf (id=XX) FAILED (5 prior attempts) with NameError: uninitialized constant AmazonS3::AWS
config/application.rb
config.autoload_paths += %W(#{config.root}/lib})
config.autoload_paths += Dir["#{config.root}/lib/**/"] # include all subdirectories
Gemfile.lock
aws-sdk (1.60.1)
aws-sdk-v1 (= 1.60.1)
aws-sdk-v1 (1.60.1)
lib/amazon_s3.rb
class AmazonS3
def self.signed_url(s3_key)
s3 = AWS::S3.new(:access_key_id => S3Config.access_key_id, :secret_access_key => S3Config.secret_access_key)
bucket = s3.buckets[S3Config.bucket]
bucket.objects[s3_key].url_for(:read, :expires => EXPIRES_IN_YEAR_2036)
end
end
lib/pdf_gen.rb
class PdfGen
def self.create_pdf
##some code to generate a pdf
AmazonS3.create(amazon_s3_key, response.body) #store on S3 bucket
end
end
Upvotes: 0
Views: 36
Reputation: 5462
As it turns out, this was an issue with the gem itself. I added:
gem 'aws-sdk-v1'
to the Gemfile instead of aws-sdk and it worked.
Upvotes: 1