我最近将railst从4.2升级到5.2,还升级了aws-sdk,但出现错误:
DEPRECATION警告:称为Aws :: Resources :: Collection的不推荐使用的方法[[]“
RuntimeError:无法索引到延迟加载的集合中
与此通话:
s3 = Aws::S3::Resource.new(
:access_key_id => ENV.fetch('AWS_ACCESS_KEY_ID'),
:secret_access_key => ENV.fetch('AWS_SECRET_ACCESS_KEY'),
:region => ENV.fetch('AWS_REGION')
)
bucket = s3.bucket(ENV.fetch("AWS_BUCKET"))
obj = bucket.objects[s3_key] # --> I GET THE ERROR HERE
file = Tempfile.new('file_json_file')
file.binmode
obj.read do |chunk|
file.write(chunk)
end
file.close
file.open
interpret_json_file(file)
我应该如何更新才能使其正常工作?
错误跟踪:
DEPRECATION WARNING: called deprecated method `[]' of an Aws::Resources::Collection
9:09:22 PM worker.1 | /Users/Mari/online-reporting/app/models/report_run.rb:15:in `parsed_contents_of_data_file'
9:09:22 PM worker.1 | /Users/Mari/online-reporting/app/jobs/generate_reports_job.rb:3:in `perform'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/backend/base.rb:81:in `block in invoke_job'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:61:in `block in initialize'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:66:in `execute'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:40:in `run_callbacks'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/backend/base.rb:78:in `invoke_job'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:230:in `block (2 levels) in run'
9:09:22 PM worker.1 | /usr/local/rvm/rubies/ruby-2.5.3/lib/ruby/2.5.0/timeout.rb:93:in `block in timeout'
9:09:22 PM worker.1 | /usr/local/rvm/rubies/ruby-2.5.3/lib/ruby/2.5.0/timeout.rb:103:in `timeout'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:230:in `block in run'
9:09:22 PM worker.1 | /usr/local/rvm/rubies/ruby-2.5.3/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:229:in `run'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:312:in `block in reserve_and_run_one_job'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:61:in `block in initialize'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:66:in `execute'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:40:in `run_callbacks'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:312:in `reserve_and_run_one_job'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:213:in `block in work_off'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:212:in `times'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:212:in `work_off'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:175:in `block (4 levels) in start'
9:09:22 PM worker.1 | /usr/local/rvm/rubies/ruby-2.5.3/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:174:in `block (3 levels) in start'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:61:in `block in initialize'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:66:in `execute'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:40:in `run_callbacks'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:173:in `block (2 levels) in start'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:172:in `loop'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:172:in `block in start'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/plugins/clear_locks.rb:7:in `block (2 levels) in <class:ClearLocks>'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:79:in `block (2 levels) in add'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:61:in `block in initialize'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:79:in `block in add'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:66:in `execute'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/lifecycle.rb:40:in `run_callbacks'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/worker.rb:171:in `start'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/delayed_job-4.1.5/lib/delayed/tasks.rb:9:in `block (2 levels) in <top (required)>'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/task.rb:271:in `block in execute'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/task.rb:271:in `each'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/task.rb:271:in `execute'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/bugsnag-6.8.0/lib/bugsnag/integrations/rake.rb:18:in `execute_with_bugsnag'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
9:09:22 PM worker.1 | /usr/local/rvm/rubies/ruby-2.5.3/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/task.rb:182:in `invoke'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:160:in `invoke_task'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:116:in `each'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:116:in `block in top_level'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:125:in `run_with_threads'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:110:in `top_level'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:83:in `block in run'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:186:in `standard_exception_handling'
9:09:22 PM worker.1 | /usr/local/rvm/gems/ruby-2.5.3/gems/rake-12.3.1/lib/rake/application.rb:80:in `run'
9:09:22 PM worker.1 | bin/rake:4:in `<main>'
答案 0 :(得分:0)
进行以下更改并使其起作用:
s3 = Aws::S3::Resource.new(:access_key_id => ENV.fetch('AWS_ACCESS_KEY_ID'),
:secret_access_key => ENV.fetch('AWS_SECRET_ACCESS_KEY'),
:region => ENV.fetch('AWS_REGION'))
obj = s3.client.get_object(bucket:ENV.fetch("AWS_BUCKET"), key: s3_key.to_s)
file = Tempfile.new('report_generation_json_file')
file.binmode
file.write(obj.body.read)
file.close
file.open
interpret_json_file(file)