Skip to content

Instantly share code, notes, and snippets.

@Michaelliv
Created April 7, 2022 05:58
Show Gist options
  • Select an option

  • Save Michaelliv/41d9550ccdaed0476cc1c9db063b9c3f to your computer and use it in GitHub Desktop.

Select an option

Save Michaelliv/41d9550ccdaed0476cc1c9db063b9c3f to your computer and use it in GitHub Desktop.
Copies all objects from source s3 path to target s3 path, filters results by last modified date
from datetime import datetime
from typing import Optional, Callable
import awswrangler
import boto3
import pytz
def copy_bucket_content(
source_s3_uri: str,
target_s3_uri: str,
last_modified_begin: datetime = datetime.now(pytz.UTC),
filter_object_path: Optional[Callable[[str], bool]] = None,
aws_profile_name: Optional[str] = None,
):
if aws_profile_name is not None:
boto3.setup_default_session(profile_name=aws_profile_name)
object_list = awswrangler.s3.list_objects(
source_s3_uri,
last_modified_begin=last_modified_begin,
)
if filter_object_path is not None:
object_list = [filter_object_path(o) for o in object_list]
awswrangler.s3.copy_objects(
object_list,
source_path=source_s3_uri,
target_path=target_s3_uri,
)
if __name__ == '__main__':
copy_bucket_content(
source_s3_uri="",
target_s3_uri="",
last_modified_begin=datetime(year=2022, month=4, day=1, tzinfo=pytz.UTC)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment