From 693197aafefa542e90121ec2a5586f91721552fa Mon Sep 17 00:00:00 2001 From: Trevor Rowe Date: Fri, 16 Oct 2015 13:52:50 -0700 Subject: [PATCH] Added second form for #copy_from and #copy_to Fixes #963 --- CHANGELOG.md | 16 +++++++++++ .../services/s3/object_copier.rb | 12 ++++++++ .../services/s3/object/multipart_copy_spec.rb | 28 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61937b2736f..e21b5566977 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,22 @@ Unreleased Changes ------------------ +* Feature - Aws::S3 - The `#copy_from` and `#copy_to` methods of `Aws::S3::Object` + now accept the source or target bucket and key as a hash with mixed + options. + + ```ruby + # old format + obj.copy_from({ bucket: 'source-bucket', key: 'source-key' }, { multipart_copy: true }) + + # new format + obj.copy_from(bucket: 'source-bucket', key: 'source-key', multipart_copy: true) + ``` + + Passing two hashes is still valid. + + See [related GitHub issue #963](https://github.com/aws/aws-sdk-ruby/issues/963). + * Feature - Aws::S3 - `Aws::S3::Client#put_object` now accepts closed files. See [related GitHub issue #939](https://github.com/aws/aws-sdk-ruby/pull/939). diff --git a/aws-sdk-resources/lib/aws-sdk-resources/services/s3/object_copier.rb b/aws-sdk-resources/lib/aws-sdk-resources/services/s3/object_copier.rb index 34b1982196a..746eadd4615 100644 --- a/aws-sdk-resources/lib/aws-sdk-resources/services/s3/object_copier.rb +++ b/aws-sdk-resources/lib/aws-sdk-resources/services/s3/object_copier.rb @@ -12,11 +12,13 @@ def initialize(object, options = {}) end def copy_from(source, options = {}) + apply_options(source, options) copy_object(source, @object, options) end def copy_to(target, options = {}) + apply_options(target, options) copy_object(@object, target, options) end @@ -57,6 +59,16 @@ def copy_target(target) end end + def apply_options(source_or_target, options) + if Hash === source_or_target + source_or_target.each do |key, value| + next if key == :bucket + next if key == :key + options[key] = value + end + end + end + end end end diff --git a/aws-sdk-resources/spec/services/s3/object/multipart_copy_spec.rb b/aws-sdk-resources/spec/services/s3/object/multipart_copy_spec.rb index f8db47794bd..9450ed3afdc 100644 --- a/aws-sdk-resources/spec/services/s3/object/multipart_copy_spec.rb +++ b/aws-sdk-resources/spec/services/s3/object/multipart_copy_spec.rb @@ -38,6 +38,20 @@ module S3 object.copy_from(bucket:'source-bucket', key:'source-key') end + it 'accept a hash with options merged' do + expect(client).to receive(:copy_object).with({ + bucket: 'bucket', + key: 'key', + copy_source: 'source-bucket/source-key', + content_type: 'text/plain', + }) + object.copy_from( + bucket: 'source-bucket', + key: 'source-key', + content_type: 'text/plain' + ) + end + it 'accepts an S3::Object source' do expect(client).to receive(:copy_object).with({ bucket: 'bucket', @@ -86,6 +100,20 @@ module S3 object.copy_to(bucket:'target-bucket', key:'target-key') end + it 'accept a hash with options merged' do + expect(client).to receive(:copy_object).with({ + bucket: 'target-bucket', + key: 'target-key', + copy_source: 'bucket/key', + content_type: 'text/plain', + }) + object.copy_to( + bucket: 'target-bucket', + key: 'target-key', + content_type: 'text/plain' + ) + end + it 'accepts an S3::Object source' do expect(client).to receive(:copy_object).with({ bucket: 'target-bucket',