Created
December 18, 2024 22:41
-
-
Save wdhowe/00802b362e349cef1fbc7600a139fe27 to your computer and use it in GitHub Desktop.
s3 sync in native python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Until boto3 adds sync capability, we are stuck with subprocess or module hacks like this. | |
| # original author: https://github.com/boto/boto3/issues/358#issuecomment-372086466 | |
| import os | |
| from awscli.clidriver import create_clidriver | |
| def aws_cli(*cmd): | |
| old_env = dict(os.environ) | |
| try: | |
| # Environment | |
| env = os.environ.copy() | |
| env['LC_CTYPE'] = u'en_US.UTF' | |
| os.environ.update(env) | |
| # Run awscli in the same process | |
| print(f"Running command: {cmd}") | |
| exit_code = create_clidriver().main(cmd) | |
| # Deal with problems | |
| if exit_code > 0: | |
| raise RuntimeError('AWS CLI exited with code {}'.format(exit_code)) | |
| finally: | |
| os.environ.clear() | |
| os.environ.update(old_env) | |
| # Example uses | |
| aws_cli('s3', 'sync', '--exclude=*', '--include=*.txt', '/Users/myuser/Downloads', 's3://my-bucket1', '--dryrun') | |
| aws_cli('s3', 'sync', '--exclude=*', '--include=*.txt', 's3://my-bucket1', 's3://my-bucket2', '--dryrun') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment