From 9ca50c3474b6280401eb16cab6c653650eb9438e Mon Sep 17 00:00:00 2001 From: Grisha Kostyuk Date: Tue, 7 Dec 2021 14:14:19 +0200 Subject: [PATCH] copy ContentType and Metadata from source object (#4666) --- moto/s3/models.py | 1 + tests/test_s3/test_s3.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/moto/s3/models.py b/moto/s3/models.py index 361b283a5..34861a9df 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -2066,6 +2066,7 @@ class S3Backend(BaseBackend, CloudWatchMetricProvider): lock_until=key.lock_until, ) self.tagger.copy_tags(key.arn, new_key.arn) + new_key.set_metadata(key.metadata) if acl is not None: new_key.set_acl(acl) diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index f6daa9d7a..9ae367f48 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -1042,6 +1042,27 @@ def test_copy_key_replace_metadata(): bucket.get_key("new-key").get_metadata("momd").should.equal("Mometadatastring") +@mock_s3 +def test_copy_key_with_metadata_boto3(): + s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) + client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) + s3.create_bucket(Bucket="foobar") + + key = s3.Object("foobar", "the-key") + metadata = {"md": "Metadatastring"} + content_type = "application/json" + initial = key.put(Body=b"{}", Metadata=metadata, ContentType=content_type) + + client.copy_object( + Bucket="foobar", CopySource="foobar/the-key", Key="new-key", + ) + + resp = client.get_object(Bucket="foobar", Key="new-key") + resp["Metadata"].should.equal(metadata) + resp["ContentType"].should.equal(content_type) + resp["ETag"].should.equal(initial["ETag"]) + + @mock_s3 def test_copy_key_replace_metadata_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME)