Skip to content

Commit

Permalink
Merge pull request #692 from mandar242/issues-689-s3_sync-file_root
Browse files Browse the repository at this point in the history
s3_sync: file_root parameter - handle individual file path

SUMMARY

Added ability to handle individual file path and upload an individual file to s3 bucket.
Fixes #689.

ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

s3_sync

Reviewed-by: Markus Bergholz <[email protected]>
Reviewed-by: None <None>
Reviewed-by: Mark Chappell <None>
Reviewed-by: None <None>
  • Loading branch information
ansible-zuul[bot] authored Aug 20, 2021
2 parents 7802551 + 6295318 commit 5067d0a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 35 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/692-s3_sync-individual-file-path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- s3_sync - fix handling individual file path to upload a individual file to s3 bucket (https://github.com/ansible-collections/community.aws/pull/692).
91 changes: 56 additions & 35 deletions plugins/modules/s3_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
file_root: roles/s3/files/
storage_class: GLACIER
- name: basic individual file upload
community.aws.s3_sync:
bucket: tedder
file_root: roles/s3/files/file_name
- name: all the options
community.aws.s3_sync:
bucket: tedder
Expand Down Expand Up @@ -322,41 +327,57 @@ def calculate_multipart_etag(source_path, chunk_size=DEFAULT_CHUNK_SIZE):

def gather_files(fileroot, include=None, exclude=None):
ret = []
for (dirpath, dirnames, filenames) in os.walk(fileroot):
for fn in filenames:
fullpath = os.path.join(dirpath, fn)
# include/exclude
if include:
found = False
for x in include.split(','):
if fnmatch.fnmatch(fn, x):
found = True
if not found:
# not on the include list, so we don't want it.
continue

if exclude:
found = False
for x in exclude.split(','):
if fnmatch.fnmatch(fn, x):
found = True
if found:
# skip it, even if previously included.
continue

chopped_path = os.path.relpath(fullpath, start=fileroot)
fstat = os.stat(fullpath)
f_size = fstat[osstat.ST_SIZE]
f_modified_epoch = fstat[osstat.ST_MTIME]
ret.append({
'fullpath': fullpath,
'chopped_path': chopped_path,
'modified_epoch': f_modified_epoch,
'bytes': f_size,
})
# dirpath = path *to* the directory
# dirnames = subdirs *in* our directory
# filenames

if os.path.isfile(fileroot):
fullpath = fileroot
fstat = os.stat(fullpath)
path_array = fileroot.split('/')
chopped_path = path_array[-1]
f_size = fstat[osstat.ST_SIZE]
f_modified_epoch = fstat[osstat.ST_MTIME]
ret.append({
'fullpath': fullpath,
'chopped_path': chopped_path,
'modified_epoch': f_modified_epoch,
'bytes': f_size,
})

else:
for (dirpath, dirnames, filenames) in os.walk(fileroot):
for fn in filenames:
fullpath = os.path.join(dirpath, fn)
# include/exclude
if include:
found = False
for x in include.split(','):
if fnmatch.fnmatch(fn, x):
found = True
if not found:
# not on the include list, so we don't want it.
continue

if exclude:
found = False
for x in exclude.split(','):
if fnmatch.fnmatch(fn, x):
found = True
if found:
# skip it, even if previously included.
continue

chopped_path = os.path.relpath(fullpath, start=fileroot)
fstat = os.stat(fullpath)
f_size = fstat[osstat.ST_SIZE]
f_modified_epoch = fstat[osstat.ST_MTIME]
ret.append({
'fullpath': fullpath,
'chopped_path': chopped_path,
'modified_epoch': f_modified_epoch,
'bytes': f_size,
})
# dirpath = path *to* the directory
# dirnames = subdirs *in* our directory
# filenames
return ret


Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/s3_sync/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test_bucket: "{{ tiny_prefix }}-testbucket-ansible"
test_bucket_2: "{{ tiny_prefix }}-testbucket-ansible-2"
test_bucket_3: "{{ tiny_prefix }}-testbucket-ansible-3"
23 changes: 23 additions & 0 deletions tests/integration/targets/s3_sync/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
- assert:
that:
- output is changed

# ============================================================

- name: Sync files already present
Expand All @@ -99,6 +100,27 @@
that:
- output is not changed

# ============================================================
- name: Create a third s3_bucket
s3_bucket:
name: "{{ test_bucket_3 }}"
state: present
register: output

- assert:
that:
- output.changed
- output.name == "{{ test_bucket_3 }}"
- not output.requester_pays

- name: Sync individual file with remote bucket
s3_sync:
bucket: "{{ test_bucket_3 }}"
file_root: "{{ output_dir }}/s3_sync/test1.txt"
register: output
- assert:
that:
- output is changed

# ============================================================
# DOCUMENTATION EXAMPLES
Expand Down Expand Up @@ -132,3 +154,4 @@
with_items:
- "{{ test_bucket }}"
- "{{ test_bucket_2 }}"
- "{{ test_bucket_3 }}"

0 comments on commit 5067d0a

Please sign in to comment.