close

how to copy s3 object from one bucket to another using python boto3

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about how to copy s3 object from one bucket to another using python boto3 in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

how to copy s3 object from one bucket to another using python boto3?

  1. how to copy s3 object from one bucket to another using python boto3?

    Since you are using s3 service resource, why not use its own copy method all the way?

  2. copy s3 object from one bucket to another using python boto3

    Since you are using s3 service resource, why not use its own copy method all the way?

Method 1

You can try:

import boto3
s3 = boto3.resource('s3')
copy_source = {
      'Bucket': 'mybucket',
      'Key': 'mykey'
    }
bucket = s3.Bucket('otherbucket')
bucket.copy(copy_source, 'otherkey')

or

import boto3
s3 = boto3.resource('s3')
copy_source = {
    'Bucket': 'mybucket',
    'Key': 'mykey'
 }
s3.meta.client.copy(copy_source, 'otherbucket', 'otherkey')

Method 2

Since you are using s3 service resource, why not use its own copy method all the way?

#!/usr/bin/env python
import boto3
s3 = boto3.resource('s3')
source= { 'Bucket' : 'bucketname1', 'Key': 'objectname'}
dest = s3.Bucket('Bucketname2')
dest.copy(source, 'backupfile')

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read