Menu
Amazon Simple Storage Service
Developer Guide (API Version 2006-03-01)

Upload an Object Using a Pre-Signed URL (AWS SDK for Ruby)

The following tasks guide you through using a Ruby script to upload an object using a pre-signed URL for either version of the SDK for Ruby.

Using AWS SDK for Ruby - Version 2

The following tasks guide you through using a Ruby script to upload an object using a pre-signed URL for SDK for Ruby - Version 2.

Uploading Objects - SDK for Ruby - Version 2

1

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

2

You provide a bucket name and an object key by calling the #bucket[] and the #object[] methods of your Aws::S3::Resource class instance.

Generate a pre-signed URL by creating an instance of the URI class and use it to parse the .presigned_url method of your Aws::S3::Resource class instance. You must specify :put as an argument to .presigned_url, and you must specify PUT to Net::HTTP::Session#send_request if you want to upload an object.

3

Anyone with the pre-signed URL can upload an object.

The upload creates an object or replaces any existing object with the same key that is specified in the pre-signed URL.

The following Ruby code sample demonstrates the preceding tasks for SDK for Ruby - Version 2.

Copy
#Uploading an object using a pre-signed URL for SDK for Ruby - Version 2. require 'aws-sdk-resources' require 'net/http' s3 = Aws::S3::Resource.new(region:'us-west-2') obj = s3.bucket('BucketName').object('KeyName') # Replace BucketName with the name of your bucket. # Replace KeyName with the name of the object you are creating or replacing. url = URI.parse(obj.presigned_url(:put)) body = "Hello World!" # This is the contents of your object. In this case, it's a simple string. Net::HTTP.start(url.host) do |http| http.send_request("PUT", url.request_uri, body, { # This is required, or Net::HTTP will add a default unsigned content-type. "content-type" => "", }) end puts obj.get.body.read # This will print out the contents of your object to the terminal window.

Using AWS SDK for Ruby - Version 1

Uploading Objects - SDK for Ruby - Version 1

1

Create an instance of the AWS:S3 class.

2

You provide a bucket name and an object key by calling the #bucket[] and the #object[] methods of your AWS::S3::S3Object class instance.

Generate a pre-signed URL by calling the .url_for method of your AWS::S3 class instance. You must specify :put as an argument to .url_for, and you must specify PUT to Net::HTTP::Session#send_request if you want to upload an object.

3

Anyone with the pre-signed URL can upload an object.

The upload creates an object or replaces any existing object with the same key that is specified in the pre-signed URL.

The following Ruby code sample demonstrates the preceding tasks for AWS SDK for Ruby - Version 1.

Copy
#Uploading an object using a pre-signed URL for SDK for Ruby - Version 1. require 'aws-sdk-v1' require 'net/http' s3 = AWS::S3.new(region:'us-west-2') obj = s3.buckets['BucketName'].objects['KeyName'] # Replace BucketName with the name of your bucket. # Replace KeyName with the name of the object you are creating or replacing. url = obj.url_for(:write, :content_type => "text/plain") body = "Hello World!" # This is the contents of your object. In this case, it's a simple string. Net::HTTP.start(url.host) do |http| http.send_request("PUT", url.request_uri, body, {"content-type" => "text/plain",}) # The content-type must be specified in the pre-signed url. end puts obj.read # This will print out the contents of your object to the terminal window. puts obj.content_type # This will print out the content type of your object to the terminal window.