Method: AWS::S3::Client#put_object

Defined in:
lib/aws/s3/client.rb

#put_object(options = {}) ⇒ Core::Response

Puts data into an object, replacing the current contents.

s3_client.put_object({
  :bucket_name => 'bucket-name',
  :key => 'readme.txt',
  :data => 'This is the readme for ...',
})

Block Form

In block form, this method yields a stream to the block that accepts data chunks. For example:

s3_client.put_object(
  :bucket_name => 'mybucket',
  :key => 'some/key'
  :content_length => File.size('myfile')
) do |buffer|

  File.open('myfile') do |io|
    buffer.write(io.read(length)) until io.eof?
  end

end

This form is useful if you need finer control over how potentially large amounts of data are read from another source before being sent to S3; for example, if you are using a non-blocking IO model and reading from a large file on disk or from another network stream. Some HTTP handlers do not support streaming request bodies, so if you plan to upload large objects using this interface you should make sure the HTTP handler you configure for the client meets your needs.

object on the server side, e.g. :aes256)

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :data (required, String, Pathname, File, IO)

    The data to upload. This can be provided as a string, a Pathname object, or any object that responds to #read and #eof? (e.g. IO, File, Tempfile, StringIO, etc).

  • :content_length (Integer)

    Required if you are using block form to write data or if it is not possible to determine the size of :data. A best effort is made to determine the content length of strings, files, tempfiles, io objects, and any object that responds to #length or #size.

  • :website_redirect_location (String)

    If the bucket is configured as a website, redirects requests for this object to another object in the same bucket or to an external URL.

  • :metadata (Hash)

    A hash of metadata to be included with the object. These will be sent to S3 as headers prefixed with x-amz-meta.

  • :acl (Symbol) — default: :private

    A canned access control policy. Accepted values include:

    • :private

    • :public_read

  • :storage_class+ (String) — default: 'STANDARD'

    Controls whether Reduced Redundancy Storage is enabled for the object. Valid values are ‘STANDARD’ and ‘REDUCED_REDUNDANCY’.

  • :server_side_encryption (Symbol, String) — default: nil

    The algorithm used to encrypt the object on the server side (e.g. :aes256).

  • :cache_control (String)

    Can be used to specify caching behavior.

  • :content_disposition (String)

    Specifies presentational information.

  • :content_encoding (String)

    Specifies the content encoding.

  • :content_md5 (String)

    The base64 encoded content md5 of the :data.

  • :content_type (String)

    Specifies the content type.

  • :expires (String)

    The date and time at which the object is no longer cacheable.

  • :acl (String)

    A canned ACL (e.g. ‘private’, ‘public-read’, etc). See the S3 API documentation for a complete list of valid values.

  • :grant_read (String)
  • :grant_write (String)
  • :grant_read_acp (String)
  • :grant_write_acp (String)
  • :grant_full_control (String)

Returns:



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/aws/s3/client.rb', line 860

object_method(:put_object, :put, :header_options => {
  :website_redirect_location => 'x-amz-website-redirect-location',
  :acl => 'x-amz-acl',
  :grant_read => 'x-amz-grant-read',
  :grant_write => 'x-amz-grant-write',
  :grant_read_acp => 'x-amz-grant-read-acp',
  :grant_write_acp => 'x-amz-grant-write-acp',
  :grant_full_control => 'x-amz-grant-full-control',
  :content_md5 => 'Content-MD5',
  :cache_control => 'Cache-Control',
  :content_disposition => 'Content-Disposition',
  :content_encoding => 'Content-Encoding',
  :content_type => 'Content-Type',
  :expires => 'Expires',
}) do

  configure_request do |request, options|

    options = compute_write_options(options)
    set_body_stream_and_content_length(request, options)

    request. = options[:metadata]
    request.storage_class = options[:storage_class]
    request.server_side_encryption = options[:server_side_encryption]

    super(request, options)

  end

  process_response do |resp|
    extract_object_headers(resp)
  end

  simulate_response do |response|
    response.data[:etag] = 'abc123'
    response.data[:version_id] = nil
  end

end