-
Notifications
You must be signed in to change notification settings - Fork 341
/
s3_object.py
1589 lines (1419 loc) · 55.4 KB
/
s3_object.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = r"""
---
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, I(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 I(mode=get).
- Ignored when I(mode) is not C(get).
type: path
encrypt:
description:
- Asks for server-side encryption of the objects when I(mode=put) or I(mode=copy).
- Ignored when I(mode) is neither C(put) nor C(copy).
default: true
type: bool
encryption_mode:
description:
- The encryption mode to use if I(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
I(mode=put) or I(mode=geturl) operation.
- Ignored when I(mode) is neither C(put) nor C(geturl).
default: 600
aliases: ['expiration']
type: int
headers:
description:
- Custom headers to use when I(mode=put) as a dictionary of key value pairs.
- Ignored when I(mode) is not C(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
default: ''
max_keys:
description:
- Max number of results to return when I(mode=list), set this if you want to retrieve fewer
than the default 1000 keys.
- Ignored when I(mode) is not C(list).
default: 1000
type: int
metadata:
description:
- Metadata to use when I(mode=put) or I(mode=copy) as a dictionary of key value pairs.
type: dict
mode:
description:
- Switches the module behaviour between
- 'C(put): upload'
- 'C(get): download'
- 'C(geturl): return download URL'
- 'C(getstr): download object as string'
- 'C(list): list keys'
- 'C(create): create bucket directories'
- 'C(delobj): delete object'
- 'C(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.
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 C(/) 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, I(mode=get), I(mode=getstr), I(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 C(private), C(public-read), C(public-read-write),
C(authenticated-read) for a bucket or C(private), C(public-read), C(public-read-write),
C(aws-exec-read), C(authenticated-read), C(bucket-owner-read), C(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 I(mode=put) or I(mode=get).
- Ignored when when I(mode) is neither C(put) nor C(get).
- Must be a Boolean, C(always), C(never), C(different) or C(latest).
- C(true) is the same as C(always).
- C(false) is equal to C(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 I(mode=get) and I(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 I(dualstack) and I(endpoint_url) at the same time has been deprecated,
the dualstack endpoints are automatically configured using the configured I(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 I(endpoint_url) if I(ceph=true).
aliases: ['rgw']
default: false
type: bool
src:
description:
- The source file path when performing a C(put) operation.
- One of I(content), I(content_base64) or I(src) must be specified when I(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 I(content_base64) parameter instead.
- One of I(content), I(content_base64) or I(src) must be specified when I(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 I(content), I(content_base64) or I(src) must be specified when I(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 I(mode=get) will fail unless
I(ignore_nonexistent_bucket=true) is specified.
type: bool
default: false
encryption_kms_key_id:
description:
- KMS key id to use when encrypting objects using I(encrypting=aws:kms).
- Ignored if I(encryption) is not C(aws:kms).
type: str
copy_src:
description:
- The source details of the object to copy.
- Required if I(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 I(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 I(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
author:
- "Lester Wade (@lwade)"
- "Sloane Hertel (@s-hertel)"
- "Alina Buzachis (@alinabuzachis)"
notes:
- Support for I(tags) and I(purge_tags) was added in release 2.0.0.
- In release 5.0.0 the I(s3_url) parameter was merged into the I(endpoint_url) parameter,
I(s3_url) remains as an alias for I(endpoint_url).
- For Walrus I(endpoint_url) should be set to the FQDN of the endpoint with neither scheme nor path.
- Support for the C(S3_URL) environment variable has been
deprecated and will be removed in a release after 2024-12-01, please use the I(endpoint_url) parameter
or the C(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
"""
EXAMPLES = r"""
- 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_'
"""
RETURN = r"""
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
"""
import base64
import copy
import io
import mimetypes
import os
import time
from ssl import SSLError
try:
# Beware, S3 is a "special" case, it sometimes catches botocore exceptions and
# re-raises them as boto3 exceptions.
import boto3
import botocore
except ImportError:
pass # Handled by AnsibleAWSModule
from ansible.module_utils.basic import to_native
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_message
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.s3 import HAS_MD5
from ansible_collections.amazon.aws.plugins.module_utils.s3 import calculate_etag
from ansible_collections.amazon.aws.plugins.module_utils.s3 import calculate_etag_content
from ansible_collections.amazon.aws.plugins.module_utils.s3 import s3_extra_params
from ansible_collections.amazon.aws.plugins.module_utils.s3 import validate_bucket_name
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
IGNORE_S3_DROP_IN_EXCEPTIONS = ["XNotImplemented", "NotImplemented"]
class Sigv4Required(Exception):
pass
class S3ObjectFailure(Exception):
def __init__(self, message=None, original_e=None):
super().__init__(message)
self.original_e = original_e
self.message = message
def key_check(module, s3, bucket, obj, version=None, validate=True):
try:
if version:
s3.head_object(aws_retry=True, Bucket=bucket, Key=obj, VersionId=version)
else:
s3.head_object(aws_retry=True, Bucket=bucket, Key=obj)
except is_boto3_error_code("404"):
return False
except is_boto3_error_code("403") as e: # pylint: disable=duplicate-except
if validate is True:
module.fail_json_aws(
e,
msg=f"Failed while looking up object (during key check) {obj}.",
)
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
boto3.exceptions.Boto3Error,
) as e: # pylint: disable=duplicate-except
raise S3ObjectFailure(f"Failed while looking up object (during key check) {obj}.", e)
return True
def etag_compare(module, s3, bucket, obj, version=None, local_file=None, content=None):
s3_etag = get_etag(s3, bucket, obj, version=version)
if local_file is not None:
local_etag = calculate_etag(module, local_file, s3_etag, s3, bucket, obj, version)
else:
local_etag = calculate_etag_content(module, content, s3_etag, s3, bucket, obj, version)
return s3_etag == local_etag
def get_etag(s3, bucket, obj, version=None):
try:
if version:
key_check = s3.head_object(aws_retry=True, Bucket=bucket, Key=obj, VersionId=version)
else:
key_check = s3.head_object(aws_retry=True, Bucket=bucket, Key=obj)
if not key_check:
return None
return key_check["ETag"]
except is_boto3_error_code("404"):
return None
def get_s3_last_modified_timestamp(s3, bucket, obj, version=None):
if version:
key_check = s3.head_object(aws_retry=True, Bucket=bucket, Key=obj, VersionId=version)
else:
key_check = s3.head_object(aws_retry=True, Bucket=bucket, Key=obj)
if not key_check:
return None
return key_check["LastModified"].timestamp()
def is_local_object_latest(s3, bucket, obj, version=None, local_file=None):
s3_last_modified = get_s3_last_modified_timestamp(s3, bucket, obj, version)
if not os.path.exists(local_file):
return False
local_last_modified = os.path.getmtime(local_file)
return s3_last_modified <= local_last_modified
def bucket_check(module, s3, bucket, validate=True):
try:
s3.head_bucket(aws_retry=True, Bucket=bucket)
except is_boto3_error_code("404") as e:
if validate:
raise S3ObjectFailure(
(
f"Bucket '{bucket}' 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."
),
e,
)
except is_boto3_error_code("403") as e: # pylint: disable=duplicate-except
if validate:
raise S3ObjectFailure(
f"Permission denied accessing bucket '{bucket}' (during bucket_check).",
e,
)
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
boto3.exceptions.Boto3Error,
) as e: # pylint: disable=duplicate-except
raise S3ObjectFailure(
f"Failed while looking up bucket '{bucket}' (during bucket_check).",
e,
)
@AWSRetry.jittered_backoff()
def paginated_list(s3, **pagination_params):
pg = s3.get_paginator("list_objects_v2")
for page in pg.paginate(**pagination_params):
for data in page.get("Contents", []):
yield data["Key"]
def paginated_versioned_list_with_fallback(s3, **pagination_params):
try:
versioned_pg = s3.get_paginator("list_object_versions")
for page in versioned_pg.paginate(**pagination_params):
delete_markers = [
{"Key": data["Key"], "VersionId": data["VersionId"]} for data in page.get("DeleteMarkers", [])
]
current_objects = [
{"Key": data["Key"], "VersionId": data["VersionId"]} for data in page.get("Versions", [])
]
yield delete_markers + current_objects
except is_boto3_error_code(IGNORE_S3_DROP_IN_EXCEPTIONS + ["AccessDenied"]):
for key in paginated_list(s3, **pagination_params):
yield [{"Key": key}]
def list_keys(s3, bucket, prefix=None, marker=None, max_keys=None):
pagination_params = {
"Bucket": bucket,
"Prefix": prefix,
"StartAfter": marker,
"MaxKeys": max_keys,
}
pagination_params = {k: v for k, v in pagination_params.items() if v}
try:
return list(paginated_list(s3, **pagination_params))
except (
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
boto3.exceptions.Boto3Error,
) as e:
raise S3ObjectFailure(f"Failed while listing the keys in the bucket {bucket}", e)
def delete_key(module, s3, bucket, obj):
if module.check_mode:
module.exit_json(
msg="DELETE operation skipped - running in check mode",
changed=True,
)
try:
s3.delete_object(aws_retry=True, Bucket=bucket, Key=obj)
module.exit_json(msg=f"Object deleted from bucket {bucket}.", changed=True)
except (
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
boto3.exceptions.Boto3Error,
) as e:
raise S3ObjectFailure(f"Failed while trying to delete {obj}.", e)
def put_object_acl(module, s3, bucket, obj, params=None):
try:
if params:
s3.put_object(aws_retry=True, **params)
for acl in module.params.get("permission"):
s3.put_object_acl(aws_retry=True, ACL=acl, Bucket=bucket, Key=obj)
except is_boto3_error_code(IGNORE_S3_DROP_IN_EXCEPTIONS):
module.warn(
"PutObjectAcl is not implemented by your storage provider. Set the permissions parameters to the empty list"
" to avoid this warning"
)
except is_boto3_error_code("AccessControlListNotSupported"): # pylint: disable=duplicate-except
module.warn("PutObjectAcl operation : The bucket does not allow ACLs.")
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
boto3.exceptions.Boto3Error,
) as e: # pylint: disable=duplicate-except
raise S3ObjectFailure(f"Failed while creating object {obj}.", e)
def create_dirkey(module, s3, bucket, obj, encrypt, expiry):
if module.check_mode:
module.exit_json(msg="PUT operation skipped - running in check mode", changed=True)
params = {"Bucket": bucket, "Key": obj, "Body": b""}
params.update(
get_extra_params(
encrypt,
module.params.get("encryption_mode"),
module.params.get("encryption_kms_key_id"),
)
)
put_object_acl(module, s3, bucket, obj, params)
# Tags
tags, _changed = ensure_tags(s3, module, bucket, obj)
url = put_download_url(s3, bucket, obj, expiry)
module.exit_json(
msg=f"Virtual directory {obj} created in bucket {bucket}",
url=url,
tags=tags,
changed=True,
)
def path_check(path):
if os.path.exists(path):
return True
else:
return False
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(
encrypt=None,
encryption_mode=None,
encryption_kms_key_id=None,
metadata=None,
):
extra = {}
if encrypt:
extra["ServerSideEncryption"] = encryption_mode
if encryption_kms_key_id and encryption_mode == "aws:kms":
extra["SSEKMSKeyId"] = encryption_kms_key_id
if metadata:
extra["Metadata"] = {}
# determine object metadata and extra arguments
for option in metadata:
extra_args_option = option_in_extra_args(option)
if extra_args_option:
extra[extra_args_option] = metadata[option]
else:
extra["Metadata"][option] = metadata[option]
return extra
def option_in_extra_args(option):
temp_option = option.replace("-", "").lower()
allowed_extra_args = {
"acl": "ACL",
"cachecontrol": "CacheControl",
"contentdisposition": "ContentDisposition",
"contentencoding": "ContentEncoding",
"contentlanguage": "ContentLanguage",
"contenttype": "ContentType",
"expires": "Expires",
"grantfullcontrol": "GrantFullControl",
"grantread": "GrantRead",
"grantreadacp": "GrantReadACP",
"grantwriteacp": "GrantWriteACP",
"metadata": "Metadata",
"requestpayer": "RequestPayer",
"serversideencryption": "ServerSideEncryption",
"storageclass": "StorageClass",
"ssecustomeralgorithm": "SSECustomerAlgorithm",
"ssecustomerkey": "SSECustomerKey",
"ssecustomerkeymd5": "SSECustomerKeyMD5",
"ssekmskeyid": "SSEKMSKeyId",
"websiteredirectlocation": "WebsiteRedirectLocation",
}
if temp_option in allowed_extra_args:
return allowed_extra_args[temp_option]
def upload_s3file(
module,
s3,
bucket,
obj,
expiry,
metadata,
encrypt,
headers,
src=None,
content=None,
acl_disabled=False,
):
if module.check_mode:
module.exit_json(msg="PUT operation skipped - running in check mode", changed=True)
try:
extra = get_extra_params(
encrypt,
module.params.get("encryption_mode"),
module.params.get("encryption_kms_key_id"),
metadata,
)
if module.params.get("permission"):
permissions = module.params["permission"]
if isinstance(permissions, str):
extra["ACL"] = permissions
elif isinstance(permissions, list):
extra["ACL"] = permissions[0]
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)
else:
f = io.BytesIO(content)
s3.upload_fileobj(aws_retry=True, Fileobj=f, Bucket=bucket, Key=obj, ExtraArgs=extra)
except (
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
boto3.exceptions.Boto3Error,
) as e:
raise S3ObjectFailure("Unable to complete PUT operation.", e)
if not acl_disabled:
put_object_acl(module, s3, bucket, obj)
# Tags
tags, _changed = ensure_tags(s3, module, bucket, obj)
url = put_download_url(s3, bucket, obj, expiry)
module.exit_json(msg="PUT operation complete", url=url, tags=tags, changed=True)
def download_s3file(module, s3, bucket, obj, dest, retries, version=None):
if module.check_mode:
module.exit_json(msg="GET operation skipped - running in check mode", changed=True)
# retries is the number of loops; range/xrange needs to be one
# more to get that count of loops.
try:
# Note: Something of a permissions related hack
# get_object returns the HEAD information, plus a *stream* which can be read.
# because the stream's dropped on the floor, we never pull the data and this is the
# functional equivalent of calling get_head which still relying on the 'GET' permission
if version:
s3.get_object(aws_retry=True, Bucket=bucket, Key=obj, VersionId=version)
else:
s3.get_object(aws_retry=True, Bucket=bucket, Key=obj)
except is_boto3_error_code(["404", "403"]) as e:
# AccessDenied errors may be triggered if 1) file does not exist or 2) file exists but
# user does not have the s3:GetObject permission. 404 errors are handled by download_file().
module.fail_json_aws(e, msg=f"Could not find the key {obj}.")
except is_boto3_error_message("require AWS Signature Version 4"): # pylint: disable=duplicate-except
raise Sigv4Required()
except is_boto3_error_code("InvalidArgument") as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg=f"Could not find the key {obj}.")
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
boto3.exceptions.Boto3Error,
) as e: # pylint: disable=duplicate-except
raise S3ObjectFailure(f"Could not find the key {obj}.", e)
optional_kwargs = {"ExtraArgs": {"VersionId": version}} if version else {}
for x in range(0, retries + 1):
try:
s3.download_file(bucket, obj, dest, aws_retry=True, **optional_kwargs)
module.exit_json(msg="GET operation complete", changed=True)
except (
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
boto3.exceptions.Boto3Error,
) as e:
# actually fail on last pass through the loop.
if x >= retries:
raise S3ObjectFailure(f"Failed while downloading {obj}.", e)
# otherwise, try again, this may be a transient timeout.
except SSLError as e: # will ClientError catch SSLError?
# actually fail on last pass through the loop.
if x >= retries:
module.fail_json_aws(e, msg="s3 download failed")
# otherwise, try again, this may be a transient timeout.
def download_s3str(module, s3, bucket, obj, version=None):
if module.check_mode:
module.exit_json(msg="GET operation skipped - running in check mode", changed=True)
try:
if version:
contents = to_native(
s3.get_object(aws_retry=True, Bucket=bucket, Key=obj, VersionId=version)["Body"].read()
)
else:
contents = to_native(s3.get_object(aws_retry=True, Bucket=bucket, Key=obj)["Body"].read())
module.exit_json(msg="GET operation complete", contents=contents, changed=True)
except is_boto3_error_message("require AWS Signature Version 4"):
raise Sigv4Required()
except is_boto3_error_code("InvalidArgument") as e: # pylint: disable=duplicate-except
module.fail_json_aws(
e,
msg=f"Failed while getting contents of object {obj} as a string.",
)
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
boto3.exceptions.Boto3Error,
) as e: # pylint: disable=duplicate-except
raise S3ObjectFailure(f"Failed while getting contents of object {obj} as a string.", e)
def get_download_url(module, s3, bucket, obj, expiry, tags=None, changed=True):
try:
url = s3.generate_presigned_url(
# aws_retry=True,
ClientMethod="get_object",
Params={"Bucket": bucket, "Key": obj},
ExpiresIn=expiry,
)
module.exit_json(
msg="Download url:",
url=url,
tags=tags,
expiry=expiry,
changed=changed,
)
except (
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
boto3.exceptions.Boto3Error,
) as e:
raise S3ObjectFailure("Failed while getting download url.", e)
def put_download_url(s3, bucket, obj, expiry):
try:
url = s3.generate_presigned_url(
# aws_retry=True,
ClientMethod="put_object",
Params={"Bucket": bucket, "Key": obj},
ExpiresIn=expiry,
)
except (
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
boto3.exceptions.Boto3Error,
) as e:
raise S3ObjectFailure("Unable to generate presigned URL", e)
return url
def get_current_object_tags_dict(module, s3, bucket, obj, version=None):
try:
if version:
current_tags = s3.get_object_tagging(aws_retry=True, Bucket=bucket, Key=obj, VersionId=version).get(
"TagSet"
)
else:
current_tags = s3.get_object_tagging(aws_retry=True, Bucket=bucket, Key=obj).get("TagSet")
except is_boto3_error_code(IGNORE_S3_DROP_IN_EXCEPTIONS):
module.warn("GetObjectTagging is not implemented by your storage provider.")
return {}
except is_boto3_error_code(["NoSuchTagSet", "NoSuchTagSetError"]):
return {}
return boto3_tag_list_to_ansible_dict(current_tags)
@AWSRetry.jittered_backoff(max_delay=120, catch_extra_error_codes=["NoSuchBucket", "OperationAborted"])
def put_object_tagging(s3, bucket, obj, tags):
s3.put_object_tagging(
Bucket=bucket,
Key=obj,
Tagging={"TagSet": ansible_dict_to_boto3_tag_list(tags)},
)
@AWSRetry.jittered_backoff(max_delay=120, catch_extra_error_codes=["NoSuchBucket", "OperationAborted"])
def delete_object_tagging(s3, bucket, obj):
s3.delete_object_tagging(Bucket=bucket, Key=obj)
def wait_tags_are_applied(module, s3, bucket, obj, expected_tags_dict, version=None):
for _dummy in range(0, 12):
try:
current_tags_dict = get_current_object_tags_dict(module, s3, bucket, obj, version)
except (
botocore.exceptions.ClientError,
botocore.exceptions.BotoCoreError,
boto3.exceptions.Boto3Error,
) as e:
raise S3ObjectFailure("Failed to get object tags.", e)
if current_tags_dict != expected_tags_dict:
time.sleep(5)
else:
return current_tags_dict
module.fail_json(
msg="Object tags failed to apply in the expected time.",
requested_tags=expected_tags_dict,
live_tags=current_tags_dict,
)
def ensure_tags(client, module, bucket, obj):
tags = module.params.get("tags")
purge_tags = module.params.get("purge_tags")
changed = False
try:
current_tags_dict = get_current_object_tags_dict(module, client, bucket, obj)
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
boto3.exceptions.Boto3Error,
) as e: # pylint: disable=duplicate-except
raise S3ObjectFailure("Failed to get object tags.", e)
# Tags is None, we shouldn't touch anything
if tags is None:
return current_tags_dict, changed
if not purge_tags:
# Ensure existing tags that aren't updated by desired tags remain
current_copy = current_tags_dict.copy()
current_copy.update(tags)
tags = current_copy
# Nothing to change, we shouldn't touch anything
if current_tags_dict == tags:
return current_tags_dict, changed
if tags:
try:
put_object_tagging(client, bucket, obj, tags)
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
boto3.exceptions.Boto3Error,
) as e:
raise S3ObjectFailure("Failed to update object tags.", e)
else:
try:
delete_object_tagging(client, bucket, obj)
except (
botocore.exceptions.BotoCoreError,
botocore.exceptions.ClientError,
boto3.exceptions.Boto3Error,
) as e:
raise S3ObjectFailure("Failed to delete object tags.", e)
current_tags_dict = wait_tags_are_applied(module, client, bucket, obj, tags)
changed = True
return current_tags_dict, changed
def get_binary_content(vars):
# the content will be uploaded as a byte string, so we must encode it first
bincontent = None