Upload an object using the AWS SDK for Ruby - Amazon Simple Storage Service

Upload an object using the AWS SDK for Ruby

The AWS SDK for Ruby - Version 3 has two ways of uploading an object to Amazon S3. The first uses a managed file uploader, which makes it easy to upload files of any size from disk. To use the managed file uploader method:

  1. Create an instance of the Aws::S3::Resource class.

  2. Reference the target object by bucket name and key. Objects live in a bucket and have unique keys that identify each object.

  3. Call#upload_file on the object.

require 'aws-sdk-s3' # Uploads an object to a bucket in Amazon Simple Storage Service (Amazon S3). # # Prerequisites: # # - An S3 bucket. # - An object to upload to the bucket. # # @param s3_client [Aws::S3::Resource] An initialized S3 resource. # @param bucket_name [String] The name of the bucket. # @param object_key [String] The name of the object. # @param file_path [String] The path and file name of the object to upload. # @return [Boolean] true if the object was uploaded; otherwise, false. # @example # exit 1 unless object_uploaded?( # Aws::S3::Resource.new(region: 'us-east-1'), # 'doc-example-bucket', # 'my-file.txt', # './my-file.txt' # ) def object_uploaded?(s3_resource, bucket_name, object_key, file_path) object = s3_resource.bucket(bucket_name).object(object_key) object.upload_file(file_path) return true rescue StandardError => e puts "Error uploading object: #{e.message}" return false end

The second way that AWS SDK for Ruby - Version 3 can upload an object uses the #put method of Aws::S3::Object. This is useful if the object is a string or an I/O object that is not a file on disk. To use this method:

  1. Create an instance of the Aws::S3::Resource class.

  2. Reference the target object by bucket name and key.

  3. Call#put, passing in the string or I/O object.

require 'aws-sdk-s3' # Uploads an object to a bucket in Amazon Simple Storage Service (Amazon S3). # # Prerequisites: # # - An S3 bucket. # - An object to upload to the bucket. # # @param s3_client [Aws::S3::Resource] An initialized S3 resource. # @param bucket_name [String] The name of the bucket. # @param object_key [String] The name of the object. # @param file_path [String] The path and file name of the object to upload. # @return [Boolean] true if the object was uploaded; otherwise, false. # @example # exit 1 unless object_uploaded?( # Aws::S3::Resource.new(region: 'us-east-1'), # 'doc-example-bucket', # 'my-file.txt', # './my-file.txt' # ) def object_uploaded?(s3_resource, bucket_name, object_key, file_path) object = s3_resource.bucket(bucket_name).object(object_key) File.open(file_path, 'rb') do |file| object.put(body: file) end return true rescue StandardError => e puts "Error uploading object: #{e.message}" return false end