Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AWS S3] Copy file #1016

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions python/lib/aws_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,32 @@ def delete_file(self, s3_object_name):
except Exception as err:
raise Exception(f"{s3_object_name} download failure = {format(err)}")

def copy_file(self, src_s3_object_name, dst_s3_object_name):
"""
Function to copy a s3 file or directory.

:param src_s3_object_name: name of the source s3 file or directory
:type src_s3_object_name: str
:param dst_s3_object_name: name of the destination s3 file or directory
:type dst_s3_object_name: str
"""

print(f"Copying {src_s3_object_name} to {dst_s3_object_name}")

try:
(src_s3_bucket_name, src_s3_bucket, src_s3_file_name) = self.get_s3_object_path_part(src_s3_object_name)
(dst_s3_bucket_name, dst_s3_bucket, dst_s3_file_name) = self.get_s3_object_path_part(dst_s3_object_name)
for obj in src_s3_bucket.objects.filter(Prefix=src_s3_file_name):
subcontent = obj.key.replace(src_s3_file_name, "")
dst_s3_bucket.Object(
dst_s3_file_name + subcontent
).copy_from(
CopySource=f'{obj.bucket_name}/{obj.key}'
)
src_s3_bucket.Object(obj.key).delete()
except Exception as err:
raise Exception(f"{src_s3_object_name} => {dst_s3_object_name} copy failure = {format(err)}")

def get_s3_object_path_part(self, s3_object_name):
"""
Function to dissect a s3 file to extract the file prefix and the bucket name
Expand Down