使用Ruby SDK将文件上传到Amazon S3 [英] Uploading a file using the Ruby SDK to Amazon S3

查看:91
本文介绍了使用Ruby SDK将文件上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传文件.一个简单的hello.txt.我正在关注文档,但无法将其上传到我的存储桶中.

I am trying to upload a file. A simple hello.txt. I was following the docs, and I am unable to upload it to my bucket.

#  START AWS CLIENT

s3 = Aws::S3::Resource.new
bucket = s3.bucket(BUCKET_NAME)

begin

  s3.buckets[BUCKET_NAME].objects[KEY].write(:file => FILE_NAME)
  puts "Uploading file #{FILE_NAME} to bucket #{BUCKET_NAME}."

  bucket.objects.each do |obj|
    puts "#{obj.key} => #{obj.etag}"
  end

rescue Aws::S3::Errors::ServiceError
  # rescues all errors returned by Amazon Simple Storage Service
end

我正在关注 http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpRuby.html

错误:

➜s3-tester ruby​​ main.rb/Users/.rvm/gems/ruby-2.1.1/gems/aws-sdk-resources-2.0.34/lib/aws-sdk-resources/collection.rb:79:in'method_missing:未定义的方法[]',用于'< Aws :: Resources :: Collection:0x000001031e5100> (NoMethodError)'来自'main.rb:18:in < main> '

➜ s3-tester ruby main.rb /Users/.rvm/gems/ruby-2.1.1/gems/aws-sdk-resources-2.0.34/lib/aws-sdk-resources/collection.rb:79:in 'method_missing: undefined method []' for '<Aws::Resources::Collection:0x000001031e5100> (NoMethodError)' from 'main.rb:18:in <main>'

推荐答案

client = Aws::S3::Client.new(region: 'us-east-1')
resource = Aws::S3::Resource.new(client: client)
bucket = resource.bucket(BUCKET_NAME)
begin
  # s3.buckets[BUCKET_NAME].objects[KEY].write(:file => FILE_NAME)
  # puts "Uploading file #{FILE_NAME} to bucket #{BUCKET_NAME}."

  bucket.objects.each do |o|
    puts o.key
  end

rescue Aws::S3::Errors::ServiceError
  # rescues all errors returned by Amazon Simple Storage Service
end

这篇关于使用Ruby SDK将文件上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆