Class: AWS::S3::Client

Inherits:
Core::Client show all
Extended by:
Validators
Includes:
Core::UriEscape, Validators
Defined in:
lib/aws/s3/client.rb,
lib/aws/s3/client/xml.rb

Overview

Client class for Amazon Simple Storage Service (S3).

Defined Under Namespace

Modules: Validators

Constant Summary collapse

API_VERSION =
'2006-03-01'
XMLNS =
"http://s3.amazonaws.com/doc/#{API_VERSION}/"

Instance Attribute Summary

Attributes inherited from Core::Client

#config, #http_read_timeout

Instance Method Summary collapse

Methods included from Validators

dns_compatible_bucket_name?, json_validation_message, path_style_bucket_name?, require_acl!, require_bucket_name!, require_part_number!, require_policy!, require_upload_id!, set_body_stream_and_content_length, valid_bucket_name?, validate!, validate_bucket_name!, validate_key!, validate_parts!

Methods included from Core::UriEscape

escape, escape_path

Methods inherited from Core::Client

#initialize, #log_warning, #operations, operations, #with_http_handler, #with_options

Constructor Details

This class inherits a constructor from AWS::Core::Client

Instance Method Details

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :upload_id (required, String)

Returns:



1290
1291
1292
1293
1294
1295
1296
# File 'lib/aws/s3/client.rb', line 1290

object_method(:abort_multipart_upload, :delete) do
  configure_request do |req, options|
      require_upload_id!(options[:upload_id])
    super(req, options)
    req.add_param('uploadId', options[:upload_id])
  end
end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :upload_id (required, String)
  • :parts (required, Array<String>)

Returns:



1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
# File 'lib/aws/s3/client.rb', line 1257

object_method(:complete_multipart_upload, :post,
              XML::CompleteMultipartUpload) do
  configure_request do |req, options|
    require_upload_id!(options[:upload_id])
    validate_parts!(options[:parts])
    super(req, options)
    req.add_param('uploadId', options[:upload_id])
    parts_xml = options[:parts].map do |part|
      "<Part>"+
        "<PartNumber>#{part[:part_number].to_i}</PartNumber>"+
        "<ETag>#{REXML::Text.normalize(part[:etag].to_s)}</ETag>"+
        "</Part>"
    end.join
    req.body =
      "<CompleteMultipartUpload>#{parts_xml}</CompleteMultipartUpload>"
  end

  process_response do |resp|
    extract_object_headers(resp)
  end

  simulate_response do |response|
    response.data = {}
  end

end

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

Copies an object from one key to another.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

    Name of the bucket to copy a object into.

  • :key (required, String)

    Where (object key) in the bucket the object should be copied to.

  • :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.

  • :copy_source (required, String)

    The source bucket name and key, joined by a forward slash (‘/’). This string must be URL-encoded. Additionally, you must have read access to the source object.

  • :acl (String)

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

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

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

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

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

  • :expires (String)

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

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

Returns:



1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
# File 'lib/aws/s3/client.rb', line 1350

object_method(:copy_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',
  :copy_source => 'x-amz-copy-source',
  :cache_control => 'Cache-Control',
  :metadata_directive => 'x-amz-metadata-directive',
  :content_type => 'Content-Type',
  :content_disposition => 'Content-Disposition',
  :expires => 'Expires',
}) do

  configure_request do |req, options|

    validate!(:copy_source, options[:copy_source]) do
      "may not be blank" if options[:copy_source].to_s.empty?
    end

    options = options.merge(:copy_source => escape_path(options[:copy_source]))
    super(req, options)
    req. = options[:metadata]
    req.storage_class = options[:storage_class]
    req.server_side_encryption = options[:server_side_encryption]

    if options[:version_id]
      req.headers['x-amz-copy-source'] += "?versionId=#{options[:version_id]}"
    end
  end

  process_response do |resp|
    extract_object_headers(resp)
  end

end

#create_bucket(options = {}) ⇒ Core::Response Also known as: put_bucket

Creates a bucket.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :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:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/aws/s3/client.rb', line 131

bucket_method(:create_bucket, :put, :header_options => {
  :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',
}) do

  configure_request do |req, options|
    validate_bucket_name!(options[:bucket_name])
    if location = options[:location_constraint]
      xmlns = "http://s3.amazonaws.com/doc/#{API_VERSION}/"
      req.body = <<-XML
        <CreateBucketConfiguration xmlns="#{xmlns}">
          <LocationConstraint>#{location}</LocationConstraint>
        </CreateBucketConfiguration>
      XML
    end
    super(req, options)
  end

end

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

Deletes an empty bucket.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



270
# File 'lib/aws/s3/client.rb', line 270

bucket_method(:delete_bucket, :delete)

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



414
415
416
417
418
419
# File 'lib/aws/s3/client.rb', line 414

bucket_method(:delete_bucket_cors, :delete) do
  configure_request do |req, options|
    req.add_param('cors')
    super(req, options)
  end
end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



311
312
313
314
315
316
317
318
# File 'lib/aws/s3/client.rb', line 311

bucket_method(:delete_bucket_lifecycle_configuration, :delete) do

  configure_request do |req, options|
    req.add_param('lifecycle')
    super(req, options)
  end

end

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

Deletes the access policy for a bucket.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



536
# File 'lib/aws/s3/client.rb', line 536

bucket_method(:delete_bucket_policy, :delete, 'policy')

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



473
474
475
476
477
478
# File 'lib/aws/s3/client.rb', line 473

bucket_method(:delete_bucket_tagging, :delete) do
  configure_request do |req, options|
    req.add_param('tagging')
    super(req, options)
  end
end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



263
# File 'lib/aws/s3/client.rb', line 263

bucket_method(:delete_bucket_website, :delete, 'website')

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :version_id (String)
  • :mfa (String)

Returns:



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'lib/aws/s3/client.rb', line 1025

object_method(:delete_object, :delete, :header_options => { :mfa => "x-amz-mfa" }) do

  configure_request do |req, options|
    super(req, options)
    if options[:version_id]
      req.add_param('versionId', options[:version_id])
    end
  end

  process_response do |resp|
    resp.data[:version_id] = resp.http_response.header('x-amz-version-id')
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :keys (required, Array<String>)
  • :quiet (Boolean) — default: true
  • :mfa (String)

Returns:



1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
# File 'lib/aws/s3/client.rb', line 1186

bucket_method(:delete_objects, :post, 'delete', XML::DeleteObjects, :header_options => { :mfa => "x-amz-mfa" }) do
  configure_request do |req, options|

    super(req, options)

    quiet = options.key?(:quiet) ? options[:quiet] : true

    # previously named this option :objects, since renamed
    keys = options[:objects] || options[:keys]

    objects = keys.inject('') do |xml,o|
      xml << "<Object><Key>#{REXML::Text.normalize(o[:key])}</Key>"
      xml << "<VersionId>#{o[:version_id]}</VersionId>" if o[:version_id]
      xml << "</Object>"
    end

    xml = '<?xml version="1.0" encoding="UTF-8"?>'
    xml << "<Delete><Quiet>#{quiet}</Quiet>#{objects}</Delete>"

    req.body = xml
    req.headers['content-md5'] = md5(xml)

  end
end

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

Gets the access control list for a bucket.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



702
# File 'lib/aws/s3/client.rb', line 702

bucket_method(:get_bucket_acl, :get, 'acl', XML::GetBucketAcl)

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/aws/s3/client.rb', line 397

bucket_method(:get_bucket_cors, :get) do

  configure_request do |req, options|
    req.add_param('cors')
    super(req, options)
  end

  process_response do |resp|
    resp.data = XML::GetBucketCors.parse(resp.http_response.body)
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/aws/s3/client.rb', line 293

bucket_method(:get_bucket_lifecycle_configuration, :get) do

  configure_request do |req, options|
    req.add_param('lifecycle')
    super(req, options)
  end

  process_response do |resp|
    xml = resp.http_response.body
    resp.data = XML::GetBucketLifecycleConfiguration.parse(xml)
  end

end

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

Gets the bucket’s location constraint.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



574
575
576
577
578
579
580
581
582
# File 'lib/aws/s3/client.rb', line 574

bucket_method(:get_bucket_location, :get, 'location') do

  process_response do |response|
    regex = />(.*)<\/LocationConstraint>/
    matches = response.http_response.body.to_s.match(regex)
    response.data[:location_constraint] = matches ? matches[1] : nil
  end

end

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

Gets the access policy for a bucket.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



523
524
525
526
527
528
529
# File 'lib/aws/s3/client.rb', line 523

bucket_method(:get_bucket_policy, :get, 'policy') do

  process_response do |resp|
    resp.data[:policy] = resp.http_response.body
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/aws/s3/client.rb', line 456

bucket_method(:get_bucket_tagging, :get) do

  configure_request do |req, options|
    req.add_param('tagging')
    super(req, options)
  end

  process_response do |resp|
    resp.data = XML::GetBucketTagging.parse(resp.http_response.body)
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:



588
589
# File 'lib/aws/s3/client.rb', line 588

bucket_method(:get_bucket_versioning, :get, 'versioning',
XML::GetBucketVersioning)

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

Returns * :index_document - (Hash)

* +:suffix+ - (String)
  • :error_document - (Hash)

    • :key - (String).

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)

Returns:

  • (Core::Response)
    • :index_document - (Hash)

      • :suffix - (String)

    • :error_document - (Hash)

      • :key - (String)



257
# File 'lib/aws/s3/client.rb', line 257

bucket_method(:get_bucket_website, :get, 'website', XML::GetBucketWebsite)

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

Gets the data for a key.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :if_modified_since (Time)

    If specified, the response will contain an additional :modified value that returns true if the object was modified after the given time. If :modified is false, then the response :data value will be nil.

  • :if_unmodified_since (Time)

    If specified, the response will contain an additional :unmodified value that is true if the object was not modified after the given time. If :unmodified returns false, the :data value will be nil.

  • :if_match (String)

    If specified, the response will contain an additional :matches value that is true if the object ETag matches the value for this option. If :matches is false, the :data value of the response will be nil.

  • :if_none_match (String)

    If specified, the response will contain an additional :matches value that is true if and only if the object ETag matches the value for this option. If :matches is true, the :data value of the response will be nil.

  • :range (Range<Integer>)

    A byte range of data to request.

Returns:



957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/aws/s3/client.rb', line 957

object_method(:get_object, :get,
              :header_options => {
                :if_modified_since => "If-Modified-Since",
                :if_unmodified_since => "If-Unmodified-Since",
                :if_match => "If-Match",
                :if_none_match => "If-None-Match"
              }) do
  configure_request do |req, options|

    super(req, options)

    if options[:version_id]
      req.add_param('versionId', options[:version_id])
    end

    ["If-Modified-Since",
     "If-Unmodified-Since"].each do |date_header|
      case value = req.headers[date_header]
      when DateTime
        req.headers[date_header] = Time.parse(value.to_s).rfc2822
      when Time
        req.headers[date_header] = value.rfc2822
      end
    end

    if options[:range]
      range = options[:range]
      range = "bytes=#{range.first}-#{range.last}" if range.is_a?(Range)
      req.headers['Range'] = range
    end

  end

  process_response do |resp|
    extract_object_headers(resp)
    resp.data[:data] = resp.http_response.body
  end

end

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

Gets the access control list for an object.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)

Returns:



796
# File 'lib/aws/s3/client.rb', line 796

object_method(:get_object_acl, :get, 'acl', XML::GetBucketAcl)

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :version_id (String)

Returns:



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/aws/s3/client.rb', line 1003

object_method(:head_object, :head) do

  configure_request do |req, options|
    super(req, options)
    if options[:version_id]
      req.add_param('versionId', options[:version_id])
    end
  end

  process_response do |resp|
    extract_object_headers(resp)
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :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)
  • :acl (Symbol)
  • :cache_control (String)
  • :content_disposition (String)
  • :content_encoding (String)
  • :content_type (String)
  • :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).

  • :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:



1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/aws/s3/client.rb', line 1124

object_method(:initiate_multipart_upload, :post, 'uploads',
              XML::InitiateMultipartUpload,
              :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',
                :cache_control => 'Cache-Control',
                :content_disposition => 'Content-Disposition',
                :content_encoding => 'Content-Encoding',
                :content_type => 'Content-Type',
                :expires => 'Expires',
              }) do

  configure_request do |req, options|
    req. = options[:metadata]
    req.storage_class = options[:storage_class]
    req.server_side_encryption = options[:server_side_encryption]
    super(req, options)
  end

  process_response do |resp|
    extract_object_headers(resp)
  end

end

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

Parameters:

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

Returns:



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/aws/s3/client.rb', line 483

add_client_request_method(:list_buckets) do

  configure_request do |req, options|
    req.http_method = "GET"
  end

  process_response do |resp|
    resp.data = XML::ListBuckets.parse(resp.http_response.body)
  end

  simulate_response do |resp|
    resp.data = Core::XML::Parser.new(XML::ListBuckets.rules).simulate
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :delimiter (String)
  • :key_marker (String)
  • :max_keys (String)
  • :upload_id_marker (String)
  • :max_uploads (String)
  • :prefix (String)

Returns:



1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
# File 'lib/aws/s3/client.rb', line 1164

bucket_method(:list_multipart_uploads,
              :get, 'uploads',
              XML::ListMultipartUploads) do
  configure_request do |req, options|
    super(req, options)
    params = %w(delimiter key_marker max_keys) +
      %w(upload_id_marker max_uploads prefix)
    params.each do |param|
      if options[param.to_sym]
        req.add_param(param.gsub(/_/, '-'), options[param.to_sym])
      end
    end
  end
end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :prefix (String)
  • :delimiter (String)
  • :max_keys (String)
  • :key_marker (String)
  • :version_id_marker (String)

Returns:



600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/aws/s3/client.rb', line 600

bucket_method(:list_object_versions, :get, 'versions',
  XML::ListObjectVersions) do

  configure_request do |req, options|
    super(req, options)
    params = %w(delimiter key_marker max_keys prefix version_id_marker)
    params.each do |param|
      if options[param.to_sym]
        req.add_param(param.gsub(/_/, '-'), options[param.to_sym])
      end
    end
  end

end

#list_objects(options = {}) ⇒ Core::Response Also known as: get_bucket

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :delimiter (String)
  • :marker (String)
  • :max_keys (String)
  • :prefix (String)

Returns:



1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/aws/s3/client.rb', line 1079

bucket_method(:list_objects, :get, XML::ListObjects) do
  configure_request do |req, options|
    super(req, options)
    params = %w(delimiter marker max_keys prefix)
    params.each do |param|
      if options[param.to_sym]
        req.add_param(param.gsub(/_/, '-'), options[param.to_sym])
      end
    end
  end
end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :upload_id (required, String)
  • :max_parts (Integer)
  • :part_number_marker (Integer)

Returns:



1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
# File 'lib/aws/s3/client.rb', line 1306

object_method(:list_parts, :get, XML::ListParts) do

  configure_request do |req, options|
      require_upload_id!(options[:upload_id])
    super(req, options)
    req.add_param('uploadId', options[:upload_id])
    req.add_param('max-parts', options[:max_parts])
    req.add_param('part-number-marker', options[:part_number_marker])
  end

end

#put_bucket_acl(options = {}) ⇒ Core::Response Also known as: set_bucket_acl

Sets the access control list for a bucket. You must specify an ACL via one of the following methods:

  • as a canned ACL (via :acl)

  • as a list of grants (via the :grant_* options)

  • as an access control policy document (via :access_control_policy)

Examples:

Using a canned acl

s3_client.put_bucket_acl(
  :bucket_name => 'bucket-name',
  :acl => 'public-read')

Using grants

s3_client.put_bucket_acl(
  :bucket_name => 'bucket-name',
  :grant_read => 'uri="http://acs.amazonaws.com/groups/global/AllUsers"',
  :grant_full_control => 'emailAddress="[email protected]", id="8a9...fa7"')

Using an access control policy document

policy_xml = <<-XML
  <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <Owner>
      <ID>852b113e7a2f25102679df27bb0ae12b3f85be6BucketOwnerCanonicalUserID</ID>
      <DisplayName>OwnerDisplayName</DisplayName>
    </Owner>
    <AccessControlList>
      <Grant>
        <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
          <ID>BucketOwnerCanonicalUserID</ID>
          <DisplayName>OwnerDisplayName</DisplayName>
        </Grantee>
        <Permission>FULL_CONTROL</Permission>
      </Grant>
      <Grant>
        <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
          <URI xmlns="">http://acs.amazonaws.com/groups/global/AllUsers</URI>
        </Grantee>
        <Permission xmlns="">READ</Permission>
      </Grant>
    </AccessControlList>
  </AccessControlPolicy>

XML
s3_client.put_bucket_acl(
  :bucket_name => 'bucket-name',
  :access_control_policy => policy_xml)

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :access_control_policy (String)

    An access control policy description as a string of XML. See the S3 API documentation for a description.

  • :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:



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/aws/s3/client.rb', line 677

bucket_method(:put_bucket_acl, :put, 'acl', :header_options => {
  :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',
}) do

  configure_request do |req, options|
    move_access_control_policy(options)
    require_acl!(options)
    super(req, options)
    req.body = options[:access_control_policy] if
       options[:access_control_policy]
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :rules (required, Array<Hash>)

    An array of rule hashes.

    • :id - (String) A unique identifier for the rule. The ID value can be up to 255 characters long. The IDs help you find a rule in the configuration.

    • :allowed_methods - (required,Array<String>) A list of HTTP methods that you want to allow the origin to execute. Each rule must identify at least one method.

    • :allowed_origins - (required,Array<String>) A list of origins you want to allow cross-domain requests from. This can contain at most one * wild character.

    • :allowed_headers - (Array<String>) A list of headers allowed in a pre-flight OPTIONS request via the Access-Control-Request-Headers header. Each header name specified in the Access-Control-Request-Headers header must have a corresponding entry in the rule. Amazon S3 will send only the allowed headers in a response that were requested. This can contain at most one * wild character.

    • :max_age_seconds - (Integer) The time in seconds that your browser is to cache the preflight response for the specified resource.

    • :expose_headers - (Array<String>) One or more headers in the response that you want customers to be able to access from their applications (for example, from a JavaScript XMLHttpRequest object).

Returns:



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/aws/s3/client.rb', line 349

bucket_method(:put_bucket_cors, :put) do
  configure_request do |req, options|

    req.add_param('cors')

    xml = Nokogiri::XML::Builder.new do |xml|
      xml.CORSConfiguration do
        options[:rules].each do |rule|
          xml.CORSRule do

            xml.ID(rule[:id]) if rule[:id]

            (rule[:allowed_methods] || []).each do |method|
              xml.AllowedMethod(method)
            end

            (rule[:allowed_origins] || []).each do |origin|
              xml.AllowedOrigin(origin)
            end

            (rule[:allowed_headers] || []).each do |header|
              xml.AllowedHeader(header)
            end

            xml.MaxAgeSeconds(rule[:max_age_seconds]) if
              rule[:max_age_seconds]

            (rule[:expose_headers] || []).each do |header|
              xml.ExposeHeader(header)
            end

          end
        end
      end
    end.doc.root.to_xml

    req.body = xml
    req.headers['content-md5'] = md5(xml)

    super(req, options)

  end
end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :tags (Hash)

Returns:



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/aws/s3/client.rb', line 426

bucket_method(:put_bucket_tagging, :put) do
  configure_request do |req, options|

    req.add_param('tagging')

    xml = Nokogiri::XML::Builder.new
    xml.Tagging do |xml|
      xml.TagSet do
        options[:tags].each_pair do |key,value|
          xml.Tag do
            xml.Key(key)
            xml.Value(value)
          end
        end
      end
    end

    xml = xml.doc.root.to_xml
    req.body = xml
    req.headers['content-md5'] = md5(xml)

    super(req, options)

  end
end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :redirect_all_requests_to (Hash)

    Describes the redirect behavior for every request to this bucket’s website endpoint. If this element is present, no other options are are allowed.

    • :host_name - (required, String) Name of the host where requests will be redirected.

    • :protocol - (String) Protocol to use (http, https) when redirecting requests. The default is the protocol that is used in the original request.

  • :index_document (Hash)
    • :suffix - (required, String) - A suffix that is appended to a request that is for a directory on the website endpoint (e.g. if the suffix is index.html and you make a request to samplebucket/images/ the data that is returned will be for the object with the key name images/index.html). The suffix must not be empty and must not include a slash character.

  • :error_document (Hash)
    • :key - (required, String) - The object key name to use when a 4XX class error occurs.

  • :routing_rules (Array<Hash>)
    • :redirect - (required, Hash)

      • :host_name - (String)

      • :protocol - (String)

      • :replace_key_prefix_with - (String)

      • :replace_key_with - (String)

      • :http_redirect_code - (String)

    • :condition - (Hash)

      • :key_prefix_equals - (String)

      • :http_error_code_returned_equals - (String)

Returns:



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/aws/s3/client.rb', line 190

bucket_method(:put_bucket_website, :put, 'website') do

  configure_request do |req, options|
    validate_bucket_name!(options[:bucket_name])
    req.body = Nokogiri::XML::Builder.new do |xml|
      xml.WebsiteConfiguration(:xmlns => XMLNS) do

        if redirect = options[:redirect_all_requests_to]
          xml.RedirectAllRequestsTo do
            xml.HostName(redirect[:host_name])
            xml.Protocol(redirect[:protocol]) if redirect[:protocol]
          end
        end

        if indx = options[:index_document]
          xml.IndexDocument do
            xml.Suffix(indx[:suffix])
          end
        end

        if err = options[:error_document]
          xml.ErrorDocument do
            xml.Key(err[:key])
          end
        end

        if rules = options[:routing_rules]
          xml.RoutingRules do
            rules.each do |rule|
              xml.RoutingRule do

                redirect = rule[:redirect]
                xml.Redirect do
                  xml.Protocol(redirect[:protocol]) if redirect[:protocol]
                  xml.HostName(redirect[:host_name]) if redirect[:host_name]
                  xml.ReplaceKeyPrefixWith(redirect[:replace_key_prefix_with]) if redirect[:replace_key_prefix_with]
                  xml.ReplaceKeyWith(redirect[:replace_key_with]) if redirect[:replace_key_with]
                  xml.HttpRedirectCode(redirect[:http_redirect_code]) if redirect[:http_redirect_code]
                end

                if condition = rule[:condition]
                  xml.Condition do
                    xml.KeyPrefixEquals(condition[:key_prefix_equals]) if condition[:key_prefix_equals]
                    xml.HttpErrorCodeReturnedEquals(condition[:http_error_code_returned_equals]) if condition[:http_error_code_returned_equals]
                  end
                end

              end
            end
          end
        end

      end
    end.doc.root.to_xml
    super(req, options)
  end

end

#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:



889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
# File 'lib/aws/s3/client.rb', line 889

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

#put_object_acl(options = {}) ⇒ Core::Response Also known as: set_object_acl

Sets the access control list for an object. You must specify an ACL via one of the following methods:

  • as a canned ACL (via :acl)

  • as a list of grants (via the :grant_* options)

  • as an access control policy document (via :access_control_policy)

Examples:

Using a canned acl

s3_client.put_object_acl(
  :bucket_name => 'bucket-name',
  :key => 'object-key',
  :acl => 'public-read')

Using grants

s3_client.put_bucket_acl(
  :bucket_name => 'bucket-name',
  :key => 'object-key',
  :grant_read => 'uri="http://acs.amazonaws.com/groups/global/AllUsers"',
  :grant_full_control => 'emailAddress="[email protected]", id="8a9...fa7"')

Using an access control policy document

policy_xml = <<-XML
  <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <Owner>
      <ID>852b113e7a2f25102679df27bb0ae12b3f85be6BucketOwnerCanonicalUserID</ID>
      <DisplayName>OwnerDisplayName</DisplayName>
    </Owner>
    <AccessControlList>
      <Grant>
        <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
          <ID>BucketOwnerCanonicalUserID</ID>
          <DisplayName>OwnerDisplayName</DisplayName>
        </Grantee>
        <Permission>FULL_CONTROL</Permission>
      </Grant>
      <Grant>
        <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
          <URI xmlns="">http://acs.amazonaws.com/groups/global/AllUsers</URI>
        </Grantee>
        <Permission xmlns="">READ</Permission>
      </Grant>
    </AccessControlList>
  </AccessControlPolicy>

XML
s3_client.put_bucket_acl(
  :bucket_name => 'bucket-name',
  :key => 'object-key',
  :access_control_policy => policy_xml)

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :access_control_policy (String)

    An access control policy description as a string of XML. See the S3 API documentation for a description.

  • :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:



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
# File 'lib/aws/s3/client.rb', line 770

object_method(:put_object_acl, :put, 'acl', :header_options => {
  :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',
}) do

  configure_request do |req, options|
    move_access_control_policy(options)
    require_acl!(options)
    super(req, options)
    req.body = options[:access_control_policy] if
       options[:access_control_policy]
  end

end

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

Restores a temporary copy of an archived object.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :days (required, Integer)

    the number of days to keep the restored object.

Returns:

Since:

  • 1.7.2



1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
# File 'lib/aws/s3/client.rb', line 1049

object_method(:restore_object, :post, 'restore',
    :header_options => { :content_md5 => 'Content-MD5' }) do
  configure_request do |req, options|
    super(req, options)

    validate!(:days, options[:days]) do
      "must be greater or equal to 1" if options[:days].to_i <= 0
    end

    xml = Nokogiri::XML::Builder.new do |xml|
      xml.RestoreRequest('xmlns' => XMLNS) do
        xml.Days(options[:days].to_i) if options[:days]
      end
    end.doc.root.to_xml

    req.body = xml
    req.headers['content-type'] = 'application/xml'
    req.headers['content-md5'] = md5(xml)
  end
end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :lifecycle_configuration (required, String)

Returns:



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/aws/s3/client.rb', line 277

bucket_method(:set_bucket_lifecycle_configuration, :put) do

  configure_request do |req, options|
    xml = options[:lifecycle_configuration]
    req.add_param('lifecycle')
    req.body = xml
    req.headers['content-md5'] = md5(xml)
    super(req, options)
  end

end

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

Sets the access policy for a bucket.

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :policy (required, String)

    This can be a String or any object that responds to #to_json.

Returns:



506
507
508
509
510
511
512
513
514
515
516
# File 'lib/aws/s3/client.rb', line 506

bucket_method(:set_bucket_policy, :put, 'policy') do

  configure_request do |req, options|
    require_policy!(options[:policy])
    super(req, options)
    policy = options[:policy]
    policy = policy.to_json unless policy.respond_to?(:to_str)
    req.body = policy
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :state (required, String)
  • :mfa_delete (String)
  • :mfa (String)

Returns:



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/aws/s3/client.rb', line 545

bucket_method(:set_bucket_versioning, :put, 'versioning', :header_options => { :mfa => "x-amz-mfa" }) do

  configure_request do |req, options|
    state = options[:state].to_s.downcase.capitalize
    unless state =~ /^(Enabled|Suspended)$/
      raise ArgumentError, "invalid versioning state `#{state}`"
    end

    # Leave validation of MFA options to S3
    mfa_delete = options[:mfa_delete].to_s.downcase.capitalize if options[:mfa_delete]

    # Generate XML request for versioning
    req.body = Nokogiri::XML::Builder.new do |xml|
      xml.VersioningConfiguration('xmlns' => XMLNS) do
        xml.Status(state)
        xml.MfaDelete(mfa_delete) if mfa_delete
      end
    end.doc.root.to_xml

    super(req, options)
  end

end

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

Parameters:

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

Options Hash (options):

  • :bucket_name (required, String)
  • :key (required, String)
  • :upload_id (required, String)
  • :part_number (required, Integer)
  • :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).

Returns:



1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/aws/s3/client.rb', line 1222

object_method(:upload_part, :put,
              :header_options => {
                :content_md5 => 'Content-MD5'
              }) do
  configure_request do |request, options|

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

    require_upload_id!(options[:upload_id])
    request.add_param('uploadId', options[:upload_id])

    require_part_number!(options[:part_number])
    request.add_param('partNumber', options[:part_number])

    super(request, options)

  end

  process_response do |resp|
    extract_object_headers(resp)
  end

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