1

在 S3 存储桶上使用 AWS 开发工具包并使用 Rspec 进行测试时,我经常遇到这个错误,即使我做了很多更改。

这是代码。

流.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

输出:TestZip.zip

4

1 回答 1

1

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

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

于 2016-10-07T06:03:40.330 回答