AWS SDK : NoMethodError: undefined method `put_object' in Rspec Testing

AWS SDK : NoMethodError: undefined method `put_object' in Rspec Testing

在 S3 存储桶上使用 AWS SDK 并使用 Rspec 进行测试,尽管我做了很多更改,但我经常遇到这个错误。

这是代码。

Flow.rb

require 'S3Ops.rb'
require 'aws-sdk'

def putzip(s3,bucket,instance)
  y=File.size('TestZip.zip')
  puts "File size of the test zip is #{ y.to_s}"
  File.open('TestZip.zip','rb') do |file|
    s3.put_object(bucket: bucket, key: instance+'/Test.zip', body: file)
  end
  result=@s3_bucket.list_objects({bucket: @bucket_name})
  z = result.contents[0].size
  puts 'File size of Uploaded file is ' + z.to_s
end

describe 'Test' do
  before(:all) do
    bucket_name = 'testbucket'
    instance_name = 'testinstance'
    s3 = S3Ops.new
    putzip(s3, bucket_name, instance_name)
  end

    **example tests**
end

S3Ops.rb

require 'aws-sdk'

class S3Ops
  def initialize
    @s3 = Aws::S3::Client.new(region: 'ap-southeast-1')
  end

**other functions**
end

错误

Failure/Error: s3.put_object(bucket: bucket, key: instance + '/Test.zip', body: file)    
NoMethodError:
   undefined method `put_object' for #<S3Ops:0x000000020707e0>

我什至尝试过全球化所有变量以增加范围并像这样在函数内部重新初始化 s3 操作。

require 'S3Ops.rb'
require 'aws-sdk'

def putzip(s3,bucket,instance)
  y=File.size('TestZip.zip')
  puts "File size of the test zip is #{ y.to_s}"
  s3 = S3Ops.new
  File.open('TestZip.zip','rb') do |file|
    s3.put_object(bucket: bucket, key: instance+'/Test.zip', body: file)
  end
  result=@s3_bucket.list_objects({bucket: @bucket_name})
  z = result.contents[0].size
  puts 'File size of Uploaded file is ' + z.to_s
end

describe 'Test' do
  before(:all) do
    @bucket_name = 'testbucket'
    @instance_name = 'testinstance'
    @s3 = S3Ops.new
    putzip(@s3, @bucket_name, @instance_name)
  end

    **example tests**
end

仍然显示相同的错误。纠正错误需要进行哪些更改?

编辑

它在示例测试中运行良好,如下所示

it 'checks for zip' do
  result = @s3.list_objects(bucket: bucket)
  puts result.contents[0].key
end

Output: TestZip.zip

我启动 S3Ops 的方式是错误的,没有 return 语句将指针返回给 Flow。因此 S3 连接失败。

我纠正了它,现在可以正常工作了。