使用Ruby SDK将文件上载到Amazon S3

nlejzf6q  于 2023-01-25  发布在  Ruby
关注(0)|答案(4)|浏览(202)

我正在尝试上传一个文件。一个简单的helloidertxt。我是按照文档,我无法上传到我的桶。

#  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
错误:
rb/用户/. rvm/gems/ruby-2.1.1/gems/aws-sdk-resources-2.0.34/库/aws-sdk-resources/集合。rb:79:in '方法缺失:未定义"<main>中的main.rb:18"的"<Aws::Resources::Collection:0x000001031e5100>(NoMethodError)"的方法[]"

rjee0c15

rjee0c151#

主要问题是您安装了AWS SDK for Ruby的第2版,但您引用的是第1版的文档。第2版文档可在以下位置找到:

http://docs.aws.amazon.com/sdkforruby/api/index.html
要更新示例以使用版本2:

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

begin

  bucket.object(KEY).upload_file(FILENAME)
  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

主要差异:

  • 版本1使用集合上的#[]方法通过键引用对象。版本2有两个方法,#objects()#object(key)。后者是getter。前者枚举桶中的所有对象。
  • 版本2有一个专门的#upload_file方法来管理从磁盘上载对象,这与版本1中的#write类似,但它也可以使用多个线程来并行上载大的对象部分。
cwdobuhd

cwdobuhd2#

我使用了一个类似下面的脚本,如果它不存在,它将创建一个新的bucket,然后将所选的文件上传到它。

#!/usr/bin/env ruby
#

require 'rubygems'
require 'aws-sdk'

bucket_name = ARGV[0]
file_name = ARGV[1]

# Get an instance of the S3 interface.
s3 = Aws::S3::Client.new(region: 'us-east-1')

key = File.basename(file_name)
resp = s3.list_buckets()
buckets = resp.data.buckets

if buckets.select { |b| b.name == bucket_name }.length == 0
  puts 'creating bucket'
  s3.create_bucket(bucket: bucket_name)
end

puts "Uploading file #{file_name} to bucket #{bucket_name}..."

# Upload a file.
s3.put_object(
  :bucket => bucket_name,
  :key    => key,
  :body   => IO.read(file_name)
)

如果你保存到upload.rb你可以上传simple.txt到my_bucket运行
$ ruby upload.rb my_bucket simple.txt

bxjv4tth

bxjv4tth3#

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
zhte4eai

zhte4eai4#

简短回答:您应该添加一个方法来检查存储桶是否已经存在。通过这样做,您将遵循最佳实践,并将失败的风险降到最低。
代码如下所示:

client = Aws::S3::Client.new(http_wire_trace: true) // Note: Enable wire_trace only in debug mode.
    
create_bucket_if_missing(client, bucket_name)

client.put_object(
     :bucket => bucket_name,
     :key    => key,
     :body   => File.read(file_path)
   )

def create_bucket_if_missing(client, bucket_name)
  begin
    client.head_bucket(bucket: bucket_name)
    return
  rescue StandardError
    puts "Failed to find bucket #{bucket_name}, creating it"
    client.create_bucket({bucket: bucket_name})
  end
end

相关问题