Last active
August 20, 2017 08:18
-
-
Save shivamMg/9b09add27187c0142eb7ab7a16f7b573 to your computer and use it in GitHub Desktop.
Copy objects from Source S3 Bucket to Destination S3 Bucket (Prefixes are used to filter which objects must be copied where)
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
| import boto3 | |
| MAX_KEYS = 10 | |
| PAGE_SIZE_COUNT = 10 | |
| def s3_copy(src_bucket, src_prefix, dest_bucket, dest_prefix): | |
| """ | |
| Copies objects from Source Bucket Prefix to Destination Bucket Prefix. | |
| s3://srcbucket/srcprefix -> s3://destbucket/destprefix | |
| :param src_bucket: Source S3 Bucket object | |
| :param src_prefix: Source Prefix | |
| :param dest_bucket: Destination S3 Bucket object | |
| :param dest_prefix: Destination Prefix | |
| :return: None | |
| """ | |
| query = src_bucket.objects.filter(Prefix=src_prefix) | |
| for obj in query.page_size(count=PAGE_SIZE_COUNT): | |
| source = dict(Bucket=src_bucket.name, Key=obj.key) | |
| # exclude src_prefix in dest_key | |
| key = obj.key[len(src_prefix):] | |
| dest_key = '{}{}'.format(dest_prefix, key) | |
| print source, dict(Bucket=dest_bucket.name, Key=dest_key) | |
| dest_bucket.copy(source, dest_key) | |
| if __name__ == '__main__': | |
| src_bucket_name = 'srcbucket' | |
| dest_bucket_name = 'destbucket' | |
| # The forward slash in the end makes sure it's a folder-to-folder copy | |
| src_prefix = 'srcprefix/folder/' | |
| dest_prefix = 'destprefix/folder/' | |
| s3 = boto3.resource('s3') | |
| src_bucket = s3.Bucket(src_bucket_name) | |
| dest_bucket = s3.Bucket(dest_bucket_name) | |
| s3_copy(src_bucket, src_prefix, dest_bucket, dest_prefix) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment