
    Vh                        d Z dZdZddlZddlZddlZddlZddlZddlZddl	m
Z
 	 ddlZddlZddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddgZ! G d de"      Z# G d de"      Z$dFdZ%dGdZ&dHdZ'dHdZ(dHdZ)dIdZ*dJdZ+dGd Z,d! Z-dHd"Z.d# Z/d$ Z0d% Z1	 	 	 	 dKd&Z2d' Z3	 	 	 dLd(Z4dHd)Z5dHd*Z6dFd+Z7d, Z8dHd-Z9 ejt                  d.d/d0g1      d2        Z; ejt                  d.d/d0g1      d3        Z<dHd4Z=d5 Z>d6 Z?d7 Z@d8 ZAd9 ZBd: ZCd; ZDd< ZEd= ZFd> ZGdHd?ZHdHd@ZIdA ZJdB ZKdC ZLdD ZMeNdEk(  r eM        yy# e$ r Y Aw xY w)Ma*  
---
module: s3_object
version_added: 1.0.0
short_description: Manage objects in S3
description:
  - This module allows the user to manage the objects and directories within S3 buckets. Includes
    support for creating and deleting objects and directories, retrieving objects as files or
    strings, generating download links and copying objects that are already stored in Amazon S3.
  - S3 buckets can be created or deleted using the M(amazon.aws.s3_bucket) module.
  - Compatible with AWS, DigitalOcean, Ceph, Walrus, FakeS3 and StorageGRID.
  - When using non-AWS services, O(endpoint_url) should be specified.
options:
  bucket:
    description:
      - Bucket name.
    required: true
    type: str
  dest:
    description:
      - The destination file path when downloading an object/key when O(mode=get).
      - Ignored when O(mode) is not V(get).
    type: path
  encrypt:
    description:
      - Asks for server-side encryption of the objects when O(mode=put) or O(mode=copy).
      - Ignored when O(mode) is neither V(put) nor V(copy).
    default: true
    type: bool
  encryption_mode:
    description:
      - The encryption mode to use if O(encrypt=true).
    default: AES256
    choices:
      - AES256
      - aws:kms
    type: str
  expiry:
    description:
      - Time limit (in seconds) for the URL generated and returned by S3/Walrus when performing a
        O(mode=put) or O(mode=geturl) operation.
      - Ignored when O(mode) is neither V(put) nor V(geturl).
    default: 600
    aliases: ['expiration']
    type: int
  headers:
    description:
      - Custom headers to use when O(mode=put) as a dictionary of key value pairs.
      - Ignored when O(mode) is not V(put).
    type: dict
  marker:
    description:
      - Specifies the key to start with when using list mode. Object keys are returned in
        alphabetical order, starting with key after the marker in order.
    type: str
  max_keys:
    description:
      - Max number of results to return when O(mode=list)
      - When not set, B(all) keys will be returned.
      - Ignored when O(mode) is not V(list).
    type: int
  metadata:
    description:
      - Metadata to use when O(mode=copy), O(mode=create) or O(mode=put) as a dictionary of key value pairs.
    type: dict
  mode:
    description:
      - Switches the module behaviour between
      - 'V(put): upload'
      - 'V(get): download'
      - 'V(geturl): return download URL'
      - 'V(getstr): download object as string'
      - 'V(list): list keys'
      - 'V(create): create bucket directories'
      - 'V(delobj): delete object'
      - 'V(copy): copy object that is already stored in another bucket'
      - Support for creating and deleting buckets was removed in release 6.0.0.
        To create and manage the bucket itself please use the M(amazon.aws.s3_bucket) module.
      - Support for V(list) has been deprecated and will be removed in a release after 2026-11-01.
        The M(amazon.aws.s3_object_info) module should be used instead of V(list).
    required: true
    choices: ['get', 'put', 'create', 'geturl', 'getstr', 'delobj', 'list', 'copy']
    type: str
  object:
    description:
      - Key name of the object inside the bucket.
      - Can be used to create "virtual directories", see examples.
      - Object key names should not include the leading C(/), see
        U(https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html) for more
        information.
      - Support for passing the leading V(/) has been deprecated and will be removed
        in a release after 2025-12-01.
    type: str
  sig_v4:
    description:
      - Forces the Boto SDK to use Signature Version 4.
      - Only applies to get modes, O(mode=get), O(mode=getstr), O(mode=geturl).
    default: true
    type: bool
    version_added: 5.0.0
  permission:
    description:
      - This option lets the user set the canned permissions on the object/bucket that are created.
        The permissions that can be set are V(private), V(public-read), V(public-read-write),
        V(authenticated-read) for a bucket or V(private), V(public-read), V(public-read-write),
        V(aws-exec-read), V(authenticated-read), V(bucket-owner-read), V(bucket-owner-full-control)
        for an object. Multiple permissions can be specified as a list; although only the first one
        will be used during the initial upload of the file.
      - For a full list of permissions see the AWS documentation
        U(https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl).
    default: ['private']
    choices:
      - "private"
      - "public-read"
      - "public-read-write"
      - "aws-exec-read"
      - "authenticated-read"
      - "bucket-owner-read"
      - "bucket-owner-full-control"
    type: list
    elements: str
  prefix:
    description:
      - Limits the response to keys that begin with the specified prefix for list mode.
    default: ""
    type: str
  version:
    description:
      - Version ID of the object inside the bucket. Can be used to get a specific version of a file
        if versioning is enabled in the target bucket.
    type: str
  overwrite:
    description:
      - Force overwrite either locally on the filesystem or remotely with the object/key.
      - Used when O(mode=put) or O(mode=get).
      - Ignored when when O(mode) is neither V(put) nor V(get).
      - Must be a Boolean, V(always), V(never), V(different) or V(latest).
      - V(true) is the same as V(always).
      - V(false) is equal to V(never).
      - When this is set to C(different) the MD5 sum of the local file is compared with the 'ETag'
        of the object/key in S3.  The ETag may or may not be an MD5 digest of the object data. See
        the ETag response header here
        U(https://docs.aws.amazon.com/AmazonS3/latest/API/RESTCommonResponseHeaders.html).
      - When O(mode=get) and O(overwrite=latest) the last modified timestamp of local file
        is compared with the 'LastModified' of the object/key in S3.
    default: 'different'
    aliases: ['force']
    type: str
  retries:
    description:
     - On recoverable failure, how many times to retry before actually failing.
    default: 0
    type: int
    aliases: ['retry']
  dualstack:
    description:
      - Enables Amazon S3 Dual-Stack Endpoints, allowing S3 communications using both IPv4 and IPv6.
      - Support for passing O(dualstack) and O(endpoint_url) at the same time has been deprecated,
        the dualstack endpoints are automatically configured using the configured O(region).
        Support will be removed in a release after 2024-12-01.
    type: bool
    default: false
  ceph:
    description:
      - Enable API compatibility with Ceph RGW.
      - It takes into account the S3 API subset working with Ceph in order to provide the same module
        behaviour where possible.
      - Requires O(endpoint_url) if O(ceph=true).
    aliases: ['rgw']
    default: false
    type: bool
  src:
    description:
      - The source file path when performing a C(put) operation.
      - One of O(content), O(content_base64) or O(src) must be specified when O(mode=put)
        otherwise ignored.
    type: path
  content:
    description:
      - The content to C(put) into an object.
      - The parameter value will be treated as a string and converted to UTF-8 before sending it to
        S3.
      - To send binary data, use the O(content_base64) parameter instead.
      - One of O(content), O(content_base64) or O(src) must be specified when O(mode=put)
        otherwise ignored.
    version_added: "1.3.0"
    type: str
  content_base64:
    description:
      - The base64-encoded binary data to C(put) into an object.
      - Use this if you need to put raw binary data, and don't forget to encode in base64.
      - One of O(content), O(content_base64) or O(src) must be specified when O(mode=put)
        otherwise ignored.
    version_added: "1.3.0"
    type: str
  ignore_nonexistent_bucket:
    description:
      - Overrides initial bucket lookups in case bucket or IAM policies are restrictive.
      - This can be useful when a user may have the C(GetObject) permission but no other
        permissions. In which case using O(mode=get) will fail unless
        O(ignore_nonexistent_bucket=true) is specified.
    type: bool
    default: false
  encryption_kms_key_id:
    description:
      - KMS key id to use when encrypting objects using O(encryption_mode=aws:kms).
      - Ignored if O(encryption_mode) is not V(aws:kms).
    type: str
  copy_src:
    description:
    - The source details of the object to copy.
    - Required if O(mode=copy).
    type: dict
    version_added: 2.0.0
    suboptions:
      bucket:
        type: str
        description:
        - The name of the source bucket.
        required: true
      object:
        type: str
        description:
        - key name of the source object.
        - if not specified, all the objects of the O(copy_src.bucket) will be copied into the specified bucket.
        required: false
      version_id:
        type: str
        description:
        - version ID of the source object.
      prefix:
        description:
        - Copy all the keys that begin with the specified prefix.
        - Ignored if O(copy_src.object) is supplied.
        default: ""
        type: str
        version_added: 6.2.0
  validate_bucket_name:
    description:
      - Whether the bucket name should be validated to conform to AWS S3 naming rules.
      - On by default, this may be disabled for S3 backends that do not enforce these rules.
      - See the Amazon documentation for more information about bucket naming rules
        U(https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).
    type: bool
    version_added: 3.1.0
    default: True
  expected_bucket_owner:
    description:
      - The account ID of the expected bucket owner.
      - If the account ID that you provide does not match the actual owner of the bucket,
        the request fails with the HTTP status code 403 Forbidden (access denied).
    type: str
    version_added: 8.2.0
author:
  - "Lester Wade (@lwade)"
  - "Sloane Hertel (@s-hertel)"
  - "Alina Buzachis (@alinabuzachis)"
notes:
  - Support for O(tags) and O(purge_tags) was added in release 2.0.0.
  - In release 5.0.0 the O(s3_url) parameter was merged into the O(endpoint_url) parameter,
    I(s3_url) remains as an alias for O(endpoint_url).
  - For Walrus O(endpoint_url) should be set to the FQDN of the endpoint with neither scheme nor path.
  - Support for the E(S3_URL) environment variable has been
    deprecated and will be removed in a release after 2024-12-01, please use the O(endpoint_url) parameter
    or the E(AWS_URL) environment variable.
  - Support for creating and deleting buckets was removed in release 6.0.0.
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.tags
  - amazon.aws.boto3
a|	  
- name: Simple PUT operation
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    src: /usr/local/myfile.txt
    mode: put

- name: PUT operation from a rendered template
  amazon.aws.s3_object:
    bucket: mybucket
    object: /object.yaml
    content: "{{ lookup('template', 'templates/object.yaml.j2') }}"
    mode: put

- name: Simple PUT operation in Ceph RGW S3
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    src: /usr/local/myfile.txt
    mode: put
    ceph: true
    endpoint_url: "http://localhost:8000"

- name: Simple GET operation
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    dest: /usr/local/myfile.txt
    mode: get

- name: Get a specific version of an object.
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    version: 48c9ee5131af7a716edc22df9772aa6f
    dest: /usr/local/myfile.txt
    mode: get

- name: PUT/upload with metadata
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    src: /usr/local/myfile.txt
    mode: put
    metadata:
      Content-Encoding: gzip
      Cache-Control: no-cache

- name: PUT/upload with custom headers
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    src: /usr/local/myfile.txt
    mode: put
    headers: 'x-amz-grant-full-control=emailAddress=owner@example.com'

- name: List keys simple
  amazon.aws.s3_object:
    bucket: mybucket
    mode: list

- name: List keys all options
  amazon.aws.s3_object:
    bucket: mybucket
    mode: list
    prefix: /my/desired/
    marker: /my/desired/0023.txt
    max_keys: 472

- name: GET an object but don't download if the file checksums match. New in 2.0
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    dest: /usr/local/myfile.txt
    mode: get
    overwrite: different

- name: Delete an object from a bucket
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    mode: delobj

- name: Copy an object already stored in another bucket
  amazon.aws.s3_object:
    bucket: mybucket
    object: /my/desired/key.txt
    mode: copy
    copy_src:
      bucket: srcbucket
      object: /source/key.txt

- name: Copy all the objects with name starting with 'ansible_'
  amazon.aws.s3_object:
    bucket: mybucket
    mode: copy
    copy_src:
      bucket: srcbucket
      prefix: 'ansible_'
a  
msg:
  description: Message indicating the status of the operation.
  returned: always
  type: str
  sample: "PUT operation complete"
url:
  description: URL of the object.
  returned: (for put and geturl operations)
  type: str
  sample: "https://my-bucket.s3.amazonaws.com/my-key.txt?AWSAccessKeyId=<access-key>&Expires=1506888865&Signature=<signature>"
expiry:
  description: Number of seconds the presigned url is valid for.
  returned: (for geturl operation)
  type: int
  sample: 600
contents:
  description: Contents of the object as string.
  returned: (for getstr operation)
  type: str
  sample: "Hello, world!"
s3_keys:
  description: List of object keys.
  returned: (for list operation)
  type: list
  elements: str
  sample:
  - prefix1/
  - prefix1/key1
  - prefix1/key2
tags:
    description: Tags of the s3 object.
    returned: always
    type: dict
    sample: {
                "Owner": "dev001",
                "env": "test"
            }
    N)SSLError)	to_native)is_boto3_error_code)is_boto3_error_message)AnsibleAWSModule)AWSRetry)HAS_MD5)calculate_etag)calculate_etag_content)list_bucket_object_keys)s3_extra_params)validate_bucket_name)ansible_dict_to_boto3_tag_list)boto3_tag_list_to_ansible_dictXNotImplementedNotImplementedc                       e Zd Zy)Sigv4RequiredN)__name__
__module____qualname__     h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/s3_object.pyr   r     s    r   r   c                         e Zd Zd fd	Z xZS )S3ObjectFailurec                 @    t         |   |       || _        || _        y N)super__init__
original_emessage)selfr"   r!   	__class__s      r   r    zS3ObjectFailure.__init__  s    !$r   NN)r   r   r   r    __classcell__)r$   s   @r   r   r     s     r   r   c                    	 |r|j                  d|||       y|j                  d||       	 y# t        d      $ r Y yt        d      $ r%}|du r| j                  |d| d	       Y d }~yd }~wt        j                  j
                  t        j                  j                  t        j                  j                  f$ r}t        d| d|      d }~ww xY w)
NT	aws_retryBucketKey	VersionIdr)   r*   r+   404F403z2Failed while looking up object (during key check) .msg)
head_objectr   fail_json_awsbotocore
exceptionsBotoCoreErrorClientErrorboto3
Boto3Errorr   )modules3bucketobjversionvalidatees          r   	key_checkrB     s    ^NNT&cWNU$ ! NNT&cNB   u% u% t  HQO !   	))''## ^
  RSVRWWXY[\]]^s,   0 0 C CA--AC CCc           	          t        ||||      j                  d      }|t        | ||||||      }||k(  S t        | ||||||      }||k(  S )Nr?   ETag)_head_objectgetr
   r   )	r;   r<   r=   r>   r?   
local_filecontents3_etag
local_etags	            r   etag_comparerL     sl    2vsG<@@HG#FJVSRYZ
 j   ,FGWb&RUW^_
j  r   c                     	 |r| j                  d|||      }n| j                  d||      }|si S |j                  d       |S # t        d      $ r i cY S w xY w)NTr(   r-   ResponseMetadatar.   )r3   popr   )r<   r=   r>   r?   obj_heads        r   rF   rF     sh    
~~VX_~`H~~V~MHI'(u% 	s   /A A AAc                 \   	 |r(|j                  d|||      d   j                         }|S |j                  d||      d   j                         }|S # t        ddg      $ r!}| j                  |d| d	       Y d }~y d }~wt	        d
      $ r t               t        d      $ r!}| j                  |d| d	       Y d }~y d }~wt        j                  j                  t        j                  j                  t        j                  j                  f$ r}t        d| d|      d }~ww xY w)NTr(   Bodyr-   r.   r/   zCould not find the key r0   r1   zrequire AWS Signature Version 4InvalidArgument)
get_objectreadr   r4   r   r   r5   r6   r7   r8   r9   r:   r   )r;   r<   r=   r>   r?   contentsrA   s          r   _get_object_contentrW     s6   C}}tFW^}_`fgllnH  }}tF}LVTYY[Hu~. F 	Q&=cU!$DEE!"CD o01 FQ&=cU!$DEE))''## C
  7uA>BBCs5   )A &A D+#A??(D+'CAD+D&&D+c                 P    d }t        | |||      }|r|d   j                         }|S )NLastModified)rF   	timestamp)r<   r=   r>   r?   last_modifiedobj_infos         r   get_s3_last_modified_timestampr]     s3    MBW5H 0::<r   c                     t        | |||      }t        j                  j                  |      syt        j                  j	                  |      }||k  S )NF)r]   ospathexistsgetmtime)r<   r=   r>   r?   rH   s3_last_modifiedlocal_last_modifieds          r   is_local_object_latestre   !  sG    5b&#wO77>>*%''**:6222r   c                    	 |j                  d|       y # t        d      $ r}|rt        d| d|      Y d }~y d }~wt        d      $ r}|rt        d| d|      Y d }~y d }~wt        j                  j
                  t        j                  j                  t        j                  j                  f$ r}t        d	| d|      d }~ww xY w)
NTr)   r*   r.   zBucket 'z' not found (during bucket_check).  Support for automatically creating buckets was removed in release 6.0.0.  The amazon.aws.s3_bucket module can be used to create buckets.r/   z$Permission denied accessing bucket 'z' (during bucket_check).z Failed while looking up bucket ')	head_bucketr   r   r5   r6   r7   r8   r9   r:   )r;   r<   r=   r@   rA   s        r   bucket_checkri   )  s    

f5u% 	!vh 'U U    u% !6vh>VW   	))''## 

 .vh6NO
 	

s+    C;CA##AC6CCc                     	 t        | ||||      S # t        j                  j                  t        j                  j                  t
        j                  j                  f$ r}t        d| |      d }~ww xY w)N)prefixstart_aftermax_keysz,Failed while listing the keys in the bucket )r   r5   r6   r8   r7   r9   r:   r   )r<   r=   rk   markerrm   rA   s         r   	list_keysro   G  su    Z&r6&f_ghh''))## Z
  LVHUWXYYZs    AA6"A11A6c                 j   | j                   r| j                  dd       	 |j                  d||       | j                  d| dd       y # t        j                  j
                  t        j                  j                  t        j                  j                  f$ r}t        d| d|      d }~ww xY w)Nz0DELETE operation skipped - running in check modeTr2   changedr-   Object deleted from bucket r0   zFailed while trying to delete )

check_mode	exit_jsondelete_objectr5   r6   r8   r7   r9   r:   r   )r;   r<   r=   r>   rA   s        r   
delete_keyrw   R  s    B 	 	
J
4C@:6(!DdS''))## J
  >se1EqIIJs   +A AB2B--B2c                    	 |r |j                   d
ddi| | j                  j                  d      D ]  }|j                  d|||        y # t	        t
              $ r | j                  d       Y y t	        d      $ r | j                  d       Y y t        j                  j                  t        j                  j                  t        j                  j                  f$ r}t        d| d	|      d }~ww xY w)Nr)   T
permission)r)   ACLr*   r+   zPutObjectAcl is not implemented by your storage provider. Set the permissions parameters to the empty list to avoid this warningAccessControlListNotSupportedz8PutObjectAcl operation : The bucket does not allow ACLs.zFailed while creating object r0   r   )
put_objectparamsrG   put_object_aclr   IGNORE_S3_DROP_IN_EXCEPTIONSwarnr5   r6   r7   r8   r9   r:   r   )r;   r<   r=   r>   r}   aclrA   s          r   r~   r~   c  s    IBMM3D3F3==$$\2 	OC#f#N	O;< 
%	
 >? PNO))''## I
  =cU!DaHHIs%   AA #C73C7AC7"C22C7c           	      ~   | j                   r| j                  dd       ||dd}|j                  t        || j                  j                  d      | j                  j                  d      |             t        | ||||       t        || ||      \  }}	t        ||||      }
| j                  d| d	| |
|d
       y )N-PUT operation skipped - running in check modeTrq   r   )r*   r+   rR   encryption_modeencryption_kms_key_idzVirtual directory z created in bucket r2   urltagsrr   )	rt   ru   updateget_extra_paramsr}   rG   r~   ensure_tagsput_download_url)r;   r<   r=   r>   encryptexpirymetadatar}   r   _changedr   s              r   create_dirkeyr   x  s    LVZ[sC8F
MMMM/0MM56		
 62vsF3 !VVS9ND(
2vsF
3C
 %8A	  r   c                 R    t        t        j                  j                  |             S r   )boolr_   r`   ra   )r`   s    r   
path_checkr     s    t$%%r   c                 @    | rt        j                  |       d   }|r|S y)Nr   zbinary/octet-stream)	mimetypes
guess_type)srccontent_types     r   guess_content_typer     s(    
 ++C03 !r   c                     i }| r||d<   |r
|dk(  r||d<   |r-i |d<   |D ]#  }t        |      }|r	||   ||<   ||   |d   |<   % |S )NServerSideEncryptionaws:kmsSSEKMSKeyIdMetadata)option_in_extra_args)r   r   r   r   extraoptionextra_args_options          r   r   r     s     E(7$%I!=4mj 	=F 4V < +3F+;'(,4V,<j!&)	= Lr   c                     | j                  dd      j                         }i ddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(}||v r||   S y ))N- r   rz   cachecontrolCacheControlcontentdispositionContentDispositioncontentencodingContentEncodingcontentlanguageContentLanguagecontenttypeContentTypeexpiresExpiresgrantfullcontrolGrantFullControl	grantread	GrantReadgrantreadacpGrantReadACPgrantwriteacpGrantWriteACPr   r   requestpayerRequestPayerserversideencryptionr   storageclassStorageClassssecustomeralgorithmSSECustomerAlgorithmssecustomerkeySSECustomerKeySSECustomerKeyMD5r   WebsiteRedirectLocation)ssecustomerkeymd5ssekmskeyidwebsiteredirectlocation)replacelower)r   temp_optionallowed_extra_argss      r   r   r     s   ..b)//1Ku 	2 	,	
 	, 	} 	9 	. 	[ 	 	 	J 	 	 6 	  	 6!" 	*#$ 1$#<). ((!+.. )r   c                 p   | j                   r| j                  dd       	 t        || j                  j	                  d      | j                  j	                  d      |      }| j                  j	                  d      r=| j                  d   }t        |t              r||d<   nt        |t              r|d   |d<   d	|vrt        |      |d	<   |r|j                  d||||
       n+t        j                  |	      }|j                  d||||       |
st)        | |||       t+        || ||      \  }}t-        ||||      }| j                  d||d       y # t        j                  j                  t        j                  j                   t"        j                  j$                  f$ r}t'        d|      d }~ww xY w)Nr   Trq   r   r   ry   rz   r   r   )r)   Filenamer*   r+   	ExtraArgs)r)   Fileobjr*   r+   r   z!Unable to complete PUT operation.zPUT operation completer   )rt   ru   r   r}   rG   
isinstancestrlistr   upload_fileioBytesIOupload_fileobjr5   r6   r8   r7   r9   r:   r   r~   r   r   )r;   r<   r=   r>   r   r   r   headersr   rI   acl_disabledr   permissionsfrA   r   r   r   s                     r   upload_s3filer     s    LVZ[F MM/0MM56	
 ==\* --5K+s+*eK.*1~e%#5c#:E- NNTCC[`Na

7#AaC[`a vr63/ !VVS9ND(
2vsF
3C
1stT 	''))## F
 A1EEFs   C.E AF5$F00F5c                 
   | j                   r| j                  dd       |rdd|iini }t        d|dz         D ]-  }	  |j                  |||fddi| | j                  d	d       / y # t        j
                  j                  t        j
                  j                  t        j
                  j                  f$ r}	||k\  rt        d
| d|	      Y d }	~	d }	~	wt        $ r"}	||k\  r| j                  |	d       Y d }	~	d }	~	ww xY w)N-GET operation skipped - running in check modeTrq   r   r,   r      r)   GET operation completezFailed while downloading r0   zs3 download failedr1   )rt   ru   rangedownload_filer5   r6   r8   r7   r9   r:   r   r   r4   )
r;   r<   r=   r>   destretriesr?   optional_kwargsxrA   s
             r   download_s3filer     s   LVZ[?F{['$:;BO1gk" B	BBVS$R$R/R!94HB
 ++--''
 	M G|%(A#a&H!LL   	BG|$$Q,@$A	Bs$   *A**AD:CD C==Dc           	          | j                   r| j                  dd       t        t        | ||||            }| j                  d|d       y )Nr   Trq   r   )r2   rV   rr   )rt   ru   r   rW   )r;   r<   r=   r>   r?   rV   s         r   download_s3strr   &  sN    LVZ[,VRgNOH
1HdSr   c                 (   	 |j                  d||d|      }| j                  d||||       y # t        j                  j                  t        j                  j
                  t        j                  j                  f$ r}t        d|      d }~ww xY w)NrT   r*   r+   ClientMethodParams	ExpiresInzDownload url:)r2   r   r   r   rr   z"Failed while getting download url.)	generate_presigned_urlru   r5   r6   r8   r7   r9   r:   r   )	r;   r<   r=   r>   r   r   rr   r   rA   s	            r   get_download_urlr   -  s    G''%$S1	 ( 
 	 	 	
 	''))## G
 BAFFGs   -0 AB BBc                     	 | j                  d||d|      }|S # t        j                  j                  t        j                  j                  t
        j                  j                  f$ r}t        d|      d }~ww xY w)Nr|   r   r   z Unable to generate presigned URL)r   r5   r6   r8   r7   r9   r:   r   )r<   r=   r>   r   r   rA   s         r   r   r   D  s    E''%$S1	 ( 
 J 	''))## E
 @!DDEs    AA<+A77A<c                 N   ||d}| j                   j                  d      r| j                   d   |d<   |r||d<   	  |j                  dddi|j                  d      }t        |      S # t        t              $ r | j                  d       i cY S t        d	d
g      $ r i cY S w xY w)Nr   expected_bucket_ownerExpectedBucketOwnerr,   r)   TTagSetz=GetObjectTagging is not implemented by your storage provider.NoSuchTagSetNoSuchTagSetErrorr   )r}   rG   get_object_taggingr   r   r   r   )r;   r<   r=   r>   r?   r}   current_tagss          r   get_current_object_tags_dictr   V  s    s+F}}01(.6M(N$%%{,r,,FtFvFJJ8T *,77 ;< ST	0CDE 	s   #A) )%B$B$#B$x   NoSuchBucketOperationAborted)	max_delaycatch_extra_error_codesc                 B    | j                  ||dt        |      i       y )Nr   )r*   r+   Tagging)put_object_taggingr   )r<   r=   r>   r   s       r   r  r  h  s)    9$?@  r   c                 *    | j                  ||       y )Nr   )delete_object_tagging)r<   r=   r>   s      r   r  r  q  s    F4r   c                 r   t        dd      D ]0  }	 t        | ||||      }||k7  rt        j                  d       .|c S  | j                  d|       y # t        j                  j                  t        j                  j
                  t        j                  j                  f$ r}t        d|      d }~ww xY w)Nr      Failed to get object tags.   z1Object tags failed to apply in the expected time.)r2   requested_tags	live_tags)r   r   r5   r6   r8   r7   r9   r:   r   timesleep	fail_json)	r;   r<   r=   r>   expected_tags_dictr?   _dummycurrent_tags_dictrA   s	            r   wait_tags_are_appliedr  v  s    2, %	C <VRQTV] ^  22JJqM$$% ?)#   ++--''
 	C
 "">BB	Cs   AAB6%B11B6c                    |j                   j                  d      }|j                   j                  d      }d}	 t        || ||      }|||fS |s#|j                         }	|	j                  |       |	}||k(  r||fS |r	 t        | |||       n	 t        | ||       t        || |||      }d}||fS # t        j                  j
                  t        j                  j                  t        j                  j                  f$ r}t        d|      d }~ww xY w# t        j                  j
                  t        j                  j                  t        j                  j                  f$ r}t        d|      d }~ww xY w# t        j                  j
                  t        j                  j                  t        j                  j                  f$ r}t        d|      d }~ww xY w)Nr   
purge_tagsFr  zFailed to update object tags.zFailed to delete object tags.T)r}   rG   r   r5   r6   r7   r8   r9   r:   r   copyr   r  r  r  )
clientr;   r=   r>   r   r  rr   r  rA   current_copys
             r   r   r     s   ==V$D""<0JG?8QTU | '))(--/D! D  '))	FvvsD9	F!&&#6 .fffc4PGg%%U 	))''## ?
 :A>>?2 --++''
 	F
 ""A1EE	F --++''
 	F
 ""A1EE	FsJ   B1 ?D E9 1ADDDAE6%E11E69AG	GGc                     d }| j                  d      r| d   j                  d      }| j                  d      rt        j                  | d         }|S )NrI   zutf-8content_base64)rG   encodebase64standard_b64decode)s3_vars
bincontents     r   get_binary_contentr    sQ    J{{9Y'..w7
{{#$..w7G/HI
r   c                    | j                   j                  d      r|}t        | ||d   |d   |d   |d         }|s=|d   r| j                  d|d    d|d    d	
       | j                  d|d    d	
       |d   rt	        |d         r|d   dk7  r|d   dk(  r| j                  dd       |d   dk(  r0t        | ||d   |d   |d   |d         r| j                  dd       |d   dk(  r/t        ||d   |d   |d   |d         r| j                  dd       	 t        | ||d   |d   |d   |d   |d          | j                  d       y # t        $ r$ t        | ||d   |d   |d   |d   |d          Y ?w xY w)Nsig_v4r=   objectr?   r@   r?   r@   Key  with version id  does not exist.r1   r   	overwritealwaysneverz6Local object already exists and overwrite is disabled.Frq   	different)r?   rH   zYLocal and remote object are identical, ignoring. Use overwrite=always parameter to force.latestzKLocal object is latest, ignoreing. Use overwrite=always parameter to force.r   rD   r>   failed)
r}   rG   rB   r  r   ru   rL   re   r   r   )r;   
connectionconnection_v4r  keyrtns        r   s3_object_do_getr1    s'   }}""
	"$F 94(9'::KGT]L^K__o!pqtGH$5#66FGHv:gfo67;;OS[;[;7*L   ;;.<HHI&v4
 o   ;8+0FHHI&v1
 a  

HHFOII&	
( E"  	
HENFOII&	
	
s    !E *F Fc                 J   | j                   j                  d      dk(  r|}|d   't        |d         s| j                  d|d    d       t	        | ||d   |d   |d	   |d
         }t        |      }|r`|d   dk7  rX|d   dk(  st        | ||d   |d   |d	   |d   |      r2t        || |d   |d         \  }}t        | ||d   |d   |d   ||       |d   s|d   |d<   t        | ||d   |d   |d   |d   |d   |d   |d   ||d          | j                  d       y )Nr   r   r   zLocal object "z"" does not exist for PUT operationr1   r=   r"  r?   r@   r#  r'  r(  r)  )r?   rH   rI   r   )rr   r   
object_aclry   r   r   r   )r   rI   r   Fr,  )r}   rG   r   r  rB   r  rL   r   r   r   ru   )r;   r.  r/  r  r0  r  r   tags_updates           r   s3_object_do_putr5    s    }}*+y8"
u~!*WU^*D~gen-==_`a	"$F $G,J'+&(2;7*lHHI&u~/
 !,J@QSZ[cSd eD+!!!# >" ' 5
		EN^, E"r   c                     |j                  dd       s| j                  d       y |d   r/t        | ||d   |d         r| j                  d|d    dd       y | j                  d	       y )
Nr"  zobject parameter is requiredr1   r=   rs   r0   Trq   zBucket parameter is required.)rG   r  rw   ru   r;   r.  r/  r  s       r   s3_object_do_delobjr8  S  s    ;;x&;<		z&*gh>OQXYaQbc-gh.?-@B 	 	

 	<=r   c                 `    t        ||d   |d   |d   |d         }| j                  d|       y )Nr=   rk   rn   rm   zLIST operation complete)r2   s3_keys)ro   ru   )r;   r.  r/  r  keyss        r   s3_object_do_listr<  `  sE    
D 2DAr   c           
         |d   j                  d      s|d   dz   |d<   t        | ||d   |d         r | j                  d|d    d|d    dd       |d	   s|d
   |d<   t        | ||d   |d   |d   |d   |d          y )Nr"  /r=   zBucket z	 and key z already exists.Frq   r   r3  ry   r   r   r   )endswithrB   ru   r   r7  s       r   s3_object_do_creater@  m  s     8%%c*#H-3WX%68IJ'(+,Igh6G5HHXY 	 	
 >" ' 5	
r   c           	         | j                   j                  d      r|}t        | ||d   |d   |d   |d         r2t        | ||d   |d   |d         }t	        | ||d   |d   |d   |       | j                  d	|d    d
       y )Nr!  r=   r"  r?   r@   r#  rD   r   r$  r&  r1   )r}   rG   rB   r   r   r  )r;   r.  r/  r  r   s        r   s3_object_do_geturlrB    s    }}""
	"$ ,HHI&
 	HHH	
 4 122BCDr   c           	         | j                   j                  d      r|}|d   r}|d   rwt        | ||d   |d   |d   |d         r	 t        | ||d   |d   |d          y |d   r | j                  d|d    d	|d    d
       y | j                  d|d    d
       y y y # t        $ r t        | ||d   |d   |d          Y y w xY w)Nr!  r=   r"  r?   r@   r#  rD   r$  r%  r&  r1   )r}   rG   rB   r   r   r  r7  s       r   s3_object_do_getstrrD    s   }}""
xWX.HHI&Z(
H%H%#I. Y4(9'::KGT]L^K__o!pq4(9'::J!KL9 /" ! !H%H%#I.s   B! !"CCc                     | j                   j                  d      }| j                   j                  d      }d}|r&t        | |||      }|s|j                  |       ||k7  }|S )Nr   r  F)r}   rG   r   r   )r;   r.  r=   r>   r   r  diffr  s           r   check_object_tagsrG    sd    ==V$D""<0JD8VUXY$$T* D(Kr   c                 P    |}d|v rt        | ||||      }t        | |d |||      }|S )Nr   )rW   r   )r;   r<   r=   r>   	head_etagr?   etagrV   s           r   calculate_object_etagrK    s;    D
d{ 'vr63H%fhb&#NKr   c
           	         	 t        | ||||	|      s| j                  d| d| dd       t        ||||	      }
t        |||      }d}d }|rct        | ||||
j	                  d	      |	      }t        | ||||j	                  d	            }||k7  rd}d
}|r|r||j	                  d      k7  r	d}d}nd}d}|rU| j
                  r)t        | |||      }i }|r|j                  ddi       ||fS t        || ||      \  }}ddi}|rd|d}||fS | j
                  rdd|ifS d}||d}|	r|j                  d|	i       t        || j                  j	                  d      | j                  j	                  d      |      }|r|j                  ddi       |j                  ||||d       t        | |||       t        || ||      \  }}d| d| d}|||dfS # t        j                  j                  t        j                  j                   t"        j                  j$                  f$ r4}t'        d| d| j                  d   j	                  d       d|      d }~ww xY w) Nr#  r$  z does not exist in bucket r0   Frq   rD   TrE   z'ETag from source and destination differr   z?Would have update object Metadata if not running in check mode.z4Would have copy object if not running in check mode.r2   z;Would have update object tags if not running in check mode.z-ETag from source and destination are the sameztags successfully updated.)r2   r   r   r,   r   r   MetadataDirectiveREPLACE)r   r)   zObject copied from bucket z to bucket zFailed while copying object z from bucket copy_srcr*   )rB   ru   rF   rK  rG   rt   rG  r   r   r   r}   r  r~   r5   r6   r7   r8   r9   r:   r   )r;   r<   r=   r>   r   r   r@   
src_bucketsrc_obj	versionId
s_obj_info
d_obj_infodo_matchdiff_msgsrc_etagdst_etagrr   resultr   	bucketsrc
extra_argstags_updatedr2   rA   s                           r   copy_object_to_bucketr]    s   V
Z)V^_7)#=j\K  
 ""j'9M
!"fc2
,VRWjnn]cNdfopH,VRjnnU[F\]H8# DHZ^^J5O)O\  HMH  +FBDMM5*g"hi& (FFC@MD'LMF!=tLF?"%***G$I   +y!9:)!!"34!!"9:	J  !!#6	"BC GGIvsjDGQ 62vs3!,R!ED,.zl+fXQOCC666))''## 

 *3%}V]]:=V=Z=Z[c=d<eefg
 	

s,   C*G -G G B5G AI%/IIc                    | j                   j                  d      }|j                  d      s|d   r| j                  d       |j                  d      }|j                  d      st        |||j                  d            }t	        |      dk(  r| j                  d| d	       d
}d}|D ]C  }	t        | ||d   |	|d   |d   |d   ||	|j                  d      
      \  }
}||
z  }||
rdndz  }E d| d|d    d}|r| d|d    d}| j                  ||       y t        | ||d   |d   |d   |d   |d   ||j                  d      |j                  d      
      \  }} | j
                  dd|i| y )NrO  r"  z_A destination object was specified while trying to copy all the objects from the source bucket.r1   r=   rk   r   z0No object found to be copied from source bucket r0   Fr   r   r@   
version_id)rR  r   zobject(s) from buckets 'z' and 'z' are the same.z copied into bucket '')rr   r2   rr   r   )r}   rG   r  ro   lenru   r]  )r;   r.  r/  r  rO  rP  r;  rr   number_keys_updatedkeyupdatedrY  r2   s                r   s3_object_do_copyre  :  s   }}  ,H<<!gh&7q 	 	
 h'J<<! Zh1GHt9>#ST^S__`!ab 	7C3!	"
#
#",,|4OGV wG1Q6	7  )GGH<M;No^())>wx?P>QQRSCc2 0HHIJJLL"ll<0
 	33F3r   c                     t        j                  | j                        }|d   rt        |d          |j	                  d      dk(  rt
        s| j                  d       |j	                  d      dvr| j                  |d         rd|d<   nd	|d<   |d
   rZ|j	                  d      dk(  r| j                  d       |d
   }|j                  d      r|dd  }||d
<   | j                  ddd       |d    |d<   d|d<   |S )Nr   r=   r'  r*  zIoverwrite=different is unavailable: ETag calculation requires MD5 supportr1   r(  r)  r*  r+  r(  r)  r"  modedeletez0Parameter object cannot be used with mode=deleter>  r   zLSupport for passing object key names with a leading '/' has been deprecated.z
2025-12-01
amazon.awsdatecollection_nameignore_nonexistent_bucketr@   Fr   )
r  deepcopyr}   r   rG   r	   r  boolean
startswith	deprecate)r;   variable_dictr>   s      r   populate_paramsrt  o  s+   MM&--0M+,]845%4Whi% .  >>-45)1M+&)0M+& XV$0!STH%>>#ab'C&)M(#^! ,   %22M$N NM*$)M.!r   c                 :   t        | ||d   |d          	 |j                  d|d         d   }|j                  d      r|d   d   d	   }|d
k(  rd|d<   |d   st        |j                  d            |d<   |S # t        j                  j
                  $ r Y Cw xY w)Nr=   r@   )r@   Trg   OwnershipControlsRulesr   ObjectOwnershipBucketOwnerEnforcedr   ry   r3  )ri   get_bucket_ownership_controlsrG   r5   r6   r8   r   )r;   r<   var_dictownership_controlsobject_ownerships        r   validate_bucketr~    s    Xh/(::NO
==U]^fUg=h
 !!'*1':1=>OP#88+/(
 N#!%hll<&@!AO ** s   ?A: :BBc                  	   g d} g d}t        d]i dt        d      dt        d d      d	t        dd
      dt        ddgd      dt        dddg      dt        d      dt               dt        dd      dt        d      dt        | d      dt        dd
      dt               d t        d!d"d#g|$      d%t        d &      d't        d(gd)*      d+t        d,&      d-t        d.gdd/0      d1t        dd
      d2t        dd
d3g      d4t        d      d5t               d6t               d7t        dd
      d8t               d9t        dd:g;      d<t        d
d=      d>t        dt        t        d      t               t        d,&      t               ?      @      dAt        d
d=      dBt        d"      }d2ddCggddDdggddDg dEdgddFdggddGddggddHdggddIdggddJd>ggg}t        |d|g dEgK      }|j                  j                  d      d!k(  r|j	                  dLdMdNO       |j                  j                  dC      }|j                  j                  d1      }|r,|r*|j	                  dPdQdNO       dR|vr|j                  dST       |j                  j                  d'      dUvr|j	                  dVdQdNO       t        |j                  dW      }t        |j                  dW      }t        j                         }		  |j                  d^dX|	i|}
 |j                  d^dX|	i|}t#        |      }|j%                  t'        |
|             t(        t*        t,        t.        t0        t2        t4        t6        dZ}||d      }	  |||
|       |jA                  d\       y # t        j                  j                  t        j                  j                  t        j                  j                  f$ r}|j!                  |dYT       Y d }~d }~ww xY w# t        j                  j8                  $ r}|j!                  |d[T       Y d }~d }~wt:        $ rY}|j<                  r'|j!                  |j<                  |j>                         n|j                  |j>                         Y d }~d }~ww xY w)_N)rG   putcreategeturlgetstrdelobjr   r  )privatezpublic-readzpublic-read-writezaws-exec-readzauthenticated-readzbucket-owner-readzbucket-owner-full-controlr=   T)requiredr   r`   )defaulttyper   r   r   AES256r   )choicesr  r   iX  int
expiration)r  r  aliasesr   dict)r  rn   rm   F)r  no_logr   rh  )r  r  r!  r"  ry   r   r   r  )r  elementsr  r  r?   )r  r'  forcer*  )r  r  rk   r   r   retryr   )r  r  r  	dualstackcephrgwr   rI   r  rn  r   r   resource_tags)r  r  r  )r  r  rO  )r=   r"  rk   r_  )r  optionsr   r   endpoint_urlr  )rI   r  r   r  rG   r  r  r  )argument_specsupports_check_moderequired_ifmutually_exclusivezSupport for 'list' mode has been deprecated and will be removed in a release after 2024-11-01.  Please use the amazon.aws.s3_object_info module instead.z
2026-11-01rj  rk  zlSupport for passing both the 'dualstack' and 'endpoint_url' parameters at the same time has been deprecated.z
2024-12-01zamazonaws.comz dualstack only applies to AWS S3r1   rg  zuSupport for passing values of 'overwrite' other than 'always', 'never', 'different' or 'latest', has been deprecated.)sigv4retry_decoratorzFailed to connect to AWS)rG   r  r  r   r  r  r  r  zInvalid endpoint providedr,  r   )r<   )!r  r   r}   rG   rr  r  r   r   jittered_backoffr  r5   r6   r8   r7   r9   r:   r4   rt  r   r~  r1  r5  r8  r<  r@  rB  rD  re  EndpointConnectionErrorr   r!   r"   ru   )valid_modes
valid_aclsr  r  r;   r  r  extra_paramsextra_params_v4r  r<   s3_v4rA   s3_object_paramsfunc_mappingfuncs                   r   mainr    s<   
 YKJ  &T"&$V,& T/& h	%:HM	&
 Cel^D& &!& v& 5/& 6"& +5& Dv.& v& Vei[R\]& T"& y+>&  B!&" gYUA>#&$ u62%&& %fug>'&( f)&* +&, v-&. #'u6"B/&0 #f1&2 v'893&4 VT25&6 T*vB'6	
7&H "vt<I&J #.K&MR 
'(	
#	<dC	H:&	*+	H:&	H:&	*&	K # @A	F }} F*X ( 	 	
 ==$$^4L!!+.I\, ( 	 	
 ,.!CD}}%-WW@ ( 	 	
 #6==>L%fmm4@O//1O@V]]QQLQWOWW 'v.OFB8HIJ  %!%%%!	L (01D(VR 01 E"= 	''))## @
 	Q$>??@. 66 AQ$?@@ (<<  qyy9QYY'	(s>   (N: P* :AP'
P""P'*SQS+AR??S__main__)NT)NNNr   r%   )T)NNNN)NNF)ODOCUMENTATIONEXAMPLESRETURNr  r  r   r   r_   r  sslr   r9   r5   ImportErroransible.module_utils.basicr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   r   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   6ansible_collections.amazon.aws.plugins.module_utils.s3r	   r
   r   r   r   r   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   r   r   	Exceptionr   r   rB   rL   rF   rW   r]   re   ri   ro   rw   r~   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r  r1  r5  r8  r<  r@  rB  rD  rG  rK  r]  re  rt  r~  r  r   r   r   r   <module>r     s  ObdL&
P   	  	  	  1 \ _ X P J Q Y Z R W f f 13CD 	I 	i 0!C.3
<ZJ"I*6&! 	./N 	4UnB2TG.$8$ S>SeBfg h S>SeBfg5 h5.2&jC#L>#B
>
B4E< MF
W
t24j'T*R#j zF a$  		s   E' 'E0/E0