Skip to content

Commit

Permalink
Merge pull request #2021 from ansible-collections/patchback/backports…
Browse files Browse the repository at this point in the history
…/stable-7/9a159338223026b95d103773c05080bb9f9bc06f/pr-2018

[PR #2018/9a159338 backport][stable-7] s3_object - fix issue when copying object with provided metadata
  • Loading branch information
abikouo authored Mar 21, 2024
2 parents 7b16614 + bf72ed5 commit 63bcca3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- s3_object - Fix the issue when copying an object with overriding metadata. (https://github.com/ansible-collections/amazon.aws/issues/1991).
9 changes: 8 additions & 1 deletion plugins/modules/s3_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@
object: /my/desired/key.txt
src: /usr/local/myfile.txt
mode: put
metadata: 'Content-Encoding=gzip,Cache-Control=no-cache'
metadata:
Content-Encoding: gzip
Cache-Control: no-cache
- name: PUT/upload with custom headers
amazon.aws.s3_object:
Expand Down Expand Up @@ -1314,6 +1316,11 @@ def copy_object_to_bucket(module, s3, bucket, obj, encrypt, metadata, validate,
metadata,
)
)
if metadata:
# 'MetadataDirective' Specifies whether the metadata is copied from the source object or replaced
# with metadata that's provided in the request. The default value is 'COPY', therefore when user
# specifies a metadata we should set it to 'REPLACE'
params.update({"MetadataDirective": "REPLACE"})
s3.copy_object(aws_retry=True, **params)
put_object_acl(module, s3, bucket, obj)
# Tags
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/s3_object/aliases
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cloud/aws
aws_s3
s3_object_info
time=12m
71 changes: 70 additions & 1 deletion tests/integration/targets/s3_object/tasks/copy_object.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
---
- block:
- vars:
withmeta_data:
something: exists
version: "1.0.2"
metacopy_data:
name: metacopy
version: "1.0.3"
block:
- name: define bucket name used for tests
ansible.builtin.set_fact:
copy_bucket:
Expand Down Expand Up @@ -142,6 +149,68 @@
- result is not changed
- result.msg == "Key this_key_does_not_exist.txt does not exist in bucket "+copy_bucket.src+"."

# Copy with metadata
- name: Set fact for bucket name
ansible.builtin.set_fact:
bucket_name: "{{ copy_bucket.dst }}"

- name: Create test bucket
amazon.aws.s3_bucket:
name: "{{ bucket_name }}"
state: present

- name: Create test object
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
object: nometa
mode: put
content: "some content"

- name: Copy and add metadata
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
object: metacopy
mode: copy
copy_src:
bucket: "{{ bucket_name }}"
object: nometa
metadata: "{{ metacopy_data }}"

- name: Create test object with metadata
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
object: withmeta
mode: put
content: "another content"
metadata: "{{ withmeta_data }}"

- name: Copy and preserve metadata
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
object: copywithmeta
mode: copy
copy_src:
bucket: "{{ bucket_name }}"
object: withmeta

- name: Get objects info
amazon.aws.s3_object_info:
bucket_name: "{{ bucket_name }}"
object_name: "{{ item }}"
loop:
- nometa
- metacopy
- withmeta
- copywithmeta
register: obj_info

- assert:
that:
- obj_info.results | selectattr('item', 'equalto', 'nometa') | map(attribute='object_info.0.object_data.metadata') | first == {}
- obj_info.results | selectattr('item', 'equalto', 'withmeta') | map(attribute='object_info.0.object_data.metadata') | first == withmeta_data
- obj_info.results | selectattr('item', 'equalto', 'metacopy') | map(attribute='object_info.0.object_data.metadata') | first == metacopy_data
- obj_info.results | selectattr('item', 'equalto', 'copywithmeta') | map(attribute='object_info.0.object_data.metadata') | first == withmeta_data

always:
- ansible.builtin.include_tasks: delete_bucket.yml
with_items:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/targets/s3_object/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,6 @@
that:
- binary_files.results[0].stat.checksum == binary_files.results[1].stat.checksum

- ansible.builtin.include_tasks: copy_object.yml
- ansible.builtin.include_tasks: copy_object_acl_disabled_bucket.yml
- name: Run tagging tests
block:
# ============================================================
Expand Down Expand Up @@ -1074,6 +1072,8 @@
- (result.tags | length) == 0

- ansible.builtin.include_tasks: copy_recursively.yml
- ansible.builtin.include_tasks: copy_object.yml
- ansible.builtin.include_tasks: copy_object_acl_disabled_bucket.yml
always:
- name: delete temporary files
file:
Expand Down

0 comments on commit 63bcca3

Please sign in to comment.