まっすぶっちの美忘録

おうちでプログラミングと読書してます

rubyでS3を扱う `unescape': uninitialized class variable @@accept_charset in #<Class:CGI> (NameError) 

コード

require 'aws-sdk'
require 'cgi/util'

Aws.config.update(
  access_key_id: ACCESS_KEY
  secret_access_key: SECRET_ACCESS_KEY
  region: 'ap-northeast-1'
)
bucket = 'bucket-name'

s3 = Aws::S3::Client.new
s3_bucket = Aws::S3::Bucket.new(bucket)

resp = s3.list_buckets
puts resp.buckets.map(&:name)

エラー

/Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_url_encoded_keys.rb:75:in `unescape': uninitialized class variable @@accept_charset in #<Class:CGI> (NameError)
Did you mean?  @@accept_charset
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_url_encoded_keys.rb:75:in `decode'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_url_encoded_keys.rb:46:in `decode_list_objects_keys'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_url_encoded_keys.rb:41:in `block in manage_keys'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/response.rb:45:in `block in on'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/http/response.rb:139:in `block in on_success'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/http/response.rb:166:in `block in listener'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/http/response.rb:130:in `on_done'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/http/response.rb:137:in `on_success'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/response.rb:44:in `on'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/response.rb:53:in `on_success'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_url_encoded_keys.rb:40:in `manage_keys'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_url_encoded_keys.rb:32:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/param_validator.rb:21:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/plugins/raise_response_errors.rb:14:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_sse_cpk.rb:19:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_dualstack.rb:24:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/s3_accelerate.rb:34:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/jsonvalue_converter.rb:20:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/idempotency_token.rb:18:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/param_converter.rb:20:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/aws-sdk-core/plugins/response_paging.rb:26:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/plugins/response_target.rb:21:in `call'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/request.rb:70:in `send_request'
    from /Users/masubuchiyoshiki/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/aws-sdk-core-2.9.11/lib/seahorse/client/base.rb:207:in `block (2 levels) in define_operation_methods'

原因

aws-sdkのバーションによるもの

注目点は aws-sdk-core-2.9.11

本当によくわからなかったので、gemのソースを見に行ったら、バージョンが3にupdateされていた。

gem update aws-sdk

をやったらとおた。