ruby/example_code/s3/upload_object_presigned_url.rb
def object_uploaded_to_presigned_url?(
  s3_resource,
  bucket_name,
  object_key,
  object_content,
  http_client = nil
)
  object = s3_resource.bucket(bucket_name).object(object_key)
  url = URI.parse(object.presigned_url(:put))

  if http_client.nil?
    Net::HTTP.start(url.host) do |http|
      http.send_request(
        'PUT',
        url.request_uri,
        object_content,
        'content-type' => ''
      )
    end
  else
    http_client.start(url.host) do |http|
      http.send_request(
        'PUT',
        url.request_uri,
        object_content,
        'content-type' => ''
      )
    end
  end
  content = object.get.body
  puts "The presigned URL for the object '#{object_key}' in the bucket " \
    "'#{bucket_name}' is:\n\n"
  puts url
  puts "\nUsing this presigned URL to get the content that " \
    "was just uploaded to this object, the object\'s content is:\n\n"
  puts content.read
  return true
rescue StandardError => e
  puts "Error uploading to presigned URL: #{e.message}"
  return false
end
# Uploads an object to a bucket in Amazon Simple Storage Service (Amazon S3)
#   by using a presigned URL.
#
# Prerequisites:
#
# - An S3 bucket.
# - An object in the bucket to upload content to.
#
# @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 object_content [String] The content to upload to the object.
# @param http_client [Net::HTTP] An initialized HTTP client.
#   This is especially useful for testing with mock HTTP clients.
#   If not specified, a default HTTP client is created.
# @return [Boolean] true if the object was uploaded; otherwise, false.
# @example
#   exit 1 unless object_uploaded_to_presigned_url?(
#     Aws::S3::Resource.new(region: 'us-west-2'),
#     'doc-example-bucket',
#     'my-file.txt',
#     'This is the content of my-file.txt'
#   )
Uploads an object to a bucket in Amazon Simple Storage Service (Amazon S3)
by using a presigned URL.



An S3 bucket.
An object in the bucket to upload content to.
Uploads an object to a bucket in Amazon Simple Storage Service ( Amazon S3 ) by using a presigned URL . An S3 bucket . An object in the bucket to upload content to .
def object_uploaded_to_presigned_url?(
  s3_resource,
  bucket_name,
  object_key,
  object_content,
  http_client = nil
)
  object = s3_resource.bucket(bucket_name).object(object_key)
  url = URI.parse(object.presigned_url(:put))
  if http_client.nil?
    Net::HTTP.start(url.host) do |http|
      http.send_request(
        'PUT',
        url.request_uri,
        object_content,
        'content-type' => ''
      )
    end
  else
    http_client.start(url.host) do |http|
      http.send_request(
        'PUT',
        url.request_uri,
        object_content,
        'content-type' => ''
      )
    end
  end
  content = object.get.body
  puts "The presigned URL for the object '#{object_key}' in the bucket " \
    "'#{bucket_name}' is:\n\n"
  puts url
  puts "\nUsing this presigned URL to get the content that " \
    "was just uploaded to this object, the object\'s content is:\n\n"
  puts content.read
  return true
rescue StandardError => e
  puts "Error uploading to presigned URL: #{e.message}"
  return false
end
def object_uploaded_to_presigned_url? ( s3_resource , bucket_name , object_key , object_content , http_client = nil ) object = s3_resource . bucket ( bucket_name ) . object ( object_key ) url = URI . parse ( object . presigned_url ( :put ) ) if http_client . nil? Net :: HTTP . start ( url . host ) do | http | http . send_request ( 'PUT' , url . request_uri , object_content , 'content-type' => '' ) end else http_client . start ( url . host ) do | http | http . send_request ( 'PUT' , url . request_uri , object_content , 'content-type' => '' ) end end content = object . get . body puts "The presigned URL for the object '#{object_key}' in the bucket " "'#{bucket_name}' is:\n\n" puts url puts "\nUsing this presigned URL to get the content that " "was just uploaded to this object, the object\'s content is:\n\n" puts content . read return true rescue StandardError => e puts "Error uploading to presigned URL: #{e.message}" return false end
Uploads an object to a bucket in Amazon Simple Storage Service (Amazon S3)
by using a presigned URL.
Uploads an object to a bucket in Amazon Simple Storage Service ( Amazon S3 ) by using a presigned URL .
true if the object was uploaded; otherwise, false.
true if the object was uploaded ; otherwise false .
Boolean
s3_resource
bucket_name
The name of the bucket.
The name of the bucket .
object_key
The name of the object.
The name of the object .
object_content
The content to upload to the object.
The content to upload to the object .
http_client
An initialized HTTP client. This is especially useful for testing with mock HTTP clients. If not specified, a default HTTP client is created.
An initialized HTTP client . This is especially useful for testing with mock HTTP clients . If not specified a default HTTP client is created .
s3_client
An initialized S3 resource.
An initialized S3 resource .
example
exit 1 unless object_uploaded_to_presigned_url?( Aws::S3::Resource.new(region: 'us-west-2'), 'doc-example-bucket', 'my-file.txt', 'This is the content of my-file.txt' )
exit 1 unless object_uploaded_to_presigned_url? ( Aws :: S3 :: Resource . new ( region : ' us - west - 2 ' ) ' doc - example - bucket ' ' my - file . txt ' ' This is the content of my - file . txt ' )