Skip to content

Instantly share code, notes, and snippets.

@koleror
Last active March 5, 2018 14:53
Show Gist options
  • Save koleror/e88835f6e045d5c9f16bbf1e5a306dec to your computer and use it in GitHub Desktop.
Save koleror/e88835f6e045d5c9f16bbf1e5a306dec to your computer and use it in GitHub Desktop.
Migrate AWS S3 bucket content to another bucket, keeping metadatas
import boto3
old_access_key_id = '***'
old_secret_access_key = '***'
old_bucket_name = '***'
new_access_key_id = '***'
new_secret_access_key = '***'
new_bucket_name = '***'
old_s3 = boto3.resource(
's3'
aws_access_key_id=old_access_key_id,
aws_secret_access_key=old_secret_access_key,
)
old_bucket = s3.Bucket(old_bucket_name)
new_s3 = boto3.resource(
's3',
aws_access_key_id=new_access_key_id,
aws_secret_access_key=new_secret_access_key,
)
new_bucket = new_s3.Bucket(new_bucket_name)
for obj_sumary in old_bucket.objects.all():
obj = obj_sumary.Object()
new_bucket.put_object(
Key=obj.key,
Body=obj.get()['Body'].read(),
Metadata=obj.metadata
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment