2017-07-03 70 views
0

我的教程在這裏以下內容:http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpRuby.html 瞭解使用Ruby上傳文件S3,我寫了這個:得到AWS ::錯誤:: MissingRegionError同時使用Ruby來上傳文件到S3

require 'aws-sdk' 

s3 = Aws::S3::Client.new(
    access_key_id: '.......', 
    secret_access_key: '........' 
) 

s3 = Aws::S3::Resource.new(region:'us-east-1') 
obj = s3.bucket('aws-adms-whitelisting-rules').object('test') 
obj.upload_file('~/Desktop/ruby/test.rb') 

但得到以下錯誤:

/Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.10.7/lib/aws-sdk-core/plugins/regional_endpoint.rb:34:in `after_initialize': missing region; use :region option or export region name to ENV['AWS_REGION'] (Aws::Errors::MissingRegionError) 
    from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.10.7/lib/seahorse/client/base.rb:84:in `block in after_initialize' 
    from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.10.7/lib/seahorse/client/base.rb:83:in `each' 
    from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.10.7/lib/seahorse/client/base.rb:83:in `after_initialize' 
    from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.10.7/lib/seahorse/client/base.rb:21:in `initialize' 
    from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-core-2.10.7/lib/seahorse/client/base.rb:105:in `new' 
    from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-resources-2.10.7/lib/aws-sdk-resources/resource.rb:169:in `extract_client' 
    from /Library/Ruby/Gems/2.0.0/gems/aws-sdk-resources-2.10.7/lib/aws-sdk-resources/resource.rb:15:in `initialize' 
    from test.rb:35:in `new' 
    from test.rb:35:in `<main>' 

你能告訴我一個前進的道路嗎?謝謝

回答

0

更爲標準的方法是將區域作爲參數傳遞給您創建的Aws::S3::Client實例。

然後,您可以將Aws::S3::Clientdocs)的實例傳遞給您正在創建的Aws::S3::Resourcedocs)。

例如:

require 'aws-sdk' 

s3 = Aws::S3::Client.new(
    region: 'us-east-1', 
    access_key_id: '.......', 
    secret_access_key: '........' 
) 

resource = Aws::S3::Resource.new(client: s3) 
obj = resource.bucket('aws-adms-whitelisting-rules').object('test') 
obj.upload_file('~/Desktop/ruby/test.rb') 
+0

感謝的答案,但我得到了同樣的錯誤 – user8142520

+0

我相信'AWS :: S3 :: Client'對象必須被傳遞作爲關鍵字參數傳遞給' Aws :: S3 :: Resource「,因爲'#initialize'只接受選項散列。 – dlachasse

+0

@dlachasse,抓住你是對的。我編輯了我的答案。嘗試用哈希初始化資源,讓我知道如果這有效。 – treiff