Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move files between two AWS S3 buckets using boto3

I have to move files between one bucket to another with Python Boto API. (I need it to "Cut" the file from the first Bucket and "Paste" it in the second one). What is the best way to do that?

** Note: Is that matter if I have two different ACCESS KEYS and SECRET KEYS?

like image 362
Gal Avatar asked May 11 '15 07:05

Gal


People also ask

How do I move files from one bucket to another in S3 Python?

import boto3 s3 = boto3. resource('s3') copy_source = { 'Bucket': 'mybucket', 'Key': 'mykey' } bucket = s3. Bucket('otherbucket') bucket. copy(copy_source, 'otherkey') # This is a managed transfer that will perform a multipart copy in # multiple threads if necessary.

Why can't I copy an object between two Amazon S3 buckets?

If the object that you can't copy between buckets is owned by another account, then the object owner can do one of the following: The object owner can grant the bucket owner full control of the object. After the bucket owner owns the object, the bucket policy applies to the object.


3 Answers

If you are using boto3 (the newer boto version) this is quite simple

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

(Docs)

like image 183
David Arenburg Avatar answered Sep 22 '22 06:09

David Arenburg


I think the boto S3 documentation answers your question.

https://github.com/boto/boto/blob/develop/docs/source/s3_tut.rst

Moving files from one bucket to another via boto is effectively a copy of the keys from source to destination and then removing the key from source.

You can get access to the buckets:

import boto

c = boto.connect_s3()
src = c.get_bucket('my_source_bucket')
dst = c.get_bucket('my_destination_bucket')

and iterate the keys:

for k in src.list():
    # copy stuff to your destination here
    dst.copy_key(k.key.name, src.name, k.key.name)
    # then delete the source key
    k.delete()

See also: Is it possible to copy all files from one S3 bucket to another with s3cmd?

like image 44
Freek Wiekmeijer Avatar answered Sep 19 '22 06:09

Freek Wiekmeijer


awscli does the job 30 times faster for me than boto coping and deleting each key. Probably due to multithreading in awscli. If you still want to run it from your python script without calling shell commands from it, you may try something like this:

Install awscli python package:

sudo pip install awscli

And then it is as simple as this:

import os
if os.environ.get('LC_CTYPE', '') == 'UTF-8':
    os.environ['LC_CTYPE'] = 'en_US.UTF-8'

from awscli.clidriver import create_clidriver
driver = create_clidriver()
driver.main('s3 mv source_bucket target_bucket --recursive'.split())
like image 25
Artem Fedosov Avatar answered Sep 18 '22 06:09

Artem Fedosov