Upload a file using the Ruby SDK on Amazon S3

I am trying to upload a file. Simple hello.txt. I kept track of the documents, and I cannot load it into 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 

I followed http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpRuby.html

Error:

➜ 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> '

+5
source share
3 answers
 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 
+2
source

The main problem is that you have version 2 of the AWS SDK for Ruby installed, but you are referring to the documentation for version 1 . Version 2 documentation can be found here:

http://docs.aws.amazon.com/sdkforruby/api/index.html

To update your example to use version 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 

The main differences:

  • Version 1 used the # [] method in the collection to refer to an object by its key. Version 2 has two methods: #objects() and #object(key) . The last is getter. The first lists all the objects in the bucket.
  • Version 2 has a specialized method #upload_file , which controls the loading of an object from disk. This is similar to #write from version 1, but can also use multiple threads to load large objects in parallel.
+7
source

I used a script, as shown below, that would create a new bucket if it does not exist, and then load the selected file into it.

 #!/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) ) 

if you saved this for upload.rb, you can upload simple.txt to my_bucket by running

$ ruby upload.rb my_bucket simple.txt

+1
source

All Articles