ruby/example_code/s3/s3-ruby-example-upload-item-to-folder.rb
# Uploads a file to a folder within a bucket in Amazon S3.
#
# @param s3_client [Aws::S3::Client] An initialized Amazon S3 client.
# @param bucket_name [String] The name of the bucket.
# @param folder_name [String] The name of the folder.
# @param file_name [String] The name of the file.
# @return [Boolean] true if the file was uploaded; otherwise, false.
# @example
#   s3_client = Aws::S3::Client.new(region: 'us-west-2')
#   exit 1 unless upload_file_to_folder?(
#     s3_client,
#     'doc-example-bucket',
#     'my-folder/',
#     'my-file.txt')
Uploads a file to a folder within a bucket in Amazon S3.
Uploads a file to a folder within a bucket in Amazon S3 .
def upload_file_to_folder?(s3_client, bucket_name, folder_name, file_name)
  s3_client.put_object(
    body: file_name,
    bucket: bucket_name,
    key: folder_name + file_name
  )
  return true
rescue StandardError => e
  puts "Error while uploading the file '#{file_name}' to the " \
    "folder '#{folder_name}' in the bucket '#{bucket_name}': #{e.message}"
end
def upload_file_to_folder? ( s3_client , bucket_name , folder_name , file_name ) s3_client . put_object ( body : file_name , bucket : bucket_name , key : folder_name + file_name ) return true rescue StandardError => e puts "Error while uploading the file '#{file_name}' to the " "folder '#{folder_name}' in the bucket '#{bucket_name}': #{e.message}" end
Uploads a file to a folder within a bucket in Amazon S3.
Uploads a file to a folder within a bucket in Amazon S3 .
true if the file was uploaded; otherwise, false.
true if the file was uploaded ; otherwise false .
Boolean
s3_client
An initialized Amazon S3 client.
An initialized Amazon S3 client .
bucket_name
The name of the bucket.
The name of the bucket .
folder_name
The name of the folder.
The name of the folder .
file_name
The name of the file.
The name of the file .
example
s3_client = Aws::S3::Client.new(region: 'us-west-2') exit 1 unless upload_file_to_folder?( s3_client, 'doc-example-bucket', 'my-folder/', 'my-file.txt')
s3_client = Aws :: S3 :: Client . new ( region : ' us - west - 2 ' ) exit 1 unless upload_file_to_folder? ( s3_client ' doc - example - bucket ' ' my - folder / ' ' my - file . txt ' )