Skip to content

Commit

Permalink
allow s3_object put to specify metadata again (#1882)
Browse files Browse the repository at this point in the history
allow s3_object put to specify metadata again

fix erroneous change to meaning
fixes #1881
optional extra refactorings as separate commits (happy to drop these if unwanted)

Reviewed-by: Mark Chappell
(cherry picked from commit 3333b65)
  • Loading branch information
timdiggins authored and patchback[bot] committed Nov 29, 2023
1 parent 0ea628d commit b9382d9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- s3_object - when doing a put and specifying ``Content-Type`` in metadata, this module (since 6.0.0) erroneously set the ``Content-Type`` to ``None`` causing the put to fail.
Fix now correctly honours the specified ``Content-Type`` (https://github.com/ansible-collections/amazon.aws/issues/1881).
20 changes: 10 additions & 10 deletions plugins/modules/s3_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,15 +639,14 @@ def path_check(path):
return False


def get_content_type(src, present=True):
if not present:
content_type = None
if src:
content_type = mimetypes.guess_type(src)[0]
if content_type is None:
# s3 default content type
content_type = "binary/octet-stream"
return content_type
def guess_content_type(src):
if src:
content_type = mimetypes.guess_type(src)[0]
if content_type:
return content_type

# S3 default content type
return "binary/octet-stream"


def get_extra_params(
Expand Down Expand Up @@ -732,7 +731,8 @@ def upload_s3file(
elif isinstance(permissions, list):
extra["ACL"] = permissions[0]

extra["ContentType"] = get_content_type(src, present=extra.get("ContentType"))
if "ContentType" not in extra:
extra["ContentType"] = guess_content_type(src)

if src:
s3.upload_file(aws_retry=True, Filename=src, Bucket=bucket, Key=obj, ExtraArgs=extra)
Expand Down
28 changes: 28 additions & 0 deletions tests/integration/targets/s3_object/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,34 @@
- "'Object deleted from bucket' in result.msg"
- result is changed

- name: test putting an object in the bucket with metadata set
s3_object:
bucket: "{{ bucket_name }}"
mode: put
src: "{{ remote_tmp_dir }}/upload.txt"
metadata: "Content-Type=text/plain"
object: delete_meta.txt
tags:
"lowercase spaced": "hello cruel world"
"Title Case": "Hello Cruel World"
retries: 3
delay: 3
register: result

- assert:
that:
- result is changed
- result.msg == "PUT operation complete"

- name: test delobj to just delete an object in the bucket
s3_object:
bucket: "{{ bucket_name }}"
mode: delobj
object: delete_meta.txt
retries: 3
delay: 3
register: result

- name: test putting an encrypted object in the bucket
s3_object:
bucket: "{{ bucket_name }}"
Expand Down

0 comments on commit b9382d9

Please sign in to comment.